List of usage examples for org.jdom2 Document getContentSize
@Override public int getContentSize()
From source file:de.danielluedecke.zettelkasten.database.Bookmarks.java
License:Open Source License
/** * /*from ww w . ja va 2s. co m*/ * @param dataObj * @param newbookmarks */ public void appendBookmarks(Daten dataObj, Document newbookmarks) { // create a list of all elements from the given xml file try { List<?> elementList = newbookmarks.getRootElement().getContent(); try { // iterate all importet bookmakrs for (int cnt = 0; cnt < newbookmarks.getContentSize(); cnt++) { // retrieve each single bookmark-element Element b = (Element) elementList.get(cnt); // get bookmark-id (i.e. unique entry-ID) String id = b.getAttributeValue("id"); // check for valid value if (id != null && !id.isEmpty()) { // find entry number from ID int index = dataObj.findZettelFromID(id); // check for valid return parameter if (index != -1) { // we now have the entry's number. now retrieve // bookmark-category String cat = b.getAttributeValue("cat"); // check for valid value if (cat != null && !cat.isEmpty()) { // retrieve possible comment String comment = b.getText(); // and add new importet bookmark addBookmark(index, cat, comment); } } } } } catch (IndexOutOfBoundsException e) { } } catch (IllegalStateException e) { Constants.zknlogger.log(Level.WARNING, e.getLocalizedMessage()); } }
From source file:org.apache.maven.io.util.AbstractJDOMWriter.java
License:Apache License
public final void write(final T source, final Document document, final Writer writer, final Format jdomFormat, final DocumentModifier modifier) throws java.io.IOException { if (modifier != null) { modifier.preProcess(document);/*from w w w. java 2 s . co m*/ } update(source, new IndentationCounter(0), document.getRootElement()); if (modifier != null) { modifier.postProcess(document); } // Override XMLOutputter to correct initial comment trailing newlines. final XMLOutputter outputter = new XMLOutputter(new AbstractXMLOutputProcessor() { /** * This will handle printing of a {@link Document}. * * @param out <code>Writer</code> to use. * @param fstack the FormatStack * @param nstack the NamespaceStack * @param doc <code>Document</code> to write. * @throws IOException if the destination Writer fails */ @Override protected void printDocument(Writer out, FormatStack fstack, NamespaceStack nstack, Document doc) throws IOException { // If there is no root element then we cannot use the normal ways to // access the ContentList because Document throws an exception. // so we hack it and just access it by index. List<Content> list = doc.hasRootElement() ? doc.getContent() : new ArrayList<Content>(doc.getContentSize()); if (list.isEmpty()) { final int sz = doc.getContentSize(); for (int i = 0; i < sz; i++) { list.add(doc.getContent(i)); } } printDeclaration(out, fstack); Walker walker = buildWalker(fstack, list, true); if (walker.hasNext()) { while (walker.hasNext()) { final Content c = walker.next(); // we do not ignore Text-like things in the Document. // the walker creates the indenting for us. if (c == null) { // but, what we do is ensure it is all whitespace, and not CDATA final String padding = walker.text(); if (padding != null && Verifier.isAllXMLWhitespace(padding) && !walker.isCDATA()) { // we do not use the escaping or text* method because this // content is outside of the root element, and thus is not // strict text. write(out, padding); } } else { switch (c.getCType()) { case Comment: printComment(out, fstack, (Comment) c); // This modification we have made to the overridden method in order // to correct newline declarations. write(out, fstack.getLineSeparator()); break; case DocType: printDocType(out, fstack, (DocType) c); break; case Element: printElement(out, fstack, nstack, (Element) c); if (walker.hasNext()) { // This modification we have made to the overridden method in order // to correct newline declarations. write(out, fstack.getLineSeparator()); } break; case ProcessingInstruction: printProcessingInstruction(out, fstack, (ProcessingInstruction) c); break; case Text: final String padding = ((Text) c).getText(); if (padding != null && Verifier.isAllXMLWhitespace(padding)) { // we do not use the escaping or text* method because this // content is outside of the root element, and thus is not // strict text. write(out, padding); } default: // do nothing. } } } if (fstack.getLineSeparator() != null) { write(out, fstack.getLineSeparator()); } } } }); outputter.setFormat(jdomFormat); outputter.output(document, writer); }