com.hfg.xml.msofficexml.part.OfficeOpenXmlRelationshipPart Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.xml.msofficexml.part;
import java.io.File;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import com.hfg.util.collection.CollectionUtil;
import com.hfg.util.FileUtil;
import com.hfg.xml.XMLTag;
import com.hfg.xml.msofficexml.OfficeOpenXmlDocument;
import com.hfg.xml.msofficexml.OfficeOpenXmlRelationship;
import com.hfg.xml.msofficexml.OfficeXML;
import com.hfg.xml.msofficexml.RelationshipType;
//------------------------------------------------------------------------------
/**
Represents an Office Open XML relationship part.
@author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
public class OfficeOpenXmlRelationshipPart extends OfficeXMLPart
{
// Because file paths may not be initially set, we delay rendering out the
// relationship XML until the last moment.
private List mRelationships;
private int mIndex = 1;
//---------------------------------------------------------------------------
public OfficeOpenXmlRelationshipPart(OfficeOpenXmlDocument inOfficeDoc)
{
super(inOfficeDoc);
setRootNode(new XMLTag(OfficeXML.RELATIONSHIPS));
}
//---------------------------------------------------------------------------
public String addRelationship(OfficeOpenXmlRelationship inValue)
{
String id = null;
if (inValue != null)
{
id = generateId();
inValue.setId(id);
if (null == mRelationships)
{
mRelationships = new ArrayList<>(25);
}
mRelationships.add(inValue);
// Register the content type
getParentDoc().getContentTypesPart().addOverride(inValue.getPart(), inValue.getType().getContentType());
}
return id;
}
//---------------------------------------------------------------------------
public String addRelationship(RelationshipType inType, OfficeXMLPart inPart)
{
OfficeOpenXmlRelationship relationship = new OfficeOpenXmlRelationship();
relationship.setPart(inPart)
.setType(inType)
.setId(generateId());
if (null == mRelationships)
{
mRelationships = new ArrayList(25);
}
mRelationships.add(relationship);
return relationship.getId();
}
//---------------------------------------------------------------------------
@Override
public void toXML(Writer inWriter)
{
// Because file paths may not be initially set, we delay rendering out the
// relationship XML until the last moment.
populateXML();
super.toXML(inWriter);
}
//---------------------------------------------------------------------------
@Override
public void toIndentedXML(Writer inWriter, int inInitialIndentLevel, int inIndentSize)
{
// Because file paths may not be initially set, we delay rendering out the
// relationship XML until the last moment.
populateXML();
super.toIndentedXML(inWriter, inInitialIndentLevel, inIndentSize);
}
//---------------------------------------------------------------------------
public void populateXML()
{
XMLTag rootNode = new XMLTag(OfficeXML.RELATIONSHIPS);
if (CollectionUtil.hasValues(mRelationships))
{
for (OfficeOpenXmlRelationship relationship : mRelationships)
{
XMLTag relationshipTag = new XMLTag(OfficeXML.RELATIONSHIP);
relationshipTag.setAttribute(OfficeXML.ID_ATT, relationship.getId());
relationshipTag.setAttribute(OfficeXML.TYPE_ATT, relationship.getType());
relationshipTag.setAttribute(OfficeXML.TARGET_ATT, getRelativePath(relationship.getPart().getFile()));
if (relationship.getType().equals(RelationshipType.SUB_DOCUMENT))
{
relationshipTag.setAttribute(OfficeXML.TARGET_MODE_ATT, "Internal");
}
rootNode.addSubtag(relationshipTag);
}
}
setRootNode(rootNode);
}
//---------------------------------------------------------------------------
private String generateId()
{
return "rId" + (mIndex++);
}
//---------------------------------------------------------------------------
private String getRelativePath(File inFile)
{
String relativePath = FileUtil.getRelativePath(inFile, getFile().getParentFile());
if (relativePath.startsWith("./"))
{
relativePath = relativePath.substring(2);
}
return relativePath;
/* String path = inFile.getPath();
if (path.startsWith(getFile().getParentFile().getParentFile().getPath()))
{
path = path.substring(getFile().getParentFile().getParentFile().getPath().length() + 1);
}
return path;
*/
}
}