All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.xml.msofficexml.docx.wordprocessingml.WmlSubDoc Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.xml.msofficexml.docx.wordprocessingml;


import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import com.hfg.xml.msofficexml.RelationshipType;
import com.hfg.xml.msofficexml.docx.Docx;
import com.hfg.xml.msofficexml.OfficeOpenXMLTag;
import com.hfg.xml.msofficexml.docx.RelationshipXML;
import com.hfg.xml.msofficexml.part.OfficeXMLPart;

public class WmlSubDoc extends OfficeOpenXMLTag
{
   private OfficeXMLPart mPart;

   private static int sIdSrc = 1;

   //##########################################################################
   // CONSTRUCTORS
   //##########################################################################

   //---------------------------------------------------------------------------
   public WmlSubDoc(Docx inDocx)
   {
      super(WmlXML.SUB_DOC, inDocx);
   }

   //##########################################################################
   // PUBLIC METHODS
   //##########################################################################

   //---------------------------------------------------------------------------
   @Override
   public Docx getParentDoc()
   {
      return (Docx) super.getParentDoc();
   }

   //---------------------------------------------------------------------------
   public WmlSubDoc setDocument(Docx inValue)
   {
      String baseFilename = "subDoc" + sIdSrc++;

      mPart = new SubDocPart(inValue);
      mPart.setFile(new File(WmlXML.WORD_DIR, baseFilename + ".docx"));

      // Add the relationship
      String relationshipId = getParentDoc().getMainDocPartRelationshipPart().addRelationship(RelationshipType.SUB_DOCUMENT, new File(mPart.getFile().getName()));
      setAttribute(RelationshipXML.ID_ATT, relationshipId);

      return this;
   }

   //##########################################################################
   // INNER CLASS
   //##########################################################################

   private class SubDocPart extends OfficeXMLPart
   {
      private Docx mDocx;

      //------------------------------------------------------------------------
      public SubDocPart(Docx inDocx)
      {
         super(inDocx);
         getParentDoc().addPart(this);

         mDocx = inDocx;
      }

      //------------------------------------------------------------------------
      @Override
      public void toXML(OutputStream inStream)
      {
         try
         {
            mDocx.write(inStream);
         }
         catch (IOException e)
         {
            throw new RuntimeException("Problem writing subdocument!", e);
         }
      }
   }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy