Example #1
0
  public static DocumentFragment createBlockForR(
      SvgConversionContext context, NodeIterator rPrNodeIt, NodeIterator childResults) {

    DocumentFragment docfrag = null;
    Document d = null;
    Element span = null;

    try {

      // Create a DOM builder and parse the fragment
      d = XmlUtils.getNewDocumentBuilder().newDocument();

      span = d.createElement("span");
      d.appendChild(span);

      CTTextCharacterProperties textCharProps =
          (CTTextCharacterProperties)
              nodeToObjectModel(rPrNodeIt.nextNode(), CTTextCharacterProperties.class);

      RPr rPr = TextStyles.getWmlRPr(textCharProps);

      // Does our rPr contain anything else?
      StringBuilder inlineStyle = new StringBuilder();
      HtmlCssHelper.createCss(context.getPmlPackage(), rPr, inlineStyle);
      if (!inlineStyle.toString().equals("")) {
        span.setAttribute("style", inlineStyle.toString());
      }

      Node n = childResults.nextNode();
      XmlUtils.treeCopy(n, span);

    } catch (Exception e) {
      log.error(e.getMessage(), e);
      // If something went wrong with the formatting,
      // still try to display the text!
      Node n = childResults.nextNode();
      XmlUtils.treeCopy(n, span);
    }

    // Machinery
    docfrag = d.createDocumentFragment();
    docfrag.appendChild(d.getDocumentElement());
    return docfrag;
  }
Example #2
0
  public static DocumentFragment createBlockForP(
      SvgConversionContext context,
      String lvl,
      String cNvPrName,
      String phType,
      NodeIterator childResults,
      NodeIterator lvlNpPr) {

    StyleTree styleTree = null;
    try {
      styleTree = context.getPmlPackage().getStyleTree();
    } catch (InvalidFormatException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }

    log.debug("lvl:" + lvl);
    int level;
    if (lvl.equals("NaN")) {
      level = 1;
    } else {
      level = Integer.parseInt(lvl);
    }
    String pStyleVal;

    System.out.println("cNvPrName: " + cNvPrName + "; " + "phType: " + phType);

    if (cNvPrName.toLowerCase().indexOf("subtitle") > -1
        || phType.toLowerCase().indexOf("subtitle") > -1) {
      // Subtitle on first page in default layout is styled as a Body.
      pStyleVal = "Lvl" + level + "Master" + context.getResolvedLayout().getMasterNumber() + "Body";
    } else if (cNvPrName.toLowerCase().indexOf("title") > -1
        || phType.toLowerCase().indexOf("title") > -1) {
      pStyleVal =
          "Lvl" + level + "Master" + context.getResolvedLayout().getMasterNumber() + "Title";
    } else {
      // eg cNvPrName: TextBox 2; phType:
      pStyleVal =
          "Lvl" + level + "Master" + context.getResolvedLayout().getMasterNumber() + "Other";
    }
    System.out.println("--> " + pStyleVal);

    try {

      // Create a DOM builder and parse the fragment
      Document document = XmlUtils.getNewDocumentBuilder().newDocument();

      // log.info("Document: " + document.getClass().getName() );

      Node xhtmlP = document.createElement("p");
      document.appendChild(xhtmlP);

      // Set @class
      log.debug(pStyleVal);
      Tree<AugmentedStyle> pTree = styleTree.getParagraphStylesTree();
      org.docx4j.model.styles.Node<AugmentedStyle> asn = pTree.get(pStyleVal);
      ((Element) xhtmlP).setAttribute("class", StyleTree.getHtmlClassAttributeValue(pTree, asn));

      StringBuilder inlineStyle = new StringBuilder();
      // Do we have CTTextParagraphProperties
      // <a:lvl?pPr>
      // Convert it to a WordML pPr
      CTTextParagraphProperties lvlPPr = unmarshalFormatting(lvlNpPr);
      if (lvlPPr != null) {

        log.debug("We have lvlPPr");
        log.debug(
            XmlUtils.marshaltoString(
                lvlPPr,
                true,
                true,
                Context.jcPML,
                "FIXME",
                "lvl1pPr",
                CTTextParagraphProperties.class));
        PPr pPr = TextStyles.getWmlPPr(lvlPPr);
        if (pPr != null) {
          HtmlCssHelper.createCss(context.getPmlPackage(), pPr, inlineStyle, false, false);
        }
        // TODO RPR
      }
      // Without this, top-margin is too large in Webkit (Midor).
      // Not tested elsewhere...
      inlineStyle.append("margin-left:3px; margin-top:3px;");

      if (!inlineStyle.toString().equals("")) {
        ((Element) xhtmlP).setAttribute("style", inlineStyle.toString());
      }

      // Our fo:block wraps whatever result tree fragment
      // our style sheet produced when it applied-templates
      // to the child nodes
      // init
      Node n = childResults.nextNode();
      do {

        // getNumberXmlNode creates a span node, which is empty
        // if there is no numbering.
        // Let's get rid of any such <span/>.

        // What we actually get is a document node
        if (n.getNodeType() == Node.DOCUMENT_NODE) {
          log.debug("handling DOCUMENT_NODE");
          // Do just enough of the handling here
          NodeList nodes = n.getChildNodes();
          if (nodes != null) {
            for (int i = 0; i < nodes.getLength(); i++) {

              if (((Node) nodes.item(i)).getLocalName().equals("span")
                  && !((Node) nodes.item(i)).hasChildNodes()) {
                // ignore
                log.debug(".. ignoring <span/> ");
              } else {
                XmlUtils.treeCopy((Node) nodes.item(i), xhtmlP);
              }
            }
          }
        } else {

          //					log.info("Node we are importing: " + n.getClass().getName() );
          //					foBlockElement.appendChild(
          //							document.importNode(n, true) );
          /*
           * Node we'd like to import is of type org.apache.xml.dtm.ref.DTMNodeProxy
           * which causes
           * org.w3c.dom.DOMException: NOT_SUPPORTED_ERR: The implementation does not support the requested type of object or operation.
           *
           * See http://osdir.com/ml/text.xml.xerces-j.devel/2004-04/msg00066.html
           *
           * So instead of importNode, use
           */
          XmlUtils.treeCopy(n, xhtmlP);
        }
        // next
        n = childResults.nextNode();

      } while (n != null);

      DocumentFragment docfrag = document.createDocumentFragment();
      docfrag.appendChild(document.getDocumentElement());

      return docfrag;

    } catch (Exception e) {
      log.error(e.getMessage(), e);
    }

    return null;
  }