001package com.hfg.xml.msofficexml.part;
002
003import java.io.Writer;
004import java.util.HashMap;
005import java.util.HashSet;
006import java.util.Map;
007import java.util.Set;
008
009import com.hfg.util.FileUtil;
010import com.hfg.util.collection.CollectionUtil;
011import com.hfg.xml.XMLTag;
012import com.hfg.xml.msofficexml.*;
013
014//------------------------------------------------------------------------------
015/**
016 Represents an Office Open XML content types part.
017
018 @author J. Alex Taylor, hairyfatguy.com
019 */
020//------------------------------------------------------------------------------
021// com.hfg XML/HTML Coding Library
022//
023// This library is free software; you can redistribute it and/or
024// modify it under the terms of the GNU Lesser General Public
025// License as published by the Free Software Foundation; either
026// version 2.1 of the License, or (at your option) any later version.
027//
028// This library is distributed in the hope that it will be useful,
029// but WITHOUT ANY WARRANTY; without even the implied warranty of
030// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
031// Lesser General Public License for more details.
032//
033// You should have received a copy of the GNU Lesser General Public
034// License along with this library; if not, write to the Free Software
035// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
036//
037// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
038// jataylor@hairyfatguy.com
039//------------------------------------------------------------------------------
040
041public class ContentTypesPart extends OfficeXMLPart
042{
043   // Because file paths may not be initially set, we delay rendering out the
044   // relationship XML until the last moment.
045   private Set<OfficeOpenXMLContentType> mDefaults;
046   private Map<OfficeXMLPart, OfficeOpenXMLContentType> mOverrideMap;
047
048   //---------------------------------------------------------------------------
049   public ContentTypesPart(OfficeOpenXmlDocument inOfficeDoc)
050   {
051      super(inOfficeDoc);
052      setFile(OfficeXML.CONTENT_TYPES_FILE);
053      setRootNode(new XMLTag(OfficeXML.TYPES));
054   }
055
056   //---------------------------------------------------------------------------
057   public void addDefault(OfficeOpenXMLContentType inContentType)
058   {
059      if (null == mDefaults)
060      {
061         mDefaults = new HashSet<>(10);
062      }
063
064      mDefaults.add(inContentType);
065   }
066
067   //---------------------------------------------------------------------------
068   public void addOverride(OfficeXMLPart inPart, OfficeOpenXMLContentType inContentType)
069   {
070      if (null == mOverrideMap)
071      {
072         mOverrideMap = new HashMap<>(20);
073      }
074
075      mOverrideMap.put(inPart, inContentType);
076   }
077
078   //---------------------------------------------------------------------------
079   @Override
080   public void toXML(Writer inWriter)
081   {
082      // Because file paths may not be initially set, we delay rendering out the
083      // relationship XML until the last moment.
084      populateXML();
085      super.toXML(inWriter);
086   }
087
088   //---------------------------------------------------------------------------
089   @Override
090   public void toIndentedXML(Writer inWriter, int inInitialIndentLevel, int inIndentSize)
091   {
092      // Because file paths may not be initially set, we delay rendering out the
093      // relationship XML until the last moment.
094      populateXML();
095      super.toIndentedXML(inWriter, inInitialIndentLevel, inIndentSize);
096   }
097
098   //---------------------------------------------------------------------------
099   public void populateXML()
100   {
101      XMLTag rootNode = new XMLTag(OfficeXML.TYPES);
102
103      rootNode.addSubtag(OfficeOpenXMLContentType.APPLICATION.toXMLTag());
104      rootNode.addSubtag(OfficeOpenXMLContentType.RELATIONSHIPS.toXMLTag());
105      rootNode.addSubtag(OfficeOpenXMLContentType.IMAGE_JPEG.toXMLTag());
106      rootNode.addSubtag(OfficeOpenXMLContentType.IMAGE_JPG.toXMLTag());
107      rootNode.addSubtag(OfficeOpenXMLContentType.IMAGE_PNG.toXMLTag());
108
109      if (CollectionUtil.hasValues(mDefaults))
110      {
111         for (OfficeOpenXMLContentType contentType : mDefaults)
112         {
113            rootNode.addSubtag(contentType.toXMLTag());
114         }
115      }
116
117      // Overrides
118      if (CollectionUtil.hasValues(mOverrideMap))
119      {
120         for (OfficeXMLPart part : mOverrideMap.keySet())
121         {
122            XMLTag subtag = mOverrideMap.get(part).toXMLTag();
123            if (part.getFile() != null)
124            {
125               String filePath = FileUtil.convertSeparatorsToUnix(part.getFile().getPath());
126
127               subtag.setAttribute(OfficeXML.PART_NAME_ATT, (filePath.startsWith("/") ? "" : "/") + filePath);
128            }
129
130            rootNode.addSubtag(subtag);
131         }
132      }
133
134      setRootNode(rootNode);
135   }
136
137}