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

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

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

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hfg.util.collection.CollectionUtil;
import com.hfg.xml.msofficexml.docx.Docx;
import com.hfg.xml.msofficexml.docx.wordprocessingml.*;
import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlCharacterStyle;
import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlNumberingStyle;
import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlParagraphStyle;
import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlStyle;
import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlStyles;
import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlTableStyle;
import com.hfg.xml.msofficexml.part.OfficeXMLPart;


//------------------------------------------------------------------------------
/**
 Office Open XML document styles 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 StylesPart extends OfficeXMLPart
{
    Map mStyleMap = new HashMap<>(50);

   private WmlDocDefaults   mDocDefaults;

   //---------------------------------------------------------------------------
    public StylesPart(Docx inDocx)
    {
        super(inDocx);
        setFile(WmlXML.STYLES_FILE);

        setRootNode(new WmlStyles(inDocx));
        defineDefaultStyles();
    }


   //---------------------------------------------------------------------------
   /**
    * Returns the document defaults tag if one exists or else instantiates a new one.
    * @return the document defaults to be used
    */
   public WmlDocDefaults getDocDefaults()
   {
      if (null == mDocDefaults)
      {
         // Check if it has been added via addSubtag()...
         mDocDefaults = getRootNode().getOptionalSubtagByName(WmlXML.DOCUMENT_DEFAULTS.getLocalName());
         if (null == mDocDefaults)
         {
            mDocDefaults = new WmlDocDefaults((Docx)getParentDoc());
            getRootNode().addSubtag(mDocDefaults);
         }
      }

      return mDocDefaults;
   }

   //---------------------------------------------------------------------------
    public WmlStyle getStyle(String inId)
    {
       return mStyleMap.get(inId);
    }

    //---------------------------------------------------------------------------
    public String addStyle(WmlStyle inValue)
    {
       ((WmlStyles)getRootNode()).addStyle(inValue);

       mStyleMap.put(inValue.getId(), inValue);

       return inValue.getId();
    }

    //---------------------------------------------------------------------------
    private void defineDefaultStyles()
    {
       List defaultParagraphStyles = WmlParagraphStyle.generateDefaultStyles((Docx) getParentDoc());
       if (CollectionUtil.hasValues(defaultParagraphStyles))
       {
         for (WmlStyle style : defaultParagraphStyles)
         {
            addStyle(style);
         }
       }

       List defaultCharacterStyles = WmlCharacterStyle.generateDefaultStyles((Docx) getParentDoc());
       if (CollectionUtil.hasValues(defaultCharacterStyles))
       {
         for (WmlStyle style : defaultCharacterStyles)
         {
            addStyle(style);
         }
       }

       List defaultNumberingStyles = WmlNumberingStyle.generateDefaultStyles((Docx) getParentDoc());
       if (CollectionUtil.hasValues(defaultNumberingStyles))
       {
         for (WmlStyle style : defaultNumberingStyles)
         {
            addStyle(style);
         }
       }

       List defaultTableStyles = WmlTableStyle.generateDefaultStyles((Docx) getParentDoc());
       if (CollectionUtil.hasValues(defaultTableStyles))
       {
         for (WmlStyle style : defaultTableStyles)
         {
            addStyle(style);
         }
       }
    }
}


/*
  
    
    
  
  
    
    
    
  
  
    
    
    
    
    
      
      
        
        
        
        
      
    
  
  
    
    
    
  
  
    
    
    
    
    
    
    
      
      
    
  

 */





© 2015 - 2024 Weber Informatics LLC | Privacy Policy