Example #1
0
  public StyleTree getStyleTree(boolean refresh) {
    // refresh is post 2.7.1

    if (refresh || styleTree == null) {

      log.info("Preparing StyleTree");

      try {
        getStyleDefinitionsPart().createVirtualStylesForDocDefaults();
      } catch (Docx4JException e) {
        // Shouldn't happen, so catch here
        log.error(e.getMessage(), e);
      }

      //			// Get these first, so we can be sure they are defined...
      //			Style defaultParagraphStyle = getStyleDefinitionsPart().getDefaultParagraphStyle();
      //			Style defaultCharacterStyle = getStyleDefinitionsPart().getDefaultCharacterStyle();

      // Styles defined in StyleDefinitionsPart
      Map<String, Style> allStyles = new HashMap<String, Style>();
      Styles styles = getStyleDefinitionsPart().getJaxbElement();
      for (org.docx4j.wml.Style s : styles.getStyle()) {
        allStyles.put(s.getStyleId(), s);
        // log.debug("live style: " + s.getStyleId() );
      }
      styleTree = new StyleTree(getStylesInUse(), allStyles);
    }
    return styleTree;
  }
Example #2
0
  /**
   * Determine the font used in this style, using the inheritance rules.
   *
   * @return the font name, or null if there is no rFonts element in any style in the style
   *     inheritance hierarchy (ie this method does not look up
   *     styles/docDefaults/rPrDefault/rPr/rFonts or from there, the theme part - see getDefaultFont
   *     to do that).
   * @see getDefaultFont
   */
  public static String getFontnameFromStyle(
      StyleDefinitionsPart styleDefinitionsPart, ThemePart themePart, org.docx4j.wml.Style style) {

    org.docx4j.wml.Styles styles = (org.docx4j.wml.Styles) styleDefinitionsPart.getJaxbElement();
    //		org.docx4j.wml.Styles styles =
    // (org.docx4j.wml.Styles)this.getStyleDefinitionsPart().getJaxbElement();

    // It is convenient to have a HashMap of styles
    Map stylesDefined = new java.util.HashMap();
    for (Iterator iter = styles.getStyle().iterator(); iter.hasNext(); ) {
      org.docx4j.wml.Style s = (org.docx4j.wml.Style) iter.next();
      stylesDefined.put(s.getStyleId(), s);
    }

    return getFontnameFromStyle(stylesDefined, themePart, style);
  }
Example #3
0
  private boolean activateStyle(org.docx4j.wml.Style s, boolean replace) {

    if (liveStyles.get(s.getStyleId()) != null) {
      // Its already live

      if (!replace) {
        return false;
      }

      // Remove existing entry
      styles.getStyle().remove(liveStyles.get(s.getStyleId()));
    }

    // Add it
    // .. to the JAXB object
    styles.getStyle().add(s);
    // .. here
    liveStyles.put(s.getStyleId(), s);

    // Now, recursively check that what it is based on is present
    boolean result1;
    if (s.getBasedOn() != null) {
      String basedOn = s.getBasedOn().getVal();
      result1 = activateStyle(basedOn);

    } else if (s.getStyleId().equals(defaultParagraphStyleId)
        || s.getStyleId().equals(defaultCharacterStyleId)) {
      // stop condition
      result1 = true;
    } else {

      log.error("Expected " + s.getStyleId() + " to have <w:basedOn ??");
      // Not properly activated
      result1 = false;
    }

    // Also add the linked style, if any
    // .. Word might expect it to be there
    boolean result2 = true;
    if (s.getLink() != null) {

      org.docx4j.wml.Style.Link link = s.getLink();
      result2 = activateStyle(link.getVal());
    }

    return (result1 & result2);
  }
Example #4
0
  private void initialiseLiveStyles() {

    log.debug("initialiseLiveStyles()");
    liveStyles = new java.util.HashMap<String, org.docx4j.wml.Style>();

    for (org.docx4j.wml.Style s : styles.getStyle()) {
      liveStyles.put(s.getStyleId(), s);
      log.debug("live style: " + s.getStyleId());
    }
  }
Example #5
0
  private void init() throws Docx4JException {

    // Make sure we have a styles definitions part
    try {
      if (styleDefinitionsPart == null) {
        styleDefinitionsPart = new StyleDefinitionsPart();
        styleDefinitionsPart.unmarshalDefaultStyles();

        // For this case, should we provide a way
        // to add this new part to the package?

      }
    } catch (Exception e) {
      throw new Docx4JException("Couldn't create default StyleDefinitionsPart", e);
    }

    defaultParagraphStyleId = this.styleDefinitionsPart.getDefaultParagraphStyle().getStyleId();
    defaultCharacterStyleId = this.styleDefinitionsPart.getDefaultCharacterStyle().getStyleId();

    // Initialise styles
    styles = (org.docx4j.wml.Styles) styleDefinitionsPart.getJaxbElement();
    initialiseLiveStyles();

    // Initialise docDefaults
    docDefaults = styles.getDocDefaults();

    if (docDefaults == null) {
      // The only way this can happen is if the
      // styles definition part is missing the docDefaults element
      // (these are present in docs created from Word, and
      // in our default styles, so maybe the user created it using
      // some 3rd party program?)
      try {
        docDefaults =
            (DocDefaults) XmlUtils.unmarshalString(StyleDefinitionsPart.docDefaultsString);
      } catch (JAXBException e) {
        throw new Docx4JException(
            "Problem unmarshalling " + StyleDefinitionsPart.docDefaultsString, e);
      }
    }

    // Setup documentDefaultPPr
    if (docDefaults.getPPrDefault() == null) {
      try {
        documentDefaultPPr = (PPr) XmlUtils.unmarshalString(StyleDefinitionsPart.pPrDefaultsString);
      } catch (JAXBException e) {
        throw new Docx4JException(
            "Problem unmarshalling " + StyleDefinitionsPart.pPrDefaultsString, e);
      }
    } else {
      documentDefaultPPr = docDefaults.getPPrDefault().getPPr();
    }

    // Setup documentDefaultRPr
    if (docDefaults.getRPrDefault() == null) {
      try {
        documentDefaultRPr = (RPr) XmlUtils.unmarshalString(StyleDefinitionsPart.rPrDefaultsString);
      } catch (JAXBException e) {
        throw new Docx4JException(
            "Problem unmarshalling " + StyleDefinitionsPart.rPrDefaultsString, e);
      }
    } else {
      documentDefaultRPr = docDefaults.getRPrDefault().getRPr();
    }

    addNormalToResolvedStylePPrComponent();
    addDefaultParagraphFontToResolvedStyleRPrComponent();
  }