List of usage examples for javax.swing.text ElementIterator ElementIterator
public ElementIterator(Element root)
From source file:EditorPaneExample16.java
public TreeNode buildHeadingTree(Document doc) { String title = (String) doc.getProperty(Document.TitleProperty); if (title == null) { title = "[No title]"; }/*from w ww .j a v a 2 s . c o m*/ Heading rootHeading = new Heading(title, 0, 0); DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(rootHeading); DefaultMutableTreeNode lastNode[] = new DefaultMutableTreeNode[7]; int lastLevel = 0; lastNode[lastLevel] = rootNode; if (doc instanceof HTMLDocument) { Element elem = doc.getDefaultRootElement(); ElementIterator iterator = new ElementIterator(elem); Heading heading; while ((heading = getNextHeading(doc, iterator)) != null) { // Add the node to the tree DefaultMutableTreeNode hNode = new DefaultMutableTreeNode(heading); int level = heading.getLevel(); if (level > lastLevel) { for (int i = lastLevel + 1; i < level; i++) { lastNode[i] = null; } lastNode[lastLevel].add(hNode); } else { int prevLevel = level - 1; while (prevLevel >= 0) { if (lastNode[prevLevel] != null) { break; } lastNode[prevLevel] = null; prevLevel--; } lastNode[prevLevel].add(hNode); } lastNode[level] = hNode; lastLevel = level; } } return rootNode; }
From source file:org.deegree.tools.metadata.InspireValidator.java
/** * parse INSPIRE metadata validator response and print out result onto the console * //from w w w. j a va2s .c o m * @param response * @throws IOException * @throws IllegalStateException */ private void parseServiceResponse(HttpResponse response) throws Exception { String s = FileUtils.readTextFile(((BasicHttpResponse) response).getEntity().getContent()).toString(); if (response.getStatusLine().getStatusCode() != 200) { outputWriter.println(s); outputWriter.println(); return; } s = "<html><head></head><body>" + s + "</body></html>"; BufferedReader br = new BufferedReader(new StringReader(s)); HTMLEditorKit htmlKit = new HTMLEditorKit(); HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument(); HTMLEditorKit.Parser parser = new ParserDelegator(); HTMLEditorKit.ParserCallback callback = htmlDoc.getReader(0); parser.parse(br, callback, true); // Parse ElementIterator iterator = new ElementIterator(htmlDoc); Element element; while ((element = iterator.next()) != null) { AttributeSet attributes = element.getAttributes(); Object name = attributes.getAttribute(StyleConstants.NameAttribute); if ((name instanceof HTML.Tag) && ((name == HTML.Tag.IMPLIED))) { // Build up content text as it may be within multiple elements StringBuffer text = new StringBuffer(); int count = element.getElementCount(); for (int i = 0; i < count; i++) { Element child = element.getElement(i); AttributeSet childAttributes = child.getAttributes(); if (childAttributes.getAttribute(StyleConstants.NameAttribute) == HTML.Tag.CONTENT) { int startOffset = child.getStartOffset(); int endOffset = child.getEndOffset(); int length = endOffset - startOffset; text.append(htmlDoc.getText(startOffset, length)); } } outputWriter.println(text.toString()); } } outputWriter.println("---------------------------------------------------------------------"); outputWriter.println(); }