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.net.URL;
import java.util.ArrayList;
import java.util.List;
import com.hfg.util.StringUtil;
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 abstract 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 = inValue.getId();
if (!StringUtil.isSet(id))
{
id = generateId();
inValue.setId(id);
}
if (null == mRelationships)
{
mRelationships = new ArrayList<>(25);
}
mRelationships.add(inValue);
if (inValue.getPart() != null)
{
// 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();
}
//---------------------------------------------------------------------------
public String addRelationship(RelationshipType inType, File inTarget)
{
return addRelationship(new OfficeOpenXmlRelationship()
.setId(generateId())
.setType(inType)
.setTarget(getRelativePath(inTarget)));
}
//---------------------------------------------------------------------------
public String addHyperlink(URL inValue)
{
return addRelationship(new OfficeOpenXmlRelationship()
.setId(generateId())
.setType(RelationshipType.HYPERLINK)
.setTarget(inValue.toString())
.setTargetMode("External"));
}
//---------------------------------------------------------------------------
public String addMedia(MediaPart inValue)
{
// getParentDoc().addMediaPart(inValue);
return addRelationship(RelationshipType.IMAGE, inValue);
}
//---------------------------------------------------------------------------
@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());
String target = relationship.getTarget();
if (null == target)
{
target = getRelativePath(relationship.getPart().getFile());
}
relationshipTag.setAttribute(OfficeXML.TARGET_ATT, target);
String targetMode = relationship.getTargetMode();
if (StringUtil.isSet(targetMode))
{
relationshipTag.setAttribute(OfficeXML.TARGET_MODE_ATT, targetMode);
}
rootNode.addSubtag(relationshipTag);
}
}
setRootNode(rootNode);
}
//---------------------------------------------------------------------------
protected 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;
}
*/
//---------------------------------------------------------------------------
protected String getRelativePath(File inFile)
{
String path = inFile.getPath();
if (path.startsWith(getFile().getParentFile().getParentFile().getPath()))
{
path = path.substring(getFile().getParentFile().getParentFile().getPath().length() + 1);
}
// Also ensure that the file paths consistently use '/' instead of '\' even if
// generated on a Windoze system. Windoze separators were causing files to appear corrupt.
return FileUtil.convertSeparatorsToUnix(path);
}
}