001package com.hfg.xml.msofficexml.docx.wordprocessingml;
002
003
004import java.io.File;
005import java.io.IOException;
006import java.io.OutputStream;
007
008import com.hfg.xml.msofficexml.RelationshipType;
009import com.hfg.xml.msofficexml.docx.Docx;
010import com.hfg.xml.msofficexml.OfficeOpenXMLTag;
011import com.hfg.xml.msofficexml.docx.RelationshipXML;
012import com.hfg.xml.msofficexml.part.OfficeXMLPart;
013
014public class WmlSubDoc extends OfficeOpenXMLTag
015{
016   private OfficeXMLPart mPart;
017
018   private static int sIdSrc = 1;
019
020   //##########################################################################
021   // CONSTRUCTORS
022   //##########################################################################
023
024   //---------------------------------------------------------------------------
025   public WmlSubDoc(Docx inDocx)
026   {
027      super(WmlXML.SUB_DOC, inDocx);
028   }
029
030   //##########################################################################
031   // PUBLIC METHODS
032   //##########################################################################
033
034   //---------------------------------------------------------------------------
035   @Override
036   public Docx getParentDoc()
037   {
038      return (Docx) super.getParentDoc();
039   }
040
041   //---------------------------------------------------------------------------
042   public WmlSubDoc setDocument(Docx inValue)
043   {
044      String baseFilename = "subDoc" + sIdSrc++;
045
046      mPart = new SubDocPart(inValue);
047      mPart.setFile(new File(WmlXML.WORD_DIR, baseFilename + ".docx"));
048
049      // Add the relationship
050      String relationshipId = getParentDoc().getDocumentPart().getRelationshipPart().addRelationship(RelationshipType.SUB_DOCUMENT, new File(mPart.getFile().getName()));
051      setAttribute(RelationshipXML.ID_ATT, relationshipId);
052
053      return this;
054   }
055
056   //##########################################################################
057   // INNER CLASS
058   //##########################################################################
059
060   private class SubDocPart extends OfficeXMLPart
061   {
062      private Docx mDocx;
063
064      //------------------------------------------------------------------------
065      public SubDocPart(Docx inDocx)
066      {
067         super(inDocx);
068         getParentDoc().addPart(this);
069
070         mDocx = inDocx;
071      }
072
073      //------------------------------------------------------------------------
074      @Override
075      public void toXML(OutputStream inStream)
076      {
077         try
078         {
079            mDocx.write(inStream);
080         }
081         catch (IOException e)
082         {
083            throw new RuntimeException("Problem writing subdocument!", e);
084         }
085      }
086   }
087}
088