001package com.hfg.xml.msofficexml.docx.wordprocessingml.style; 002 003 004import com.hfg.util.StringUtil; 005import com.hfg.xml.XMLTag; 006import com.hfg.xml.msofficexml.docx.Docx; 007import com.hfg.xml.msofficexml.docx.wordprocessingml.WmlParagraphProperties; 008import com.hfg.xml.msofficexml.docx.wordprocessingml.WmlTextRunProperties; 009import com.hfg.xml.msofficexml.docx.wordprocessingml.WmlXML; 010import com.hfg.xml.msofficexml.docx.wordprocessingml.WmlXMLTag; 011 012//------------------------------------------------------------------------------ 013/** 014 Style for Docx's Office Open XML documents. 015 016 @author J. Alex Taylor, hairyfatguy.com 017 */ 018//------------------------------------------------------------------------------ 019// com.hfg XML/HTML Coding Library 020// 021// This library is free software; you can redistribute it and/or 022// modify it under the terms of the GNU Lesser General Public 023// License as published by the Free Software Foundation; either 024// version 2.1 of the License, or (at your option) any later version. 025// 026// This library is distributed in the hope that it will be useful, 027// but WITHOUT ANY WARRANTY; without even the implied warranty of 028// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 029// Lesser General Public License for more details. 030// 031// You should have received a copy of the GNU Lesser General Public 032// License along with this library; if not, write to the Free Software 033// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 034// 035// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 036// jataylor@hairyfatguy.com 037//------------------------------------------------------------------------------ 038 039public abstract class WmlStyle extends WmlXMLTag 040{ 041 private XMLTag mBasedOn; 042 private XMLTag mNameTag; 043 private XMLTag mLinkTag; 044 private XMLTag mUI_PriorityTag; 045 private XMLTag mSemiHiddenTag; 046 private XMLTag mUnhideWhenUsedTag; 047 private WmlTextRunProperties mRunProperties; 048 private WmlParagraphProperties mParagraphProperties; 049 050 private static int sId = 1; // Used for generating the style id when one isn't specified 051 052 //########################################################################## 053 // CONSTRUCTORS 054 //########################################################################## 055 056 //--------------------------------------------------------------------------- 057 public WmlStyle(WmlStyleType inType, Docx inDocx) 058 { 059 this(inType, "s" + sId++, inDocx); 060 } 061 062 //--------------------------------------------------------------------------- 063 public WmlStyle(WmlStyleType inType, String inId, Docx inDocx) 064 { 065 super(WmlXML.STYLE, inDocx); 066 setType(inType); 067 setAttribute(WmlXML.STYLE_ID_ATT, inId); 068 } 069 070 //########################################################################## 071 // PUBLIC METHODS 072 //########################################################################## 073 074 //--------------------------------------------------------------------------- 075 public WmlStyle setId(String inValue) 076 { 077 setAttribute(WmlXML.STYLE_ID_ATT, inValue); 078 return this; 079 } 080 081 //--------------------------------------------------------------------------- 082 public String getId() 083 { 084 return getAttributeValue(WmlXML.STYLE_ID_ATT.getLocalName()); 085 } 086 087 //--------------------------------------------------------------------------- 088 public WmlStyle setName(String inValue) 089 { 090 if (null == mNameTag) 091 { 092 // Check it it has been added via addSubtag()... 093 mNameTag = getOptionalSubtagByName(WmlXML.NAME); 094 if (null == mNameTag) 095 { 096 mNameTag = new XMLTag(WmlXML.NAME); 097 addSubtag(mNameTag); 098 } 099 } 100 101 mNameTag.setAttribute(WmlXML.VALUE_ATT, inValue); 102 103 return this; 104 } 105 106 //--------------------------------------------------------------------------- 107 public String getName() 108 { 109 return mNameTag != null ? mNameTag.getAttributeValue(WmlXML.VALUE_ATT.getLocalName()) : null; 110 } 111 112 //--------------------------------------------------------------------------- 113 public WmlStyle setBasedOn(String inStyleId) 114 { 115 if (null == mBasedOn) 116 { 117 // Check it it has been added via addSubtag()... 118 mBasedOn = getOptionalSubtagByName(WmlXML.BASED_ON); 119 if (null == mBasedOn) 120 { 121 mBasedOn = new XMLTag(WmlXML.BASED_ON); 122 addSubtag(mBasedOn); 123 } 124 } 125 126 mBasedOn.setAttribute(WmlXML.VALUE, inStyleId); 127 128 return this; 129 } 130 131 //--------------------------------------------------------------------------- 132 public WmlStyle setLink(String inStyleId) 133 { 134 if (null == mLinkTag) 135 { 136 // Check it it has been added via addSubtag()... 137 mLinkTag = getOptionalSubtagByName(WmlXML.LINK); 138 if (null == mLinkTag) 139 { 140 mLinkTag = new XMLTag(WmlXML.LINK); 141 addSubtag(mLinkTag); 142 } 143 } 144 145 mLinkTag.setAttribute(WmlXML.VALUE, inStyleId); 146 147 return this; 148 } 149 150 //--------------------------------------------------------------------------- 151 public WmlStyle setUI_Priority(Integer inValue) 152 { 153 if (null == inValue) 154 { 155 mUI_PriorityTag = null; 156 removeSubtagsByName(WmlXML.UI_PRIORITY); 157 } 158 else 159 { 160 if (null == mUI_PriorityTag) 161 { 162 // Check it it has been added via addSubtag()... 163 mUI_PriorityTag = getOptionalSubtagByName(WmlXML.UI_PRIORITY); 164 if (null == mUI_PriorityTag) 165 { 166 mUI_PriorityTag = new XMLTag(WmlXML.UI_PRIORITY); 167 addSubtag(mUI_PriorityTag); 168 } 169 } 170 171 mUI_PriorityTag.setAttribute(WmlXML.VALUE, inValue); 172 } 173 174 return this; 175 } 176 177 //--------------------------------------------------------------------------- 178 public WmlStyle setSemiHidden() 179 { 180 if (null == mSemiHiddenTag) 181 { 182 // Check it it has been added via addSubtag()... 183 mSemiHiddenTag = getOptionalSubtagByName(WmlXML.SEMI_HIDDEN); 184 if (null == mSemiHiddenTag) 185 { 186 mSemiHiddenTag = new XMLTag(WmlXML.SEMI_HIDDEN); 187 addSubtag(mSemiHiddenTag); 188 } 189 } 190 191 return this; 192 } 193 194 //--------------------------------------------------------------------------- 195 public WmlStyle setUnhideWhenUsed() 196 { 197 if (null == mUnhideWhenUsedTag) 198 { 199 // Check it it has been added via addSubtag()... 200 mUnhideWhenUsedTag = getOptionalSubtagByName(WmlXML.UNHIDE_WHEN_USED); 201 if (null == mUnhideWhenUsedTag) 202 { 203 mUnhideWhenUsedTag = new XMLTag(WmlXML.UNHIDE_WHEN_USED); 204 addSubtag(mUnhideWhenUsedTag); 205 } 206 } 207 208 return this; 209 } 210 211 //--------------------------------------------------------------------------- 212 public WmlStyle setIsDefault(boolean inValue) 213 { 214 setAttribute(WmlXML.DEFAULT_ATT, inValue ? "1" : "0"); 215 return this; 216 } 217 218 //--------------------------------------------------------------------------- 219 private WmlStyle setType(WmlStyleType inValue) 220 { 221 setAttribute(WmlXML.TYPE_ATT, inValue.name()); 222 return this; 223 } 224 225 //--------------------------------------------------------------------------- 226 public WmlStyleType getType() 227 { 228 WmlStyleType type = null; 229 String stringValue = getAttributeValue(WmlXML.TYPE_ATT.getLocalName()); 230 if (StringUtil.isSet(stringValue)) 231 { 232 type = WmlStyleType.valueOf(stringValue); 233 } 234 235 return type; 236 } 237 238 //--------------------------------------------------------------------------- 239 /** 240 * Returns the text run properties tag if one exists or else instantiates a new one. 241 * @return the text run properties for this style 242 */ 243 public WmlTextRunProperties getTextRunProperties() 244 { 245 if (null == mRunProperties) 246 { 247 // Check it it has been added via addSubtag()... 248 mRunProperties = getOptionalSubtagByName(WmlXML.RUN_PROPS); 249 if (null == mRunProperties) 250 { 251 mRunProperties = new WmlTextRunProperties(getParentDoc()); 252 addSubtag(0, mRunProperties); 253 } 254 } 255 256 return mRunProperties; 257 } 258 259 //--------------------------------------------------------------------------- 260 /** 261 * Returns the paragraph properties tag if one exists or else instantiates a new one. 262 * @return the paragraph properties for this style 263 */ 264 public WmlParagraphProperties getParagraphProperties() 265 { 266 if (null == mParagraphProperties) 267 { 268 // Check it it has been added via addSubtag()... 269 mParagraphProperties = getOptionalSubtagByName(WmlXML.PARAGRAPH_PROPS); 270 if (null == mParagraphProperties) 271 { 272 mParagraphProperties = new WmlParagraphProperties(getParentDoc()); 273 addSubtag(0, mParagraphProperties); 274 } 275 } 276 277 return mParagraphProperties; 278 } 279 280}