List of usage examples for javax.swing.text AttributeSet getAttribute
public Object getAttribute(Object key);
From source file:ShowHTMLViews.java
public static void displayAttributes(AttributeSet a, int indent, PrintStream out) { if (a == null) return;/*from ww w . ja v a2 s . co m*/ a = a.copyAttributes(); Enumeration x = a.getAttributeNames(); for (int i = 0; i < indent; i++) { out.print(" "); } if (x != null) { out.println("ATTRIBUTES:"); while (x.hasMoreElements()) { for (int i = 0; i < indent; i++) { out.print(" "); } Object attr = x.nextElement(); out.println( " (" + attr + ", " + a.getAttribute(attr) + ")" + " [" + getShortClassName(attr.getClass()) + "/" + getShortClassName(a.getAttribute(attr).getClass()) + "] "); } } else { out.println("No attributes"); } if (a.getResolveParent() != null) { displayAttributes(a.getResolveParent().copyAttributes(), indent + 1, out); } }
From source file:Main.java
private void listAttributes(AttributeSet attributes) { Enumeration e = attributes.getAttributeNames(); while (e.hasMoreElements()) { Object name = e.nextElement(); Object value = attributes.getAttribute(name); if (!attributes.containsAttribute(name.toString(), value)) { System.out.println("containsAttribute() fails"); }//from ww w. ja v a 2 s. c o m if (!attributes.isDefined(name.toString())) { System.out.println("isDefined() fails"); } System.out.println(name + "=" + value); } }
From source file:Main.java
@Override public void mouseClicked(MouseEvent e) { Element h = getHyperlinkElement(e); if (h != null) { Object attribute = h.getAttributes().getAttribute(HTML.Tag.A); if (attribute instanceof AttributeSet) { AttributeSet set = (AttributeSet) attribute; String href = (String) set.getAttribute(HTML.Attribute.HREF); if (href != null) { try { Desktop.getDesktop().browse(new URI(href)); } catch (Exception e1) { e1.printStackTrace(); }/*from w ww . ja v a2 s . c o m*/ } } } }
From source file:MainClass.java
private void writeAttributes(AttributeSet attributes) throws IOException { Enumeration e = attributes.getAttributeNames(); while (e.hasMoreElements()) { Object name = e.nextElement(); String value = (String) attributes.getAttribute(name); try {/*from w ww. j a v a2s. c o m*/ if (name == HTML.Attribute.HREF || name == HTML.Attribute.SRC || name == HTML.Attribute.LOWSRC || name == HTML.Attribute.CODEBASE) { URL u = new URL(base, value); out.write(" " + name + "=\"" + u + "\""); } else { out.write(" " + name + "=\"" + value + "\""); } } catch (MalformedURLException ex) { System.err.println(ex); System.err.println(base); System.err.println(value); ex.printStackTrace(); } } }
From source file:EditorPaneExample10A.java
public URL[] findLinks(Document doc, String protocol) { Vector links = new Vector(); Vector urlNames = new Vector(); URL baseURL = (URL) doc.getProperty(Document.StreamDescriptionProperty); if (doc instanceof HTMLDocument) { HTMLDocument.Iterator iterator = ((HTMLDocument) doc).getIterator(HTML.Tag.A); for (; iterator.isValid(); iterator.next()) { AttributeSet attrs = iterator.getAttributes(); Object linkAttr = attrs.getAttribute(HTML.Attribute.HREF); if (linkAttr instanceof String) { try { URL linkURL = new URL(baseURL, (String) linkAttr); if (protocol == null || protocol.equalsIgnoreCase(linkURL.getProtocol())) { String linkURLName = linkURL.toString(); if (urlNames.contains(linkURLName) == false) { urlNames.addElement(linkURLName); links.addElement(linkURL); }/*from w ww . ja v a 2s.c o m*/ } } catch (MalformedURLException e) { // Ignore invalid links } } } } URL[] urls = new URL[links.size()]; links.copyInto(urls); links.removeAllElements(); urlNames.removeAllElements(); return urls; }
From source file:net.sf.jasperreports.engine.util.JEditorPaneHtmlMarkupProcessor.java
@Override public String convert(String srcText) { JEditorPane editorPane = new JEditorPane("text/html", srcText); editorPane.setEditable(false);//from w ww .j a va 2 s .co m List<Element> elements = new ArrayList<Element>(); Document document = editorPane.getDocument(); Element root = document.getDefaultRootElement(); if (root != null) { addElements(elements, root); } int startOffset = 0; int endOffset = 0; int crtOffset = 0; String chunk = null; JRPrintHyperlink hyperlink = null; Element element = null; Element parent = null; boolean bodyOccurred = false; int[] orderedListIndex = new int[elements.size()]; String whitespace = " "; String[] whitespaces = new String[elements.size()]; for (int i = 0; i < elements.size(); i++) { whitespaces[i] = ""; } StringBuilder text = new StringBuilder(); List<JRStyledText.Run> styleRuns = new ArrayList<>(); for (int i = 0; i < elements.size(); i++) { if (bodyOccurred && chunk != null) { text.append(chunk); Map<Attribute, Object> styleAttributes = getAttributes(element.getAttributes()); if (hyperlink != null) { styleAttributes.put(JRTextAttribute.HYPERLINK, hyperlink); hyperlink = null; } if (!styleAttributes.isEmpty()) { styleRuns.add( new JRStyledText.Run(styleAttributes, startOffset + crtOffset, endOffset + crtOffset)); } } chunk = null; element = elements.get(i); parent = element.getParentElement(); startOffset = element.getStartOffset(); endOffset = element.getEndOffset(); AttributeSet attrs = element.getAttributes(); Object elementName = attrs.getAttribute(AbstractDocument.ElementNameAttribute); Object object = (elementName != null) ? null : attrs.getAttribute(StyleConstants.NameAttribute); if (object instanceof HTML.Tag) { HTML.Tag htmlTag = (HTML.Tag) object; if (htmlTag == Tag.BODY) { bodyOccurred = true; crtOffset = -startOffset; } else if (htmlTag == Tag.BR) { chunk = "\n"; } else if (htmlTag == Tag.OL) { orderedListIndex[i] = 0; String parentName = parent.getName().toLowerCase(); whitespaces[i] = whitespaces[elements.indexOf(parent)] + whitespace; if (parentName.equals("li")) { chunk = ""; } else { chunk = "\n"; ++crtOffset; } } else if (htmlTag == Tag.UL) { whitespaces[i] = whitespaces[elements.indexOf(parent)] + whitespace; String parentName = parent.getName().toLowerCase(); if (parentName.equals("li")) { chunk = ""; } else { chunk = "\n"; ++crtOffset; } } else if (htmlTag == Tag.LI) { whitespaces[i] = whitespaces[elements.indexOf(parent)]; if (element.getElement(0) != null && (element.getElement(0).getName().toLowerCase().equals("ol") || element.getElement(0).getName().toLowerCase().equals("ul"))) { chunk = ""; } else if (parent.getName().equals("ol")) { int index = elements.indexOf(parent); Object type = parent.getAttributes().getAttribute(HTML.Attribute.TYPE); Object startObject = parent.getAttributes().getAttribute(HTML.Attribute.START); int start = startObject == null ? 0 : Math.max(0, Integer.valueOf(startObject.toString()) - 1); String suffix = ""; ++orderedListIndex[index]; if (type != null) { switch (((String) type).charAt(0)) { case 'A': suffix = getOLBulletChars(orderedListIndex[index] + start, true); break; case 'a': suffix = getOLBulletChars(orderedListIndex[index] + start, false); break; case 'I': suffix = JRStringUtil.getRomanNumeral(orderedListIndex[index] + start, true); break; case 'i': suffix = JRStringUtil.getRomanNumeral(orderedListIndex[index] + start, false); break; case '1': default: suffix = String.valueOf(orderedListIndex[index] + start); break; } } else { suffix += orderedListIndex[index] + start; } chunk = whitespaces[index] + suffix + DEFAULT_BULLET_SEPARATOR + " "; } else { chunk = whitespaces[elements.indexOf(parent)] + DEFAULT_BULLET_CHARACTER + " "; } crtOffset += chunk.length(); } else if (element instanceof LeafElement) { if (element instanceof RunElement) { RunElement runElement = (RunElement) element; AttributeSet attrSet = (AttributeSet) runElement.getAttribute(Tag.A); if (attrSet != null) { hyperlink = new JRBasePrintHyperlink(); hyperlink.setHyperlinkType(HyperlinkTypeEnum.REFERENCE); hyperlink.setHyperlinkReference((String) attrSet.getAttribute(HTML.Attribute.HREF)); hyperlink.setLinkTarget((String) attrSet.getAttribute(HTML.Attribute.TARGET)); } } try { chunk = document.getText(startOffset, endOffset - startOffset); } catch (BadLocationException e) { if (log.isDebugEnabled()) { log.debug("Error converting markup.", e); } } } } } if (chunk != null) { if (!"\n".equals(chunk)) { text.append(chunk); Map<Attribute, Object> styleAttributes = getAttributes(element.getAttributes()); if (hyperlink != null) { styleAttributes.put(JRTextAttribute.HYPERLINK, hyperlink); hyperlink = null; } if (!styleAttributes.isEmpty()) { styleRuns.add( new JRStyledText.Run(styleAttributes, startOffset + crtOffset, endOffset + crtOffset)); } } else { //final newline, not appending //check if there's any style run that would have covered it, that can happen if there's a <li> tag with style int length = text.length(); for (ListIterator<JRStyledText.Run> it = styleRuns.listIterator(); it.hasNext();) { JRStyledText.Run run = it.next(); //only looking at runs that end at the position where the newline should have been //we don't want to hide bugs in which runs that span after the text length are created if (run.endIndex == length + 1) { if (run.startIndex < run.endIndex - 1) { it.set(new JRStyledText.Run(run.attributes, run.startIndex, run.endIndex - 1)); } else { it.remove(); } } } } } JRStyledText styledText = new JRStyledText(null, text.toString()); for (JRStyledText.Run run : styleRuns) { styledText.addRun(run); } styledText.setGlobalAttributes(new HashMap<Attribute, Object>()); return JRStyledTextParser.getInstance().write(styledText); }
From source file:edu.ku.brc.af.ui.forms.formatters.DataObjFieldFormatSinglePanel.java
/** * @param formatter/*from w w w . j a va 2s .co m*/ */ protected void setFormatterFromTextPane(final DataObjSwitchFormatter formatter) { // visit every character in the document text looking for fields // store characters not associated with components (jbutton) to make up the separator text DefaultStyledDocument doc = (DefaultStyledDocument) formatEditor.getStyledDocument(); String text = formatEditor.getText(); int docLen = doc.getLength(); int lastFieldPos = 0; Vector<DataObjDataField> fields = new Vector<DataObjDataField>(); //int cnt = 0; for (int i = 0; i < docLen; ++i) { Element element = doc.getCharacterElement(i); AttributeSet attrs = element.getAttributes(); Object obj = attrs.getAttribute(StyleConstants.ComponentAttribute); //System.out.print(String.format("i: %d, lastFieldPos: %d cnt: %d, F: %s", i, lastFieldPos, cnt, (obj instanceof FieldDefinitionComp ? "Y" : "N"))); if (obj instanceof FieldDefinitionComp) { //System.out.println(cnt+" "+(obj instanceof FieldDefinitionComp)); // found button at the current position // create corresponding field String sepStr = (lastFieldPos <= i - 1) ? text.substring(lastFieldPos, i) : ""; FieldDefinitionComp fieldDefBtn = (FieldDefinitionComp) obj; DataObjDataField fmtField = fieldDefBtn.getValue(); fmtField.setSep(sepStr); fields.add(fmtField); //System.out.print(" Sep: ["+sepStr+"]"); lastFieldPos = i + 1; //cnt++; } } // XXX: what do we do with the remaining of the text? right now we ignore it // That's because we can't create an empty formatter field just to use the separator... DataObjDataField[] fieldsArray = new DataObjDataField[fields.size()]; for (int i = 0; i < fields.size(); ++i) { fieldsArray[i] = fields.elementAt(i); } DataObjDataFieldFormat singleFormatter = fieldsArray.length == 0 ? null : new DataObjDataFieldFormat("", tableInfo.getClassObj(), false, "", "", fieldsArray); formatter.setSingle(singleFormatter); }
From source file:EditorPaneExample16.java
public Heading getNextHeading(Document doc, ElementIterator iter) { Element elem;/*from ww w.j a v a2s . c o m*/ while ((elem = iter.next()) != null) { AttributeSet attrs = elem.getAttributes(); Object type = attrs.getAttribute(StyleConstants.NameAttribute); int level = getHeadingLevel(type); if (level > 0) { // It is a heading - get the text String headingText = ""; int count = elem.getElementCount(); for (int i = 0; i < count; i++) { Element child = elem.getElement(i); AttributeSet cattrs = child.getAttributes(); if (cattrs.getAttribute(StyleConstants.NameAttribute) == HTML.Tag.CONTENT) { try { int offset = child.getStartOffset(); headingText += doc.getText(offset, child.getEndOffset() - offset); } catch (BadLocationException e) { } } } headingText = headingText.trim(); return new Heading(headingText, level, elem.getStartOffset()); } } return null; }
From source file:EditorPaneExample16.java
public URL[] findLinks(Document doc, String protocol) { Vector links = new Vector(); Vector urlNames = new Vector(); URL baseURL = (URL) doc.getProperty(Document.StreamDescriptionProperty); if (doc instanceof HTMLDocument) { Element elem = doc.getDefaultRootElement(); ElementIterator iterator = new ElementIterator(elem); while ((elem = iterator.next()) != null) { AttributeSet attrs = elem.getAttributes(); Object link = attrs.getAttribute(HTML.Tag.A); if (link instanceof AttributeSet) { Object linkAttr = ((AttributeSet) link).getAttribute(HTML.Attribute.HREF); if (linkAttr instanceof String) { try { URL linkURL = new URL(baseURL, (String) linkAttr); if (protocol == null || protocol.equalsIgnoreCase(linkURL.getProtocol())) { String linkURLName = linkURL.toString(); if (urlNames.contains(linkURLName) == false) { urlNames.addElement(linkURLName); links.addElement(linkURL); }/*from ww w . j av a 2s . c o m*/ } } catch (MalformedURLException e) { // Ignore invalid links } } } } } URL[] urls = new URL[links.size()]; links.copyInto(urls); links.removeAllElements(); urlNames.removeAllElements(); return urls; }
From source file:com.hexidec.ekit.component.RelativeImageView.java
/** Method looks up an integer-valued attribute (not recursive!) *//*from w w w. j a va 2 s . c om*/ private int getIntAttr(HTML.Attribute name, int iDefault) { AttributeSet attr = fElement.getAttributes(); if (attr.isDefined(name)) { int i; String val = (String) attr.getAttribute(name); if (val == null) { i = iDefault; } else { try { i = Math.max(0, Integer.parseInt(val)); } catch (NumberFormatException nfe) { i = iDefault; } } return i; } else { return iDefault; } }