List of usage examples for javax.swing.text Element getElement
public Element getElement(int index);
From source file:Main.java
public static void main(String[] argv) throws Exception { JTextPane textPane = new JTextPane(); Element section = textPane.getDocument().getDefaultRootElement(); int paraCount = section.getElementCount(); for (int i = 0; i < paraCount; i++) { Element e = section.getElement(i); int rangeStart = e.getStartOffset(); int rangeEnd = e.getEndOffset(); String para = textPane.getText(rangeStart, rangeEnd - rangeStart); System.out.println(para); }/*w ww. ja va 2s .co m*/ }
From source file:Main.java
public static void main(String[] argv) throws Exception { JTextArea textArea = new JTextArea("word1 word2\nword3\nword4"); Element paragraph = textArea.getDocument().getDefaultRootElement(); int contentCount = paragraph.getElementCount(); for (int i = 0; i < contentCount; i++) { Element e = paragraph.getElement(i); int rangeStart = e.getStartOffset(); int rangeEnd = e.getEndOffset(); String line = textArea.getText(rangeStart, rangeEnd - rangeStart); System.out.println(line); }/*from w w w . j a v a 2s .co m*/ }
From source file:MainClass.java
public static void main(String args[]) throws Exception { URL url = new URL("http://www.java2s.com"); URLConnection connection = url.openConnection(); InputStream is = connection.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); 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);//from www . java2 s . co m 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.H1)) { 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)); } } System.out.println(name + ": " + text.toString()); } } }
From source file:MainClass.java
public static void main(String args[]) throws Exception { URL url = new URL("http://www.google.com"); URLConnection connection = url.openConnection(); InputStream is = connection.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); 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);/*from w w w .ja v a 2 s .c om*/ Element element; ElementIterator iterator = new ElementIterator(htmlDoc); while ((element = iterator.next()) != null) { AttributeSet attributes = element.getAttributes(); Object name = attributes.getAttribute(StyleConstants.NameAttribute); if ((name instanceof HTML.Tag) && (name == HTML.Tag.H1 || name == HTML.Tag.H2 || name == HTML.Tag.P)) { // Build up content text as it may be within multiple elements 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; System.out.println(htmlDoc.getText(startOffset, length)); } } } } }
From source file:ElementIteratorExample.java
public static void main(String args[]) throws Exception { if (args.length != 1) { System.err.println("Usage: java ElementIteratorExample input-URL"); }/*from w ww. j a va2 s. com*/ // Load HTML file synchronously URL url = new URL(args[0]); URLConnection connection = url.openConnection(); InputStream is = connection.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); 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.H1) || (name == HTML.Tag.H2) || (name == HTML.Tag.H3))) { // 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)); } } System.out.println(name + ": " + text.toString()); } } System.exit(0); }
From source file:Main.java
private static final Element getLineElem(Document d, int offs) { Element map = d.getDefaultRootElement(); int index = map.getElementIndex(offs); Element elem = map.getElement(index); if ((offs >= elem.getStartOffset()) && (offs < elem.getEndOffset())) { return elem; }//w ww .j a v a2 s . c o m return null; }
From source file:com.hp.alm.ali.idea.ui.editor.field.HTMLAreaField.java
private static String checkElements(Element element) { if (!allowedElements.contains(element.getName())) { return element.getName(); }//w w w. ja v a 2s . c o m for (int i = 0; i < element.getElementCount(); i++) { String failed; if ((failed = checkElements(element.getElement(i))) != null) { return failed; } } return null; }
From source file:Main.java
private String addWhiteSpace(FilterBypass fb, int offset, String text) throws BadLocationException { Document doc = fb.getDocument(); Element root = doc.getDefaultRootElement(); int line = root.getElementIndex(offset); int i = root.getElement(line).getStartOffset(); StringBuilder whiteSpace = new StringBuilder(text); while (true) { String temp = doc.getText(i, 1); if (temp.equals(" ") || temp.equals("\t")) { System.out.println("added"); whiteSpace.append(temp);//from w w w. ja v a 2 s . co m i++; } else { break; } } return whiteSpace.toString(); }
From source file:Main.java
public Main() { setSize(400, 400);//from ww w .j a va2s . c om styleSheet.addRule(".someclass1 {color: blue;}"); styleSheet.addRule(".someclass2 {color: green;}"); htmlEditorKit.setStyleSheet(styleSheet); htmlDocument = (HTMLDocument) htmlEditorKit.createDefaultDocument(); JTextPane jTextPane = new JTextPane(); jTextPane.setEditorKit(htmlEditorKit); jTextPane.setDocument(htmlDocument); try { Element htmlElement = htmlDocument.getRootElements()[0]; bodyElement = htmlElement.getElement(0); Container contentPane = getContentPane(); contentPane.add(jTextPane, BorderLayout.CENTER); super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); addContent("<span class=someclass1>test 1</span><br>"); addContent("<span class=someclass2>test 2</span><br>"); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public void reapplyStyles() { Element sectionElem = bodyElement.getElement(bodyElement.getElementCount() - 1); int paraCount = sectionElem.getElementCount(); for (int i = 0; i < paraCount; i++) { Element e = sectionElem.getElement(i); int rangeStart = e.getStartOffset(); int rangeEnd = e.getEndOffset(); htmlDocument.setParagraphAttributes(rangeStart, rangeEnd - rangeStart, e.getAttributes(), true); }//from ww w .java 2s . c o m }