List of usage examples for org.jdom2 JDOMFactory element
public Element element(String name);
Element
in no org.jdom2.Namespace
. From source file:org.kdp.word.Parser.java
License:Apache License
/** * Parse the input file and return a well formed document *///w w w. j a va 2 s . c o m private Document parseHTML(final JDOMFactory factory, final File infile) throws SAXException, IOException { final AtomicReference<Document> docref = new AtomicReference<>(); String charset = getProperty(PROPERTY_INPUT_CHARSET); charset = charset != null ? charset : "UTF-8"; InputStream inputStream = new FileInputStream(infile); Reader reader = new InputStreamReader(inputStream, charset); InputSource source = new InputSource(reader); source.setEncoding(charset); SAXParserImpl.newInstance(null).parse(source, new DefaultHandler() { Stack<Element> stack = new Stack<>(); public void startElement(String uri, String localName, String name, Attributes inatts) { Element element = factory.element(localName.toLowerCase()); List<Attribute> outatts = element.getAttributes(); for (int i = 0; i < inatts.getLength(); i++) { String att = inatts.getLocalName(i); String val = inatts.getValue(i); outatts.add(factory.attribute(att, val)); } if (docref.get() == null) { docref.set(factory.document(element)); } else { Element parent = stack.peek(); parent.getChildren().add(element); } stack.push(element); } public void endElement(String uri, String localName, String name) { stack.pop(); } public void characters(char[] arr, int start, int length) { StringBuilder sb = new StringBuilder(length); for (int i = 0; i < length; i++) { char ch = arr[start + i]; sb.append(ch); } Element parent = stack.peek(); parent.addContent(sb.toString()); } }); return docref.get(); }
From source file:org.kdp.word.transformer.FootnodeTransformer.java
License:Apache License
@Override public void transform(Context context) { Map<String, Footnode> footnodes = new LinkedHashMap<>(); Element root = context.getSourceRoot(); for (Element el : root.getChildren()) { findFootnodes(context, el, footnodes); }//w w w.j a v a 2 s.c om JDOMFactory factory = context.getJDOMFactory(); for (Footnode fn : footnodes.values()) { // Footnode Ref Element fnref = fn.fnref; String text = getFootnodeText(fnref); Parent parent = fnref.getParent(); int index = parent.indexOf(fnref); parent.removeContent(index); Element span = factory.element("span"); span.setAttribute("class", "MsoFootnoteReference"); Element a = fnref.clone(); a.removeContent(); a.setText(text); //a.setAttribute("type", "noteref", OPFTransformer.NS_OPF); span.addContent(a); parent.addContent(index, span); /* Footnode Text Element fntxt = fn.fntxt; Element p = findMsoFootnoteText(fntxt); text = getFootnodeText(p); p.getAttributes().clear(); p.removeContent(); p.setText(text); String divid = fn.id.substring(1); Element div = JDOMUtils.findElement(root, "div", "id", divid); IllegalStateAssertion.assertSame(p.getParentElement(), div, "Unexpected parent: " + div); Parent divparent = div.getParent(); Element aside = factory.element("aside"); aside.setAttribute("type", "footnote", OPFTransformer.NS_OPF); aside.setAttribute("id", fn.id); index = divparent.indexOf(div); divparent.removeContent(div); aside.addContent(p.clone()); divparent.addContent(index, aside); */ } }
From source file:org.kdp.word.transformer.ListParagraphTransformer.java
License:Apache License
private void processListItemBatch(Context context, Element parent, List<Element> listItems) { boolean ordered = false; for (Element el : listItems) { removeNestedSpanElements(el);// w w w. ja v a 2 s .com normalizeListItemText(el); ordered = processItemMarker(el); el.getAttributes().clear(); } Element firstItem = listItems.get(0); int index = parent.indexOf(firstItem); for (Element el : listItems) { parent.removeContent(el); } JDOMFactory factory = context.getJDOMFactory(); Element ul = factory.element(ordered ? "ol" : "ul"); for (Element el : listItems) { Element li = factory.element("li"); li.setAttribute("class", "MsoListParagraph"); for (Content co : el.getContent()) { li.addContent(co.clone()); } ul.addContent(li); } parent.addContent(index, ul); }
From source file:org.kdp.word.transformer.OPFTransformer.java
License:Apache License
private void processManifest(Context context, Element opf) { Element manifest = JDOMUtils.findElement(opf, "manifest"); JDOMFactory factory = context.getJDOMFactory(); Parser parser = context.getParser(); // Cover Image String imgsrc = parser.getProperty(Parser.PROPERTY_OPF_MANIFEST_COVER_IMAGE); IllegalStateAssertion.assertNotNull(imgsrc, "Cannot obtain property: " + Parser.PROPERTY_OPF_MANIFEST_COVER_IMAGE); String imgtype = parser.getProperty(Parser.PROPERTY_OPF_MANIFEST_COVER_IMAGE_TYPE); IllegalStateAssertion.assertNotNull(imgsrc, "Cannot obtain property: " + Parser.PROPERTY_OPF_MANIFEST_COVER_IMAGE_TYPE); Element item = factory.element("item"); item.getAttributes().add(factory.attribute("id", "CoverImage")); item.getAttributes().add(factory.attribute("href", imgsrc)); item.getAttributes().add(factory.attribute("properties", "cover-image")); item.getAttributes().add(factory.attribute("media-type", imgtype)); manifest.getChildren().add(item);//from w w w .j a v a 2s. co m // Write sections Sections sections = context.getAttribute(Sections.class); if (sections != null) { for (Section section : sections) { item = factory.element("item"); item.setAttribute("id", section.name); Path targetPath = IOUtils.bookRelative(context, section.target); item.setAttribute("href", targetPath.toString()); if (section.isnav) { item.setAttribute("properties", "nav"); } item.setAttribute("media-type", "application/xhtml+xml"); manifest.getChildren().add(item); } } // Write Content item = factory.element("item"); item.setAttribute("id", "Content"); Path targetPath = IOUtils.bookRelative(context, context.getTarget()); item.setAttribute("href", targetPath.toString()); item.setAttribute("media-type", "application/xhtml+xml"); manifest.getChildren().add(item); }
From source file:org.kdp.word.transformer.OPFTransformer.java
License:Apache License
private void processSpine(Context context, Element opf) { Element spine = JDOMUtils.findElement(opf, "spine"); JDOMFactory factory = context.getJDOMFactory(); // Cover Image Element itemref = factory.element("itemref"); itemref.setAttribute("idref", "CoverImage"); spine.getChildren().add(itemref);/* ww w . j a va2s . c o m*/ // Write sections Sections sections = context.getAttribute(Sections.class); if (sections != null) { for (Section section : sections) { itemref = factory.element("itemref"); itemref.setAttribute("idref", section.name); spine.getChildren().add(itemref); } } // Content itemref = factory.element("itemref"); itemref.setAttribute("idref", "Content"); spine.getChildren().add(itemref); }
From source file:org.kdp.word.transformer.StyleTransformer.java
License:Apache License
@Override public void transform(Context context) { Parser parser = context.getParser(); Options options = context.getOptions(); Path cssPath = options.getExternalCSS(); if (cssPath == null) return;/*from w w w . java 2 s. co m*/ // Parse the external styles replacements = parseStyleReplacements(context); parseExternalStyles(context, cssPath); String wltoks = parser.getProperty(Parser.PROPERTY_STYLE_REPLACE_WHITELIST); if (wltoks != null) { for (String tok : wltoks.split(",")) { whitelist.add(tok.trim()); } } // Remove existing style definitions Element root = context.getSourceRoot(); Element elStyle = JDOMUtils.findElement(root, "style"); if (elStyle != null) { elStyle.getParentElement().removeContent(elStyle); } // Copy the CSS to the book dir Path cssName = cssPath.getFileName(); try { Path bookDir = options.getBookDir(); Path cssBook = bookDir.resolve(cssName); bookDir.toFile().mkdirs(); Files.copy(cssPath, cssBook, StandardCopyOption.REPLACE_EXISTING); } catch (IOException ex) { throw new IllegalStateException(ex); } // Add reference to external styles Element elHead = JDOMUtils.findElement(root, "head"); if (elHead != null) { JDOMFactory factory = context.getJDOMFactory(); Element elLink = factory.element("link"); elLink.setAttribute("rel", "stylesheet"); elLink.setAttribute("type", "text/css"); elLink.setAttribute("href", cssName.toString()); elHead.addContent(elLink); transformStyles(context, root); } }
From source file:org.kdp.word.transformer.TOCTransformer.java
License:Apache License
@Override public void transform(Context context) { JDOMFactory factory = context.getJDOMFactory(); Element root = context.getSourceRoot(); for (Element el : root.getChildren()) { transformInternal(context, el);//from ww w . j av a 2 s .c o m } Element first = JDOMUtils.findElement(root, "p", "class", "MsoToc1"); if (first != null) { Element parent = first.getParentElement(); List<Element> children = parent.getChildren(); // Add the nav element Element nav = factory.element("nav"); nav.getAttributes().add(factory.attribute("type", "toc", OPFTransformer.NS_OPF)); int index = children.indexOf(first); children.add(index, nav); // Add the ol element Element ol = factory.element("ol"); ol.setAttribute("class", "Toc"); nav.getChildren().add(ol); Iterator<Element> itel = children.iterator(); while (itel.hasNext()) { Element el = itel.next(); if (JDOMUtils.isElement(el, "p", "class", "MsoToc1")) { Element li = factory.element("li"); li.getAttributes().add(factory.attribute("class", "MsoToc1")); li.addContent(el.cloneContent()); ol.getChildren().add(li); itel.remove(); } } } }
From source file:org.kdp.word.transformer.TOCTransformer.java
License:Apache License
private void transformInternal(Context context, Element el) { JDOMFactory factory = context.getJDOMFactory(); Element root = context.getSourceRoot(); if (JDOMUtils.isElement(el, "p", "class", "MsoToc1")) { Element tocel = getFirstTextElement(el); String tocname = tocel.getText(); int dotidx = tocname.indexOf("..."); if (dotidx > 0) { tocname = tocname.substring(0, dotidx).trim(); }//w w w .ja v a 2 s .c o m String aname = findAnchorName(root, tocname); IllegalStateAssertion.assertNotNull(aname, "Cannot find anchor for: " + tocname); Element anchor = factory.element("a"); Path targetPath = IOUtils.bookRelative(context, context.getTarget()); anchor.getAttributes().add(factory.attribute("href", targetPath + "#" + aname)); anchor.setText(tocname); el.getChildren().clear(); el.setText(null); el.getChildren().add(anchor); } else { for (Element ch : el.getChildren()) { transformInternal(context, ch); } } }