Ignore:
Timestamp:
02/27/10 06:52:46 (2 years ago)
Author:
jharrop
Message:

Moved jcPML to org.pptx4j.jaxb.Context

Don't assume default paragraph,character styles are Normal,Default.. respectively.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/StyleDefinitionsPart.java

    r976 r1096  
    2323import java.io.IOException; 
    2424 
    25 import javax.xml.bind.JAXBContext; 
    26 import javax.xml.bind.JAXBException; 
    27 import javax.xml.bind.Unmarshaller; 
    28  
    2925import org.apache.log4j.Logger; 
    3026import org.docx4j.XmlUtils; 
     
    3228import org.docx4j.openpackaging.exceptions.Docx4JException; 
    3329import org.docx4j.openpackaging.exceptions.InvalidFormatException; 
     30import org.docx4j.openpackaging.packages.WordprocessingMLPackage; 
    3431import org.docx4j.openpackaging.parts.JaxbXmlPart; 
    3532import org.docx4j.openpackaging.parts.PartName; 
     
    4239import org.docx4j.wml.Style.BasedOn; 
    4340 
     41import javax.xml.bind.JAXBContext; 
     42import javax.xml.bind.JAXBException; 
     43import javax.xml.bind.Unmarshaller; 
     44 
    4445 
    4546public final class StyleDefinitionsPart extends JaxbXmlPart<Styles> { 
     
    4748        private static Logger log = Logger.getLogger(StyleDefinitionsPart.class);                
    4849         
    49  
    5050        public StyleDefinitionsPart(PartName partName) throws InvalidFormatException { 
    5151                super(partName); 
     
    292292                 
    293293                // 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                 
    295307                BasedOn based = Context.getWmlObjectFactory().createStyleBasedOn(); 
    296308                based.setVal(ROOT_NAME);                 
     
    310322                } 
    311323        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; 
    312379    } 
    313380     
     
    333400            + "</w:pPrDefault>" 
    334401          + "</w:docDefaults>"; 
     402         
    335403     
    336404     
Note: See TracChangeset for help on using the changeset viewer.