Java 类javax.swing.text.html.HTMLDocument.Iterator 实例源码

项目:marathonv5    文件:JEditorPaneTagJavaElement.java   
@Override public String getAttribute(final String name) {
    if ("text".equals(name)) {
        return getText();
    }
    if ("hRefIndex".equals(name)) {
        return getHRefIndex() + "";
    }
    if ("textIndex".equals(name)) {
        return getTextIndex() + "";
    }
    return EventQueueWait.exec(new Callable<String>() {
        @Override public String call() throws Exception {
            Iterator iterator = findTag((HTMLDocument) ((JEditorPane) parent.getComponent()).getDocument());
            AttributeSet attributes = iterator.getAttributes();
            Attribute attr = findAttribute(name);
            if (attr != null && attributes.isDefined(attr)) {
                return attributes.getAttribute(attr).toString();
            }
            return null;
        }
    });
}
项目:marathonv5    文件:JEditorPaneTagJavaElement.java   
@Override public Object _makeVisible() {
    JEditorPane editor = (JEditorPane) parent.getComponent();
    Iterator iterator = findTag((HTMLDocument) editor.getDocument());
    int startOffset = iterator.getStartOffset();
    int endOffset = iterator.getEndOffset();
    try {
        Rectangle bounds = editor.modelToView(startOffset + (endOffset - startOffset) / 2);
        if (bounds != null) {
            bounds.height = editor.getVisibleRect().height;
            editor.scrollRectToVisible(bounds);
        }
    } catch (BadLocationException e) {
        throw new InvalidElementStateException("Unable to get text for tag " + tag + " in document with index " + index, e);
    }
    return null;
}
项目:marathonv5    文件:JEditorPaneTagJavaElement.java   
public int getTextIndex() {
    return EventQueueWait.exec(new Callable<Integer>() {
        @Override public Integer call() throws Exception {
            String href = getText();
            int hRefIndex = 0;
            int current = 0;
            JEditorPane editor = (JEditorPane) parent.getComponent();
            HTMLDocument document = (HTMLDocument) editor.getDocument();
            Iterator iterator = document.getIterator(Tag.A);
            while (iterator.isValid()) {
                if (current++ >= index) {
                    return hRefIndex;
                }
                String attribute = ((HTMLDocument) ((JEditorPane) parent.getComponent()).getDocument())
                        .getText(iterator.getStartOffset(), iterator.getEndOffset() - iterator.getStartOffset());
                if (attribute != null && attribute.equals(href)) {
                    hRefIndex++;
                }
                iterator.next();
            }
            return -1;
        }
    });
}
项目:marathonv5    文件:REditorPane.java   
public void setHRef(int pos, Document doc) {
    hRef = null;
    text = null;
    if (!(doc instanceof HTMLDocument)) {
        return;
    }
    HTMLDocument hdoc = (HTMLDocument) doc;
    Iterator iterator = hdoc.getIterator(HTML.Tag.A);
    while (iterator.isValid()) {
        if (pos >= iterator.getStartOffset() && pos < iterator.getEndOffset()) {
            AttributeSet attributes = iterator.getAttributes();
            if (attributes != null && attributes.getAttribute(HTML.Attribute.HREF) != null) {
                try {
                    text = hdoc.getText(iterator.getStartOffset(), iterator.getEndOffset() - iterator.getStartOffset()).trim();
                    hRef = attributes.getAttribute(HTML.Attribute.HREF).toString();
                    setIndexOfHrefAndText(hdoc, pos, text, hRef);
                } catch (BadLocationException e) {
                    e.printStackTrace();
                }
                return;
            }
        }
        iterator.next();
    }
}
项目:StarQuestCode    文件:StationUtils.java   
public static String getStationAtLocation(Location l){
    System.out.println("Method called.");
    ApplicableRegionSet set = wg.getRegionManager(l.getWorld()).getApplicableRegions(l);
    System.out.println("Crazy test!");
    Set<ProtectedRegion> regions = set.getRegions();
    java.util.Iterator<ProtectedRegion> i = regions.iterator();
    while(i.hasNext()){
        ProtectedRegion r = i.next();
        List<String> stations = new ArrayList<String>();
        stations.addAll(alphaStations);
        stations.addAll(betaStations);
        stations.addAll(gammaStations);
        for(String s : stations){
            if(r.getId().equals(s.toLowerCase())){
                return s;
            }
        }
    }
    return null;
}
项目:marathonv5    文件:JEditorPaneTagJavaElement.java   
@Override public String _getText() {
    Iterator iterator = findTag((HTMLDocument) ((JEditorPane) parent.getComponent()).getDocument());
    int startOffset = iterator.getStartOffset();
    int endOffset = iterator.getEndOffset();
    try {
        return ((HTMLDocument) ((JEditorPane) parent.getComponent()).getDocument()).getText(startOffset,
                endOffset - startOffset);
    } catch (BadLocationException e) {
        throw new InvalidElementStateException("Unable to get text for tag " + tag + " in document with index " + index, e);
    }
}
项目:marathonv5    文件:JEditorPaneTagJavaElement.java   
private Iterator findTag(HTMLDocument doc) {
    Iterator iterator = doc.getIterator(tag);
    int current = 0;
    while (iterator.isValid()) {
        if (current++ == index) {
            break;
        }
        iterator.next();
    }
    if (!iterator.isValid()) {
        throw new NoSuchElementException("Unable to find tag " + tag + " in document with index " + index, null);
    }
    return iterator;
}
项目:marathonv5    文件:JEditorPaneTagJavaElement.java   
@Override public void _moveto() {
    JEditorPane editor = (JEditorPane) parent.getComponent();
    Iterator iterator = findTag((HTMLDocument) editor.getDocument());
    int startOffset = iterator.getStartOffset();
    int endOffset = iterator.getEndOffset();
    try {
        Rectangle bounds = editor.modelToView(startOffset + (endOffset - startOffset) / 2);
        getDriver().getDevices().moveto(parent.getComponent(), bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
    } catch (BadLocationException e) {
        throw new InvalidElementStateException("Unable to get text for tag " + tag + " in document with index " + index, e);
    }
}
项目:marathonv5    文件:JEditorPaneTagJavaElement.java   
@Override public Point _getMidpoint() {
    JEditorPane editor = (JEditorPane) parent.getComponent();
    Iterator iterator = findTag((HTMLDocument) editor.getDocument());
    int startOffset = iterator.getStartOffset();
    int endOffset = iterator.getEndOffset();
    try {
        Rectangle bounds = editor.modelToView(startOffset + (endOffset - startOffset) / 2);
        return new Point(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
    } catch (BadLocationException e) {
        throw new InvalidElementStateException("Unable to get text for tag " + tag + " in document with index " + index + "("
                + "StartOffset: " + startOffset + " EndOffset: " + endOffset + ")", e);
    }
}
项目:marathonv5    文件:JEditorPaneTagJavaElement.java   
public int getHRefIndex() {
    return EventQueueWait.exec(new Callable<Integer>() {
        @Override public Integer call() throws Exception {
            String href = getAttribute("href");
            int hRefIndex = 0;
            int current = 0;
            JEditorPane editor = (JEditorPane) parent.getComponent();
            HTMLDocument document = (HTMLDocument) editor.getDocument();
            Iterator iterator = document.getIterator(Tag.A);
            while (iterator.isValid()) {
                if (current++ >= index) {
                    return hRefIndex;
                }
                AttributeSet attributes = iterator.getAttributes();
                if (attributes != null) {
                    Object attributeObject = attributes.getAttribute(HTML.Attribute.HREF);
                    if (attributeObject != null) {
                        String attribute = attributeObject.toString();
                        if (attribute.equals(href)) {
                            hRefIndex++;
                        }
                    }
                }
                iterator.next();
            }
            return -1;
        }
    });
}
项目:marathonv5    文件:JEditorPaneJavaElement.java   
private void fillElements(Tag tag, ArrayList<IJavaElement> r, Predicate predicate) {
    HTMLDocument document = (HTMLDocument) ((JEditorPane) getComponent()).getDocument();
    Iterator iterator = document.getIterator(tag);
    int index = 0;
    while (iterator.isValid()) {
        JEditorPaneTagJavaElement e = new JEditorPaneTagJavaElement(this, tag, index++);
        if (predicate.isValid(e)) {
            r.add(e);
        }
        iterator.next();
    }
}
项目:marathonv5    文件:REditorPane.java   
private void setIndexOfHrefAndText(HTMLDocument hdoc, int pos, String text, String hRef) {
    this.hRefIndex = 0;
    this.textIndex = 0;
    Iterator iterator = hdoc.getIterator(HTML.Tag.A);
    while (iterator.isValid()) {
        if (pos >= iterator.getStartOffset() && pos < iterator.getEndOffset()) {
            return;
        } else {
            AttributeSet attributes = iterator.getAttributes();
            if (attributes != null && attributes.getAttribute(HTML.Attribute.HREF) != null) {
                try {
                    String t = hdoc.getText(iterator.getStartOffset(), iterator.getEndOffset() - iterator.getStartOffset())
                            .trim();
                    String h = attributes.getAttribute(HTML.Attribute.HREF).toString();
                    if (t.equals(text)) {
                        this.textIndex++;
                    }
                    if (h.equals(hRef)) {
                        this.hRefIndex++;
                    }
                } catch (BadLocationException e) {
                    e.printStackTrace();
                }
            }
        }
        iterator.next();
    }
}
项目:marathonv5    文件:REditorPaneTest.java   
private void searchAsText(String spec, boolean isText) {
    Document document = getEditor().getDocument();
    hRef = null;
    text = null;
    hRefIndex = 0;
    textIndex = 0;
    linkPosition = -1;
    int lastIndexOf = spec.lastIndexOf('(');
    if (lastIndexOf != -1) {
        if (isText) {
            textIndex = Integer.parseInt(spec.substring(lastIndexOf + 1, spec.length() - 1));
        } else {
            hRefIndex = Integer.parseInt(spec.substring(lastIndexOf + 1, spec.length() - 1));
        }
        spec = spec.substring(0, lastIndexOf);
    }
    if (!(document instanceof HTMLDocument)) {
        return;
    }
    HTMLDocument hdoc = (HTMLDocument) document;
    Iterator iterator = hdoc.getIterator(HTML.Tag.A);
    int curIndex = 0;
    while (iterator.isValid()) {
        String t;
        AttributeSet attributes = iterator.getAttributes();
        try {
            if (isText) {
                t = hdoc.getText(iterator.getStartOffset(), iterator.getEndOffset() - iterator.getStartOffset());
            } else {
                t = attributes.getAttribute(HTML.Attribute.HREF).toString();
            }
        } catch (BadLocationException e1) {
            return;
        }
        if (t.contains(spec) && (isText && curIndex++ == textIndex || !isText && curIndex++ == hRefIndex)) {
            if (attributes != null && attributes.getAttribute(HTML.Attribute.HREF) != null) {
                try {
                    text = hdoc.getText(iterator.getStartOffset(), iterator.getEndOffset() - iterator.getStartOffset()).trim();
                    hRef = attributes.getAttribute(HTML.Attribute.HREF).toString();
                    linkPosition = (iterator.getStartOffset() + iterator.getEndOffset()) / 2;
                } catch (BadLocationException e) {
                    return;
                }
                return;
            }
        }
        iterator.next();
    }
}
项目:mindraider    文件:TextWysiwyg.java   
/**
 * Inject hyperlinks to text.
 * 
 * @param text
 *            annotation.
 * @param mrHyperlink
 *            hyperlink to be included (in MR format
 *            {@link NoteInterlinking}). Can be null.
 * @param offset
 *            where to include hyperlink.
 * @return annotation with injected links.
 */
private String injectLinksToText(String text, String mrHyperlink, int offset) {
    logger.debug("=-> injectLinksToText");
    // convert links to MR notation
    Iterator iterator = ((HTMLDocument) getDocument()).getIterator(HTML.getTag("a"));
    int lastIndex = 0;
    boolean injected = true;
    if (mrHyperlink != null && offset >= 0) {
        injected = false;
    }
    StringBuffer linked = new StringBuffer();
    do {
        if (iterator.getStartOffset() > 0) {
            // injection: is it between the last position and the next
            // injection?
            if (!injected && offset < iterator.getStartOffset()) {
                injectLinksToTextAppend(mrHyperlink, text, offset, lastIndex, linked);

                lastIndex = offset;
                injected = true;
            }

            // process regular links
            String hrefText = text.substring(iterator.getStartOffset(), iterator.getEndOffset());
            Object hrefAttribute = iterator.getAttributes().getAttribute(HTML.Attribute.HREF);
            if(hrefAttribute==null) {
                linked.append(text.substring(lastIndex, iterator.getStartOffset()));
            } else {
                String href = hrefAttribute.toString();
                logger.debug("  [" + iterator.getStartOffset() + "-" + iterator.getEndOffset() + "]: "
                        + hrefText + " # " + href);
                if (NoteInterlinking.isMindRaiderLink(href)) {
                    injectLinksToTextAppend("%GREEN%[[" + href + "][" + hrefText + "]]%ENDCOLOR%",
                            text,
                            iterator.getStartOffset(),
                            lastIndex,
                            linked);
                } else {
                    injectLinksToTextAppend("[[" + href + "][" + hrefText + "]]",
                            text,
                            iterator.getStartOffset(),
                            lastIndex,
                            linked);
                }
            }

            lastIndex = iterator.getEndOffset();
            iterator.next();
        } else {
            break;
        }
    } while (true);

    // check whether injection is in the last part
    if (!injected) {
        injectLinksToTextAppend(mrHyperlink, text, offset, lastIndex, linked);
        lastIndex = offset;
    }

    linked.append(text.substring(lastIndex));
    String result = linked.toString();
    return result;
}