001package com.hfg.xml.msofficexml.docx.part;
002
003import java.util.HashMap;
004import java.util.List;
005import java.util.Map;
006
007import com.hfg.util.collection.CollectionUtil;
008import com.hfg.xml.msofficexml.docx.Docx;
009import com.hfg.xml.msofficexml.docx.wordprocessingml.*;
010import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlCharacterStyle;
011import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlNumberingStyle;
012import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlParagraphStyle;
013import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlStyle;
014import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlStyles;
015import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlTableStyle;
016import com.hfg.xml.msofficexml.part.OfficeXMLPart;
017
018
019//------------------------------------------------------------------------------
020/**
021 Office Open XML document styles part.
022
023 @author J. Alex Taylor, hairyfatguy.com
024 */
025//------------------------------------------------------------------------------
026// com.hfg XML/HTML Coding Library
027//
028// This library is free software; you can redistribute it and/or
029// modify it under the terms of the GNU Lesser General Public
030// License as published by the Free Software Foundation; either
031// version 2.1 of the License, or (at your option) any later version.
032//
033// This library is distributed in the hope that it will be useful,
034// but WITHOUT ANY WARRANTY; without even the implied warranty of
035// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
036// Lesser General Public License for more details.
037//
038// You should have received a copy of the GNU Lesser General Public
039// License along with this library; if not, write to the Free Software
040// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
041//
042// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
043// jataylor@hairyfatguy.com
044//------------------------------------------------------------------------------
045
046public class StylesPart extends OfficeXMLPart
047{
048    Map<String, WmlStyle> mStyleMap = new HashMap<>(50);
049
050   private WmlDocDefaults   mDocDefaults;
051
052   //---------------------------------------------------------------------------
053    public StylesPart(Docx inDocx)
054    {
055        super(inDocx);
056        setFile(WmlXML.STYLES_FILE);
057
058        setRootNode(new WmlStyles(inDocx));
059        defineDefaultStyles();
060    }
061
062
063   //---------------------------------------------------------------------------
064   /**
065    * Returns the document defaults tag if one exists or else instantiates a new one.
066    * @return the document defaults to be used
067    */
068   public WmlDocDefaults getDocDefaults()
069   {
070      if (null == mDocDefaults)
071      {
072         // Check if it has been added via addSubtag()...
073         mDocDefaults = getRootNode().getOptionalSubtagByName(WmlXML.DOCUMENT_DEFAULTS.getLocalName());
074         if (null == mDocDefaults)
075         {
076            mDocDefaults = new WmlDocDefaults((Docx)getParentDoc());
077            getRootNode().addSubtag(mDocDefaults);
078         }
079      }
080
081      return mDocDefaults;
082   }
083
084   //---------------------------------------------------------------------------
085    public WmlStyle getStyle(String inId)
086    {
087       return mStyleMap.get(inId);
088    }
089
090    //---------------------------------------------------------------------------
091    public String addStyle(WmlStyle inValue)
092    {
093       ((WmlStyles)getRootNode()).addStyle(inValue);
094
095       mStyleMap.put(inValue.getId(), inValue);
096
097       return inValue.getId();
098    }
099
100    //---------------------------------------------------------------------------
101    private void defineDefaultStyles()
102    {
103       List<WmlStyle> defaultParagraphStyles = WmlParagraphStyle.generateDefaultStyles((Docx) getParentDoc());
104       if (CollectionUtil.hasValues(defaultParagraphStyles))
105       {
106         for (WmlStyle style : defaultParagraphStyles)
107         {
108            addStyle(style);
109         }
110       }
111
112       List<WmlStyle> defaultCharacterStyles = WmlCharacterStyle.generateDefaultStyles((Docx) getParentDoc());
113       if (CollectionUtil.hasValues(defaultCharacterStyles))
114       {
115         for (WmlStyle style : defaultCharacterStyles)
116         {
117            addStyle(style);
118         }
119       }
120
121       List<WmlStyle> defaultNumberingStyles = WmlNumberingStyle.generateDefaultStyles((Docx) getParentDoc());
122       if (CollectionUtil.hasValues(defaultNumberingStyles))
123       {
124         for (WmlStyle style : defaultNumberingStyles)
125         {
126            addStyle(style);
127         }
128       }
129
130       List<WmlStyle> defaultTableStyles = WmlTableStyle.generateDefaultStyles((Docx) getParentDoc());
131       if (CollectionUtil.hasValues(defaultTableStyles))
132       {
133         for (WmlStyle style : defaultTableStyles)
134         {
135            addStyle(style);
136         }
137       }
138    }
139}
140
141
142/*
143  <w:style w:type="paragraph" w:default="1" w:styleId="Normal">
144    <w:name w:val="Normal"/>
145    <w:qFormat/>
146  </w:style>
147  <w:style w:type="character" w:default="1" w:styleId="DefaultParagraphFont">
148    <w:name w:val="Default Paragraph Font"/>
149    <w:semiHidden/>
150    <w:unhideWhenUsed/>
151  </w:style>
152  <w:style w:type="table" w:default="1" w:styleId="TableNormal">
153    <w:name w:val="Normal Table"/>
154    <w:semiHidden/>
155    <w:unhideWhenUsed/>
156    <w:qFormat/>
157    <w:tblPr>
158      <w:tblInd w:w="0" w:type="dxa"/>
159      <w:tblCellMar>
160        <w:top w:w="0" w:type="dxa"/>
161        <w:left w:w="108" w:type="dxa"/>
162        <w:bottom w:w="0" w:type="dxa"/>
163        <w:right w:w="108" w:type="dxa"/>
164      </w:tblCellMar>
165    </w:tblPr>
166  </w:style>
167  <w:style w:type="numbering" w:default="1" w:styleId="NoList">
168    <w:name w:val="No List"/>
169    <w:semiHidden/>
170    <w:unhideWhenUsed/>
171  </w:style>
172  <w:style w:type="character" w:styleId="Hyperlink">
173    <w:name w:val="Hyperlink"/>
174    <w:basedOn w:val="DefaultParagraphFont"/>
175    <w:uiPriority w:val="99"/>
176    <w:semiHidden/>
177    <w:unhideWhenUsed/>
178    <w:rsid w:val="001828F2"/>
179    <w:rPr>
180      <w:color w:val="0000FF" w:themeColor="hyperlink"/>
181      <w:u w:val="single"/>
182    </w:rPr>
183  </w:style>
184
185 */
186