Example usage for javax.swing.text MutableAttributeSet getAttribute

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

Introduction

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

Prototype

public Object getAttribute(Object key);

Source Link

Document

Fetches the value of the given attribute.

Usage

From source file:Main.java

public final static void main(String[] args) throws Exception {
    final ArrayList<String> list = new ArrayList<String>();

    ParserDelegator parserDelegator = new ParserDelegator();
    ParserCallback parserCallback = new ParserCallback() {
        public void handleText(final char[] data, final int pos) {
        }//from   w  ww  .  j  av a  2  s.  c o m

        public void handleStartTag(Tag tag, MutableAttributeSet attribute, int pos) {
            if (tag == Tag.A) {
                String address = (String) attribute.getAttribute(Attribute.HREF);
                list.add(address);
            }
        }

        public void handleEndTag(Tag t, final int pos) {
        }

        public void handleSimpleTag(Tag t, MutableAttributeSet a, final int pos) {
        }

        public void handleComment(final char[] data, final int pos) {
        }

        public void handleError(final java.lang.String errMsg, final int pos) {
        }
    };
    parserDelegator.parse(new FileReader("a.html"), parserCallback, false);
    System.out.println(list);
}

From source file:com.net2plan.utils.HTMLUtils.java

private static String prepareImagePath(String html, URL url) {
    final Set<String> list = new TreeSet<String>();
    final ParserDelegator parserDelegator = new ParserDelegator();
    final HTMLEditorKit.ParserCallback parserCallback = new HTMLEditorKit.ParserCallback() {
        @Override//  www.  j  ava2 s  .co m
        public void handleText(final char[] data, final int pos) {
        }

        @Override
        public void handleStartTag(HTML.Tag tag, MutableAttributeSet attribute, int pos) {
            if (tag == HTML.Tag.IMG) {
                String address = (String) attribute.getAttribute(HTML.Attribute.SRC);
                list.add(address);
            }
        }

        @Override
        public void handleEndTag(HTML.Tag t, final int pos) {
        }

        @Override
        public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, final int pos) {
            if (t == HTML.Tag.IMG) {
                String address = (String) a.getAttribute(HTML.Attribute.SRC);
                list.add(address);
            }
        }

        @Override
        public void handleComment(final char[] data, final int pos) {
        }

        @Override
        public void handleError(final String errMsg, final int pos) {
        }
    };

    final Reader reader = new StringReader(html);
    try {
        parserDelegator.parse(reader, parserCallback, true);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    for (String item : list) {
        try {
            URL newURL = new URL(url, item);
            html = html.replace(item, newURL.toExternalForm());
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

    return html;
}

From source file:Main.java

public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
    if (t == HTML.Tag.A) {
        System.out.println("<A HREF=\"" + a.getAttribute(HTML.Attribute.HREF) + "\">"
                + a.getAttribute(HTML.Attribute.HREF) + "</A><BR>");
    }//from  w w  w  .j av a2s  . c  o  m
}

From source file:MainClass.java

public void handleStartTag(HTML.Tag tag, MutableAttributeSet attributes, int position) {
    try {/*from w ww.  j a  v a  2  s  . c om*/
        out.write("<" + tag);
        this.writeAttributes(attributes);
        if (tag == HTML.Tag.APPLET && attributes.getAttribute(HTML.Attribute.CODEBASE) == null) {
            String codebase = base.toString();
            if (codebase.endsWith(".htm") || codebase.endsWith(".html")) {
                codebase = codebase.substring(0, codebase.lastIndexOf('/'));
            }
            out.write(" codebase=\"" + codebase + "\"");
        }
        out.write(">");
        out.flush();
    } catch (IOException ex) {
        System.err.println(ex);
    }
}

From source file:net.meltdowntech.steamstats.SteamUser.java

public void fetchLevel() {
    try {// w  ww.  jav  a 2  s .c  o m
        String url = String.format("http://steamcommunity.com/profiles/%s/badges", getCommunityId() + "");
        URL steam = new URL(url);
        URLConnection yc = steam.openConnection();
        Reader reader = new InputStreamReader(yc.getInputStream());
        HTMLEditorKit.Parser parser = new ParserDelegator();
        parser.parse(reader, new HTMLEditorKit.ParserCallback() {
            private boolean foundIt;

            @Override
            public void handleText(char[] data, int pos) {
                if (foundIt)
                    values.put("level", Integer.parseInt(new String(data)));
            }

            @Override
            public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
                if (t == HTML.Tag.SPAN) {
                    Object o = a.getAttribute(HTML.Attribute.CLASS);
                    if (o != null && o.equals("friendPlayerLevelNum"))
                        foundIt = true;
                }
            }

            @Override
            public void handleEndTag(HTML.Tag t, int pos) {
                if (t == HTML.Tag.SPAN)
                    foundIt = false;
            }
        }, true);

    } catch (IOException ex) {
        Util.printError("Invalid data");
    }
}

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

public void replaceAttributes(Element e, AttributeSet a, Tag tag) {
    if ((e != null) && (a != null)) {
        try {//from  ww w. j  av  a2  s .  c  o  m
            writeLock();
            int start = e.getStartOffset();
            DefaultDocumentEvent changes = new DefaultDocumentEvent(start, e.getEndOffset() - start,
                    DocumentEvent.EventType.CHANGE);
            AttributeSet sCopy = a.copyAttributes();
            changes.addEdit(new AttributeUndoableEdit(e, sCopy, false));
            MutableAttributeSet attr = (MutableAttributeSet) e.getAttributes();
            Enumeration aNames = attr.getAttributeNames();
            Object value;
            Object aName;
            while (aNames.hasMoreElements()) {
                aName = aNames.nextElement();
                value = attr.getAttribute(aName);
                if (value != null && !value.toString().equalsIgnoreCase(tag.toString())) {
                    attr.removeAttribute(aName);
                }
            }
            attr.addAttributes(a);
            changes.end();
            fireChangedUpdate(changes);
            fireUndoableEditUpdate(new UndoableEditEvent(this, changes));
        } finally {
            writeUnlock();
        }
    }
}