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

com.hfg.xml.msofficexml.docx.part.NumberingPart Maven / Gradle / Ivy

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

import java.util.List;

import com.hfg.util.StringBuilderPlus;
import com.hfg.util.collection.CollectionUtil;
import com.hfg.xml.msofficexml.OfficeXML;
import com.hfg.xml.msofficexml.docx.Docx;
import com.hfg.xml.msofficexml.docx.RelationshipXML;
import com.hfg.xml.msofficexml.docx.wordprocessingml.*;

//------------------------------------------------------------------------------
/**
 Represents an Office Open XML numbering 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 class NumberingPart extends DocxPart
{

   public static Integer ORDERED_LIST_ID;
   public static Integer UNORDERED_LIST_ID;

   //###########################################################################
   // PRIVATE FIELDS
   //###########################################################################

   private int mAbstractNumIdSrc = 0;
   private int mNumIdSrc = 1;

   private WmlXMLTag mNumberingTag;

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

   //---------------------------------------------------------------------------
   public NumberingPart(Docx inDocx)
   {
      super(inDocx);

      setFile(WmlXML.NUMBERING_FILE);

      mNumberingTag = new WmlXMLTag(WmlXML.NUMBERING, inDocx);

      mNumberingTag.addXMLNamespaceDeclaration(WmlXML.WORDPROCESSINGML_NAMESPACE)
            .addXMLNamespaceDeclaration(RelationshipXML.RELATIONSHIP_NAMESPACE)
            .addXMLNamespaceDeclaration(OfficeXML.MARKUP_CAPABILITY_NAMESPACE)
            .addXMLNamespaceDeclaration(WmlXML.WORDML_14_NAMESPACE)
            .addXMLNamespaceDeclaration(WmlXML.WORDML_15_NAMESPACE)
            .addXMLNamespaceDeclaration(WmlXML.WORDML_16_NAMESPACE)
            .addXMLNamespaceDeclaration(WmlXML.WORDML_16_COMMENTS_EXTENSIBLE_NAMESPACE)
            .addXMLNamespaceDeclaration(WmlXML.WORDML_16_COMMENTS_IDS_NAMESPACE)
            .addXMLNamespaceDeclaration(WmlXML.WORDML_16_SYMEX_NAMESPACE);

      StringBuilderPlus ignorable = new StringBuilderPlus().setDelimiter(" ")
            .delimitedAppend(WmlXML.WORDML_14_NAMESPACE.getPrefix())
            .delimitedAppend(WmlXML.WORDML_15_NAMESPACE.getPrefix())
            .delimitedAppend(WmlXML.WORDML_16_NAMESPACE.getPrefix())
            .delimitedAppend(WmlXML.WORDML_16_COMMENTS_EXTENSIBLE_NAMESPACE.getPrefix())
            .delimitedAppend(WmlXML.WORDML_16_COMMENTS_IDS_NAMESPACE.getPrefix())
            .delimitedAppend(WmlXML.WORDML_16_SYMEX_NAMESPACE.getPrefix());
      mNumberingTag.setAttribute(OfficeXML.IGNORABLE_ATT, ignorable);

      setRootNode(mNumberingTag);

      defineDefaults();
   }

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

   //---------------------------------------------------------------------------
   public int addNumberingDef(WmlAbstractNumbering inValue)
   {
      mNumberingTag.addSubtag(inValue);

      int numberingId = assignNumberingId();

      WmlXMLTag numTag = mNumberingTag.addSubtag(WmlXML.NUM, getParentDoc());
      numTag.setAttribute(WmlXML.NUMBERING_ID_ATT, numberingId);
      numTag.addSubtag(WmlXML.ABSTRACT_NUM_ID).setAttribute(WmlXML.VALUE_ATT, inValue.getId());

      return numberingId;
   }

   //---------------------------------------------------------------------------
   private synchronized int assignAbstractNumId()
   {
      return mAbstractNumIdSrc++;
   }

   //---------------------------------------------------------------------------
   private synchronized int assignNumberingId()
   {
      return mNumIdSrc++;
   }

   //---------------------------------------------------------------------------
   private void defineDefaults()
   {
      List defaultAbstractNumberings = WmlAbstractNumbering.generateDefaultValues((Docx) getParentDoc());
      if (CollectionUtil.hasValues(defaultAbstractNumberings))
      {
         for (WmlAbstractNumbering numberingDef : defaultAbstractNumberings)
         {
            int numberingId = addNumberingDef(numberingDef);
            WmlNumberingFormat numberingFormat = numberingDef.getLevels().get(0).getNumberingFormat();
            if (numberingFormat.equals(WmlNumberingFormat.bullet))
            {
               UNORDERED_LIST_ID = numberingId;
            }
            else if (numberingFormat.equals(WmlNumberingFormat.decimal))
            {
               ORDERED_LIST_ID = numberingId;
            }
         }
      }
   }
}






© 2015 - 2024 Weber Informatics LLC | Privacy Policy