Changeset 1096 for trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/StyleDefinitionsPart.java
- Timestamp:
- 02/27/10 06:52:46 (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/StyleDefinitionsPart.java
r976 r1096 23 23 import java.io.IOException; 24 24 25 import javax.xml.bind.JAXBContext;26 import javax.xml.bind.JAXBException;27 import javax.xml.bind.Unmarshaller;28 29 25 import org.apache.log4j.Logger; 30 26 import org.docx4j.XmlUtils; … … 32 28 import org.docx4j.openpackaging.exceptions.Docx4JException; 33 29 import org.docx4j.openpackaging.exceptions.InvalidFormatException; 30 import org.docx4j.openpackaging.packages.WordprocessingMLPackage; 34 31 import org.docx4j.openpackaging.parts.JaxbXmlPart; 35 32 import org.docx4j.openpackaging.parts.PartName; … … 42 39 import org.docx4j.wml.Style.BasedOn; 43 40 41 import javax.xml.bind.JAXBContext; 42 import javax.xml.bind.JAXBException; 43 import javax.xml.bind.Unmarshaller; 44 44 45 45 46 public final class StyleDefinitionsPart extends JaxbXmlPart<Styles> { … … 47 48 private static Logger log = Logger.getLogger(StyleDefinitionsPart.class); 48 49 49 50 50 public StyleDefinitionsPart(PartName partName) throws InvalidFormatException { 51 51 super(partName); … … 292 292 293 293 // Now point Normal at this 294 Style normal = getStyleById("Normal"); 294 Style normal = getDefaultParagraphStyle(); 295 if (normal==null) { 296 log.warn("No default paragraph style!!"); 297 normal = Context.getWmlObjectFactory().createStyle(); 298 normal.setType("paragraph"); 299 normal.setStyleId("Normal"); 300 301 org.docx4j.wml.Style.Name n = Context.getWmlObjectFactory().createStyleName(); 302 n.setVal("Normal"); 303 normal.setName(n); 304 this.jaxbElement.getStyle().add(normal); 305 } 306 295 307 BasedOn based = Context.getWmlObjectFactory().createStyleBasedOn(); 296 308 based.setVal(ROOT_NAME); … … 310 322 } 311 323 return null; 324 } 325 326 private Style defaultCharacterStyle; 327 public Style getDefaultCharacterStyle() { 328 329 if (defaultCharacterStyle==null) { 330 defaultCharacterStyle = getDefaultStyle("character"); 331 } 332 // OpenOffice conversion to docx 333 // doesn't necessarily contain a default character style 334 // so manufacture one 335 if (defaultCharacterStyle==null) { 336 try { 337 defaultCharacterStyle = (Style)XmlUtils.unmarshalString(DEFAULT_CHARACTER_STYLE_DEFAULT); 338 this.jaxbElement.getStyle().add(defaultCharacterStyle); 339 } catch (JAXBException e) { 340 e.printStackTrace(); 341 } 342 } 343 return defaultCharacterStyle; 344 } 345 346 private final static String DEFAULT_CHARACTER_STYLE_DEFAULT = "<w:style w:type=\"character\" w:default=\"1\" w:styleId=\"DefaultParagraphFont\" " + Namespaces.W_NAMESPACE_DECLARATION + "><w:name w:val=\"Default Paragraph Font\" /></w:style>"; 347 348 349 private Style defaultParagraphStyle; 350 public Style getDefaultParagraphStyle() { 351 352 if (defaultParagraphStyle==null) { 353 defaultParagraphStyle = getDefaultStyle("paragraph"); 354 } 355 // OpenOffice conversion to docx 356 // doesn't set default, so use name 357 // (alternatively, could use id=style0) 358 if (defaultParagraphStyle==null) { 359 for ( org.docx4j.wml.Style s : this.jaxbElement.getStyle() ) { 360 if( s.getType().equals("paragraph") 361 && s.getName().getVal().equals("Default") ) { 362 log.info("Style with name " + s.getName().getVal() + ", id '" + s.getStyleId() + "' is default " + s.getType() + " style"); 363 defaultParagraphStyle=s; 364 break; 365 } 366 } 367 } 368 return defaultParagraphStyle; 369 } 370 private Style getDefaultStyle(String type) { 371 372 for ( org.docx4j.wml.Style s : this.jaxbElement.getStyle() ) { 373 if( s.isDefault() && s.getType().equals(type)) { 374 log.info("Style with name " + s.getName().getVal() + ", id '" + s.getStyleId() + "' is default " + s.getType() + " style"); 375 return s; 376 } 377 } 378 return null; 312 379 } 313 380 … … 333 400 + "</w:pPrDefault>" 334 401 + "</w:docDefaults>"; 402 335 403 336 404
Note: See TracChangeset
for help on using the changeset viewer.
