List of usage examples for org.jdom2 JDOMFactory document
public Document document(Element rootElement);
Document
, with the supplied org.jdom2.Element
as the root element, and no org.jdom2.DocType
declaration. 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 om*/ 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.SectionTransformer.java
License:Apache License
@Override public void transform(Context context) { JDOMFactory factory = context.getJDOMFactory(); Sections sections = new Sections(); context.putAttribute(Sections.class, sections); Element root = context.getSourceRoot(); for (Element el : root.getChildren()) { findWordSections(context, sections, el); }//from w w w .j av a2s . c om boolean navfound = false; Iterator<Section> itsec = sections.iterator(); while (itsec.hasNext()) { Section section = itsec.next(); if (navfound) { itsec.remove(); continue; } navfound = section.isnav; // Remove the section from the original document Element element = section.element; Parent parent = element.getParent(); parent.removeContent(element); // Build the target document Element rootClone = root.clone(); Element bodyClone = JDOMUtils.findElement(rootClone, "body"); bodyClone.removeContent(); bodyClone.addContent(element.clone()); // Write the section document Document doc = factory.document(rootClone); File outfile = section.target.toFile(); try { outfile.getParentFile().mkdirs(); FileOutputStream fos = new FileOutputStream(outfile); IOUtils.writeDocument(context, doc, fos); fos.close(); } catch (IOException ex) { throw new IllegalStateException(ex); } } }
From source file:org.mycore.common.content.transformer.MCRXSL2XMLTransformer.java
License:Open Source License
private Document getDocument(JDOMResult result) { Document resultDoc = result.getDocument(); if (resultDoc == null) { //Sometimes a transformation produces whitespace strings //JDOM would produce a empty document if it detects those //So we remove them, if they exists. List<Content> transformResult = result.getResult(); int origSize = transformResult.size(); Iterator<Content> iterator = transformResult.iterator(); while (iterator.hasNext()) { Content content = iterator.next(); if (content instanceof Text) { String trimmedText = ((Text) content).getTextTrim(); if (trimmedText.length() == 0) { iterator.remove();// w ww . ja va 2 s . c o m } } } if (transformResult.size() < origSize) { JDOMFactory f = result.getFactory(); if (f == null) { f = new DefaultJDOMFactory(); } resultDoc = f.document(null); resultDoc.setContent(transformResult); } } return resultDoc; }