Example usage for javax.swing.text SimpleAttributeSet getAttribute

List of usage examples for javax.swing.text SimpleAttributeSet getAttribute

Introduction

In this page you can find the example usage for javax.swing.text SimpleAttributeSet getAttribute.

Prototype

public Object getAttribute(Object name) 

Source Link

Document

Gets the value of an attribute.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    URL url = new URI("http://www.google.com").toURL();
    URLConnection conn = url.openConnection();
    Reader rd = new InputStreamReader(conn.getInputStream());

    EditorKit kit = new HTMLEditorKit();
    HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
    kit.read(rd, doc, 0);/* ww  w  .j a  v a  2 s  . c  o  m*/

    HTMLDocument.Iterator it = doc.getIterator(HTML.Tag.A);
    while (it.isValid()) {
        SimpleAttributeSet s = (SimpleAttributeSet) it.getAttributes();

        String link = (String) s.getAttribute(HTML.Attribute.HREF);
        if (link != null) {
            System.out.println(link);
        }
        it.next();
    }
}

From source file:com.hexidec.ekit.component.HTMLUtilities.java

public SimpleAttributeSet removeAttribute(SimpleAttributeSet sourceAS, SimpleAttributeSet removeAS) {
    try {/*ww  w.j  ava 2 s .  com*/
        String[] sourceKeys = new String[sourceAS.getAttributeCount()];
        String[] sourceValues = new String[sourceAS.getAttributeCount()];
        Enumeration sourceEn = sourceAS.getAttributeNames();
        int i = 0;
        while (sourceEn.hasMoreElements()) {
            Object temp = new Object();
            temp = sourceEn.nextElement();
            sourceKeys[i] = (String) temp.toString();
            sourceValues[i] = new String();
            sourceValues[i] = (String) sourceAS.getAttribute(temp).toString();
            i++;
        }
        String[] removeKeys = new String[removeAS.getAttributeCount()];
        String[] removeValues = new String[removeAS.getAttributeCount()];
        Enumeration removeEn = removeAS.getAttributeNames();
        int j = 0;
        while (removeEn.hasMoreElements()) {
            removeKeys[j] = (String) removeEn.nextElement().toString();
            removeValues[j] = (String) removeAS.getAttribute(removeKeys[j]).toString();
            j++;
        }
        SimpleAttributeSet result = new SimpleAttributeSet();
        boolean hit = false;
        for (int countSource = 0; countSource < sourceKeys.length; countSource++) {
            hit = false;
            if (sourceKeys[countSource] == "name" | sourceKeys[countSource] == "resolver") {
                hit = true;
            } else {
                for (int countRemove = 0; countRemove < removeKeys.length; countRemove++) {
                    if (removeKeys[countRemove] != "NULL") {
                        if (sourceKeys[countSource].toString() == removeKeys[countRemove].toString()) {
                            if (removeValues[countRemove] != "NULL") {
                                if (sourceValues[countSource].toString() == removeValues[countRemove]
                                        .toString()) {
                                    hit = true;
                                }
                            } else if (removeValues[countRemove] == "NULL") {
                                hit = true;
                            }
                        }
                    } else if (removeKeys[countRemove] == "NULL") {
                        if (sourceValues[countSource].toString() == removeValues[countRemove].toString()) {
                            hit = true;
                        }
                    }
                }
            }
            if (!hit) {
                result.addAttribute(sourceKeys[countSource].toString(), sourceValues[countSource].toString());
            }
        }
        return result;
    } catch (ClassCastException cce) {
        return null;
    }
}

From source file:org.apache.camel.maven.JavadocParser.java

@Override
protected void startTag(TagElement tag) throws ChangedCharSetException {
    super.startTag(tag);

    final HTML.Tag htmlTag = tag.getHTMLTag();
    if (htmlTag != null) {
        if (HTML.Tag.A.equals(htmlTag)) {
            final SimpleAttributeSet attributes = getAttributes();
            final Object name = attributes.getAttribute(HTML.Attribute.NAME);
            if (name != null) {
                final String nameAttr = (String) name;
                if (parserState == ParserState.INIT
                        && ("method_summary".equals(nameAttr) || "method.summary".equals(nameAttr))) {
                    parserState = ParserState.METHOD_SUMMARY;
                } else if (parserState == ParserState.METHOD) {
                    if (methodWithTypes == null) {

                        final String hrefAttr = (String) attributes.getAttribute(HTML.Attribute.HREF);
                        if (hrefAttr != null && hrefAttr.contains(hrefPattern)) {

                            // unescape HTML
                            String methodSignature = hrefAttr.substring(hrefAttr.indexOf('#') + 1);
                            final int firstHyphen = methodSignature.indexOf('-');
                            if (firstHyphen != -1) {
                                final int lastHyphen = methodSignature.lastIndexOf('-');
                                methodSignature = methodSignature.substring(0, firstHyphen) + "("
                                        + methodSignature.substring(firstHyphen + 1, lastHyphen) + ")";
                                methodSignature = methodSignature.replaceAll("-", ",");
                            }/*from  ww w . j  a va  2s .  c o  m*/
                            // support varargs
                            if (methodSignature.contains("...)")) {
                                methodSignature = methodSignature.replaceAll("\\.\\.\\.\\)", "[])");
                            }
                            // map Java8 array types
                            if (methodSignature.contains(":A")) {
                                methodSignature = methodSignature.replaceAll(":A", "[]");
                            }
                            methodWithTypes = unescapeHtml(methodSignature);
                        }
                    } else {
                        final String title = (String) attributes.getAttribute(HTML.Attribute.TITLE);
                        if (title != null) {
                            // append package name to type name text
                            methodTextBuilder.append(title.substring(title.lastIndexOf(' '))).append('.');
                        }
                    }
                }
            }
        } else if (parserState == ParserState.METHOD_SUMMARY && HTML.Tag.CODE.equals(htmlTag)) {
            parserState = ParserState.METHOD;
        }
    }
}