001package com.hfg.xml.msofficexml.docx.part; 002 003import java.io.File; 004 005import com.hfg.util.FileUtil; 006import com.hfg.xml.XMLTag; 007import com.hfg.xml.msofficexml.OfficeXML; 008import com.hfg.xml.msofficexml.RelationshipType; 009import com.hfg.xml.msofficexml.docx.Docx; 010import com.hfg.xml.msofficexml.docx.wordprocessingml.WmlXML; 011import com.hfg.xml.msofficexml.part.OfficeXMLPart; 012 013//------------------------------------------------------------------------------ 014/** 015 Represents the VBA relationship part (container) for an Office Open XML. 016 017 @author J. Alex Taylor, hairyfatguy.com 018 */ 019//------------------------------------------------------------------------------ 020// com.hfg XML/HTML Coding Library 021// 022// This library is free software; you can redistribute it and/or 023// modify it under the terms of the GNU Lesser General Public 024// License as published by the Free Software Foundation; either 025// version 2.1 of the License, or (at your option) any later version. 026// 027// This library is distributed in the hope that it will be useful, 028// but WITHOUT ANY WARRANTY; without even the implied warranty of 029// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 030// Lesser General Public License for more details. 031// 032// You should have received a copy of the GNU Lesser General Public 033// License along with this library; if not, write to the Free Software 034// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 035// 036// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 037// jataylor@hairyfatguy.com 038//------------------------------------------------------------------------------ 039 040public class VbaRelationshipPart extends OfficeXMLPart 041{ 042 private int mIndex = 1; 043 044 //--------------------------------------------------------------------------- 045 public VbaRelationshipPart(Docx inDocx) 046 { 047 super(inDocx); 048 049 setFile(WmlXML.VBA_RELATIONSHIP_FILE); 050 051 setRootNode(new XMLTag(OfficeXML.RELATIONSHIPS)); 052 } 053 054 //--------------------------------------------------------------------------- 055 public String addRelationship(RelationshipType inType, File inTarget) 056 { 057 String id = generateId(); 058 059 XMLTag relationshipTag = new XMLTag(OfficeXML.RELATIONSHIP) 060 .setAttribute(OfficeXML.ID_ATT, id) 061 .setAttribute(OfficeXML.TYPE_ATT, inType) 062 .setAttribute(OfficeXML.TARGET_ATT, getRelativePath(inTarget)); 063 064 if (inType.equals(RelationshipType.SUB_DOCUMENT)) 065 { 066 relationshipTag.setAttribute(OfficeXML.TARGET_MODE_ATT, "Internal"); 067 } 068 069 getRootNode().addSubtag(relationshipTag); 070 071 return id; 072 } 073 074 //--------------------------------------------------------------------------- 075 private String generateId() 076 { 077 return "rId" + (mIndex++); 078 } 079 080 //--------------------------------------------------------------------------- 081 private String getRelativePath(File inFile) 082 { 083 String path = inFile.getPath(); 084 if (path.startsWith(getFile().getParentFile().getParentFile().getPath())) 085 { 086 path = path.substring(getFile().getParentFile().getParentFile().getPath().length() + 1); 087 } 088 089 // Also ensure that the file paths consistently use '/' instead of '\' even if 090 // generated on a Windoze system. Windoze separators were causing files to appear corrupt. 091 return FileUtil.convertSeparatorsToUnix(path); 092 } 093}