Java Code Examples for org.netbeans.editor.BaseDocument#getSyntaxSupport()

The following examples show how to use org.netbeans.editor.BaseDocument#getSyntaxSupport() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExtKit.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public boolean gotoDeclaration(JTextComponent target) {
    BaseDocument doc = Utilities.getDocument(target);
    if (doc == null)
        return false;
    try {
        Caret caret = target.getCaret();
        int dotPos = caret.getDot();
        int[] idBlk = Utilities.getIdentifierBlock(doc, dotPos);
        ExtSyntaxSupport extSup = (ExtSyntaxSupport)doc.getSyntaxSupport();
        if (idBlk != null) {
            int decPos = extSup.findDeclarationPosition(doc.getText(idBlk), idBlk[1]);
            if (decPos >= 0) {
                caret.setDot(decPos);
                return true;
            }
        }
    } catch (BadLocationException e) {
    }
    return false;
}
 
Example 2
Source File: StrutsEditorUtilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static int findEndOfElement(BaseDocument doc, String element) throws BadLocationException{
    String docText = doc.getText(0, doc.getLength());
    int offset = doc.getText(0, doc.getLength()).indexOf("</" + element);           //NOI18N
    ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport();
    TokenItem token;
    while (offset > 0){
       token = sup.getTokenChain(offset, offset+1); 
       if (token != null && token.getTokenID().getNumericID() == XML_ELEMENT){
            offset = token.getOffset();
            token = token.getNext();
            while (token != null
                    && !(token.getTokenID().getNumericID() == XML_ELEMENT
                    && token.getImage().equals(">")))               //NOI18N
                token = token.getNext();
            if (token != null)
                offset = token.getOffset();
           return offset;
       }
       offset = doc.getText(0, doc.getLength()).indexOf("</" + element);            //NOI18N
    }
    return -1;
}
 
Example 3
Source File: XDMPerfNumberTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testReadUsingSyntaxParser() throws Exception {
    System.out.println("testReadUsingSyntaxParser");
    java.net.URL url = getClass().getResource(SCHEMA_FILE);            
    // prepare document
    BaseDocument basedoc = new BaseDocument(true, "text/xml"); //NOI18N
    insertStringInDocument(new InputStreamReader(url.openStream(),"UTF-8"), basedoc);
    long start = System.currentTimeMillis();
    SyntaxSupport sup = basedoc.getSyntaxSupport();
    TokenItem ti = sup.getTokenChain(0);
    while(ti != null) {
        ti = ti.getNext();
    }
    long end = System.currentTimeMillis();
    System.out.println("Time taken to parse using Syntax Parser: " + (end-start) + "ms.\n");
    this.assertNotNull("Syntax parser model didn't get created!!!", sup);
}
 
Example 4
Source File: JSFEditorUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Returns the value of from-view-id element of navigation rule definition on the offset possition.
 *  If there is not the navigation rule definition on the offset, then returns null. 
 */
public static String getNavigationRule(BaseDocument doc, int offset){
    try {
        ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport();
        TokenItem token = sup.getTokenChain(offset, offset+1);
        // find the srart of the navigation rule definition
        while (token != null
                && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT
                && (token.getImage().equals("<navigation-rule")
                || token.getImage().equals("<managed-bean"))))
            token = token.getPrevious();
        if (token != null && token.getImage().equals("<navigation-rule")){
            // find the from-view-ide element
            while (token != null
                    && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT
                    && (token.getImage().equals("</navigation-rule")
                    || token.getImage().equals("<from-view-id"))))
                token = token.getNext();
            if (token!= null && token.getImage().equals("<from-view-id")){
                token = token.getNext();
                while (token!=null 
                        && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT
                        && token.getImage().equals(">")))
                    token = token.getNext();
                while (token != null
                        && token.getTokenID().getNumericID() != JSFEditorUtilities.XML_TEXT
                        && token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT)
                    token = token.getNext();
                if (token != null && token.getTokenID().getNumericID() == JSFEditorUtilities.XML_TEXT)
                    return token.getImage().trim();
            }
        }
    } 
    catch (BadLocationException e) {
        Exceptions.printStackTrace(e);
    }
    return null;
}
 
Example 5
Source File: ColoringTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void dumpTokens(String fileName, String ext) throws Exception {
    String pkgName = getClass().getPackage().getName();
    DataObject obj = TestUtil.THIS.findDataObject(pkgName + ".data", fileName, ext);
    EditorCookie ed = (EditorCookie) obj.getCookie(EditorCookie.class);
    BaseDocument doc = (BaseDocument) ed.openDocument();
    ExtSyntaxSupport ess = (ExtSyntaxSupport) doc.getSyntaxSupport();
    TokenItem token = ess.getTokenChain(0, doc.getLength());
    
    while (token != null) {
        TokenID tokenID = token.getTokenID();
        ref(tokenID.getName()+ ": " + token.getImage());
        token = token.getNext();
    }
    compareReferenceFiles();
}
 
Example 6
Source File: GotoAction.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void actionPerformed(ActionEvent evt, JTextComponent target) {
    String actionName = actionName();
    if (EditorActionNames.gotoDeclaration.equals(actionName)) {
        resetCaretMagicPosition(target);
        if (target != null) {
            if (hyperlinkGoTo(target)) {
                return;
            }
            
            BaseDocument doc = Utilities.getDocument(target);
            if (doc != null) {
                try {
                    Caret caret = target.getCaret();
                    int dotPos = caret.getDot();
                    int[] idBlk = Utilities.getIdentifierBlock(doc, dotPos);
                    ExtSyntaxSupport extSup = (ExtSyntaxSupport) doc.getSyntaxSupport();
                    if (idBlk != null) {
                        int decPos = extSup.findDeclarationPosition(doc.getText(idBlk), idBlk[1]);
                        if (decPos >= 0) {
                            caret.setDot(decPos);
                        }
                    }
                } catch (BadLocationException e) {
                }
            }
        }
    }
}
 
Example 7
Source File: StrutsConfigHyperlinkProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@NbBundle.Messages({
        "lbl.goto.formbean.not.found=ActionForm Bean {0} not found."
    })
private void findForm(String name, BaseDocument doc){
    ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport();
    
    int offset = findDefinitionInSection(sup, "form-beans", "form-bean", "name", name);
    if (offset > 0){
        JTextComponent target = Utilities.getFocusedComponent();
        target.setCaretPosition(offset);
    } else {
        StatusDisplayer.getDefault().setStatusText(Bundle.lbl_goto_formbean_not_found(name));
    }
}
 
Example 8
Source File: StrutsEditorUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static int findStartOfElement(BaseDocument doc, String element) throws BadLocationException{
    String docText = doc.getText(0, doc.getLength());
    int offset = doc.getText(0, doc.getLength()).indexOf("<" + element);            //NOI18N
    ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport();
    TokenItem token;
    while (offset > 0){
       token = sup.getTokenChain(offset, offset+1); 
       if (token != null && token.getTokenID().getNumericID() == XML_ELEMENT)
           return token.getOffset();
       offset = doc.getText(0, doc.getLength()).indexOf("<" + element);             //NOI18N
    }
    return -1;
}
 
Example 9
Source File: JSFConfigHyperlinkProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private String[] getElementValue(javax.swing.text.Document doc, int offset){
    String tag = null;
    String value = null;

    try {
        BaseDocument bdoc = (BaseDocument) doc;
        JTextComponent target = Utilities.getFocusedComponent();

        if (target == null || target.getDocument() != bdoc)
            return null;
        ExtSyntaxSupport sup = (ExtSyntaxSupport)bdoc.getSyntaxSupport();
        TokenItem token = sup.getTokenChain(offset, offset+1);
        if (token == null || token.getTokenID().getNumericID() != JSFEditorUtilities.XML_TEXT)
            return null;
        value = token.getImage();
        if (value != null){
            String original = value;
            value = value.trim();
            valueOffset = token.getOffset()+(original.indexOf(value));
        }
        // Find element
        // 4 - tag
        while(token != null
                && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT
                && !token.getImage().equals(">")))
            token = token.getPrevious();
        if (token == null)
            return null;
        tag = token.getImage().substring(1);
        if (debug) debug ("element: " + tag );   // NOI18N
        if (debug) debug ("value: " + value );  //NOI18N
        return new String[]{tag, value};

    }
    catch (BadLocationException e) {
        Exceptions.printStackTrace(e);
    }
    return null;
}
 
Example 10
Source File: StrutsEditorUtilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** Returns the value of the path attribute, when there is an action
 * definition on the offset possition. Otherwise returns null.
 */
public static String getActionPath(BaseDocument doc, int offset){
    try {
        ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport();
        TokenItem token = sup.getTokenChain(offset, offset+1);
        // find the element, which is on the offset
        while (token != null
                && !(token.getTokenID().getNumericID() == StrutsEditorUtilities.XML_ELEMENT
                    && !( token.getImage().equals("/>") 
                        || token.getImage().equals(">")
                        || token.getImage().equals("<forward")
                        || token.getImage().equals("<exception")
                        || token.getImage().equals("</action")
                        || token.getImage().equals("<description")
                        || token.getImage().equals("<display-name")
                        || token.getImage().equals("<set-property")
                        || token.getImage().equals("<icon"))))
            token = token.getPrevious();
        if (token != null && token.getImage().equals("<action")){   //NOI18N
            token = token.getNext();
            while (token!= null 
                    && token.getTokenID().getNumericID() != StrutsEditorUtilities.XML_ELEMENT
                    && !(token.getTokenID().getNumericID() == StrutsEditorUtilities.XML_ATTRIBUTE
                    && token.getImage().equals("path")))   // NOI18N
                token = token.getNext();
            if (token != null && token.getImage().equals("path")){ // NOI18N
                token = token.getNext();
                while (token != null 
                        && token.getTokenID().getNumericID() != StrutsEditorUtilities.XML_ATTRIBUTE_VALUE    
                        && token.getTokenID().getNumericID() != StrutsEditorUtilities.XML_ELEMENT
                        && token.getTokenID().getNumericID() != StrutsEditorUtilities.XML_ATTRIBUTE)
                    token = token.getNext();
                if (token != null && token.getTokenID().getNumericID() == StrutsEditorUtilities.XML_ATTRIBUTE_VALUE){
                    String value = token.getImage().trim();
                    value = value.substring(1);
                    value = value.substring(0, value.length()-1);
                    return value;
                }
            }
        }   
        return null;
    } catch (BadLocationException e) {
        Exceptions.printStackTrace(e);
    }
    return null;
}
 
Example 11
Source File: StrutsEditorUtilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static int writeElementIntoFather(BaseDocument doc, BaseBean bean, 
        String father, String fatherName, String element) throws IOException{
    int possition = -1;
    ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport();
    TokenItem token =  null;
    try {
        int offset = 0;
        // find the name as an attribute value
        do{
            offset = doc.getText(0, doc.getLength()).indexOf("\""+fatherName+"\"", offset+1);       //NOI18N
            token = sup.getTokenChain(offset, offset+1);
            if (token != null && token.getTokenID().getNumericID() == XML_ATTRIBUTE_VALUE)
                while ( token != null 
                        && token.getTokenID().getNumericID() != XML_ELEMENT)
                    token = token.getPrevious();
        }
        while (offset > 0 && token != null 
                    && !(token.getTokenID().getNumericID() == XML_ELEMENT
                    && token.getImage().equals("<"+father)));                       //NOI18N
        if (token != null && token.getTokenID().getNumericID() == XML_ELEMENT
                && token.getImage().equals("<"+father)){                            //NOI18N
            token = token.getNext();
            // find the /> or >
            while (token != null && token.getTokenID().getNumericID() != XML_ELEMENT )
                token = token.getNext();
            if (token != null && token.getImage().equals("/>")){                    //NOI18N
                StringBuffer text = new StringBuffer();
                offset = token.getOffset();
                text.append(">");                                                   //NOI18N
                text.append(END_LINE);
                text.append(addNewLines(bean));
                text.append(END_LINE);
                text.append("</"); text.append(father); text.append(">");           //NOI18N
                Reformat fmt = Reformat.get(doc);
                fmt.lock();
                try {
                    doc.atomicLock();
                    try{
                        doc.remove(offset, 2);
                        doc.insertString(offset, text.toString(), null);
                        Position endPos = doc.createPosition(offset + text.length() - 1);
                        fmt.reformat(offset, endPos.getOffset());
                        offset += Math.max(0, endPos.getOffset() - offset);
                        possition = offset;
                    }
                    finally{
                        doc.atomicUnlock();
                    }
                } finally {
                    fmt.unlock();
                }
            }
            if (token != null && token.getImage().equals(">")){                     //NOI18N
                offset = -1;
                while (token != null 
                        && !(token.getTokenID().getNumericID() == XML_ELEMENT
                        && token.getImage().equals("</"+father))){                  //NOI18N
                    while(token != null
                            && !(token.getTokenID().getNumericID() == XML_ELEMENT
                            && (token.getImage().equals("<"+element)                //NOI18N
                            || token.getImage().equals("</"+father))))              //NOI18N
                        token = token.getNext();
                    if (token != null && token.getImage().equals("<"+element)){     //NOI18N
                        while (token!= null
                                && !(token.getTokenID().getNumericID() == XML_ELEMENT
                                && (token.getImage().equals("/>")                   //NOI18N
                                || token.getImage().equals("</"+element))))         //NOI18N
                            token = token.getNext();
                        if (token != null && token.getImage().equals("</"+element)) //NOI18N
                            while (token!= null
                                    && !(token.getTokenID().getNumericID() == XML_ELEMENT
                                    && token.getImage().equals(">")))               //NOI18N
                                token = token.getNext();
                        if (token != null )
                            offset = token.getOffset() + token.getImage().length()-1;
                    }    
                    if (token != null && token.getImage().equals("</"+father) && offset == -1){     //NOI18N
                        while (token!= null
                                && !(token.getTokenID().getNumericID() == XML_ELEMENT
                                && (token.getImage().equals("/>")                   //NOI18N
                                || token.getImage().equals(">"))))                  //NOI18N
                            token = token.getPrevious();
                        offset = token.getOffset()+token.getImage().length()-1;
                    }   
                }
                if (offset > 0)
                    possition = writeString(doc, addNewLines(bean), offset);
            }
        }
            
    } catch (BadLocationException ex) {
        Exceptions.printStackTrace(ex);
    }
    return possition;
}
 
Example 12
Source File: StrutsEditorUtilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static int writeBean(BaseDocument doc, BaseBean bean, String element, String section) throws IOException{
    int possition = -1;
    boolean addroot = false;
    boolean addsection = false;
    String sBean = addNewLines(bean);
    StringBuffer appendText = new StringBuffer();
    ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport();
    TokenItem token;
    int offset;
    try {
        String docText = doc.getText(0, doc.getLength());
        //Check whether there is root element
        String findString = "</" + element;                             //NOI18N
        
        //find index of last definition 
        offset = docText.lastIndexOf(findString);
        if (offset == -1){
            if ((offset = findEndOfElement(doc, "struts-config")) == -1){                 //NOI18N
                offset = doc.getLength();
                appendText = new StringBuffer();
                appendText.append("<struts-config>");                   //NOI18N
                appendText.append(END_LINE);
                if (section != null && section.length()>0 ){
                    appendText.append("<"+section+">");                 //NOI18N
                    appendText.append(END_LINE);
                    appendText.append(sBean);
                    appendText.append(END_LINE);
                    appendText.append("</"+section+">");                //NOI18N
                }
                else{
                    appendText.append(sBean);
                }
                appendText.append(END_LINE);
                appendText.append("</struts-config>");                  //NOI18N
                appendText.append(END_LINE);
                possition = writeString(doc, appendText.toString(), offset);
            }
            else{
                if (section != null && section.length()>0){
                    int offsetSection;
                    if ((offsetSection = findEndOfElement(doc, section)) == -1){
                        appendText.append("<"+section+">");             //NOI18N
                        appendText.append(END_LINE);
                        appendText.append(sBean);
                        appendText.append(END_LINE);
                        appendText.append("</"+section+">");            //NOI18N
                    }
                    else {
                        appendText.append(sBean);
                        offset = offsetSection;
                    }
                }
                else 
                    appendText.append(sBean);
                token = sup.getTokenChain(offset, offset+1);
                if (token != null) token = token.getPrevious();
                while (token != null
                        && !(token.getTokenID().getNumericID() == XML_ELEMENT
                        && token.getImage().equals(">")))               //NOI18N
                    token = token.getPrevious();
                if (token != null)
                    offset = token.getOffset();
                possition=writeString(doc, appendText.toString(), offset);
            }
        }
        else{
            token = sup.getTokenChain(offset, offset+1);
            if (token != null && token.getTokenID().getNumericID() == XML_ELEMENT){
                while (token != null
                        && !(token.getTokenID().getNumericID() == XML_ELEMENT
                        && token.getImage().equals(">")))               //NOI18N
                    token = token.getPrevious();
                if (token != null)
                    possition = writeString(doc, sBean, token.getOffset());    
            }
        }
    } catch (BadLocationException ex) {
        Exceptions.printStackTrace(ex);
    }        
    return possition;
}
 
Example 13
Source File: JSFEditorUtilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** Writes new bean to the document directly under <faces-config> element
 */
public static int writeBean(BaseDocument doc, BaseBean bean, String element) throws IOException{
    String sBean = addNewLines(bean);
    int possition = -1;
    ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport();
    TokenItem token;
    try {
        String docText = doc.getText(0, doc.getLength());
        //Check whether there is root element
        if (docText.indexOf("<faces-config") == -1){                //NOI18N
            doc.insertString(doc.getLength(), "<faces-config>"      //NOI18N
                    + END_LINE + "</faces-config>", null);          //NOI18N       
            docText = doc.getText(0, doc.getLength());
        }
        String findString = "</" + element;
        
        //find index of last definition 
        int offset = docText.lastIndexOf(findString);
        if (offset == -1)
            offset = docText.length() - 2;
        token = sup.getTokenChain(offset, offset+1);
        if (offset < (docText.length() - 2)
                && token != null && token.getTokenID().getNumericID() == XML_ELEMENT){
            while (token != null
                    && !(token.getTokenID().getNumericID() == XML_ELEMENT
                    && token.getImage().equals(">")))               //NOI18N
                token = token.getNext();
            if (token != null)
                possition = writeString(doc, sBean, token.getOffset());    
        }
        else {
            // write to end
            if (token != null && token.getImage().equals(">"))      //NOI18N
                token = token.getPrevious();
            while (token != null
                    && !(token.getTokenID().getNumericID() == XML_ELEMENT
                    && token.getImage().equals(">")))               //NOI18N
                token = token.getPrevious();
            if (token != null)
                possition = writeString(doc, sBean, token.getOffset());    
        }
    } catch (BadLocationException ex) {
        Exceptions.printStackTrace(ex);
    }
    return possition;
}
 
Example 14
Source File: StrutsConfigHyperlinkProvider.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** This method finds the value for an attribute of element of on the offset.
 * @return Returns null, when the offset is not a value of an attribute. If the there is value
 * of an attribute, then returns String array [element, attribute, value].
 */
private String[] getElementAttrValue(javax.swing.text.Document doc, int offset){
    String attribute = null;
    String tag = null;
    String value = null;
    
    try {
        BaseDocument bdoc = (BaseDocument) doc;
        JTextComponent target = Utilities.getFocusedComponent();
        
        if (target == null || target.getDocument() != bdoc)
            return null;
        
        ExtSyntaxSupport sup = (ExtSyntaxSupport)bdoc.getSyntaxSupport();
        //TokenID tokenID = sup.getTokenID(offset);
        TokenItem token = sup.getTokenChain(offset, offset+1);
        //if (debug) debug ("token: "  +token.getTokenID().getNumericID() + ":" + token.getTokenID().getName());
        // when it's not a value -> do nothing.
        if (token == null || token.getTokenID().getNumericID() != StrutsEditorUtilities.XML_ATTRIBUTE_VALUE)
            return null;
        value = token.getImage();
        if (value != null){
            // value = value.substring(0, offset - token.getOffset());
            //if (debug) debug ("value to cursor: " + value);
            value = value.trim();
            valueOffset = token.getOffset();
            if (value.charAt(0) == '"') {
                value = value.substring(1);
                valueOffset ++;
            }
            
            if (value.length() > 0  && value.charAt(value.length()-1) == '"') value = value.substring(0, value.length()-1);
            value = value.trim();
            //if (debug) debug ("value: " + value);
        }
        
        //if (debug) debug ("Token: " + token);
        // Find attribute and tag
        // 5 - attribute
        // 4 - tag
        while(token != null && token.getTokenID().getNumericID() != StrutsEditorUtilities.XML_ATTRIBUTE
                && token.getTokenID().getNumericID() != StrutsEditorUtilities.XML_ELEMENT)
            token = token.getPrevious();
        if (token != null && token.getTokenID().getNumericID() == StrutsEditorUtilities.XML_ATTRIBUTE){
            attribute = token.getImage();
            while(token != null && token.getTokenID().getNumericID() != StrutsEditorUtilities.XML_ELEMENT)
                token = token.getPrevious();
            if (token != null && token.getTokenID().getNumericID() == StrutsEditorUtilities.XML_ELEMENT)
                tag = token.getImage();
        }
        if (attribute == null || tag == null)
            return null;
        tag = tag.substring(1);
        if (debug) debug("element: " + tag );   // NOI18N
        if (debug) debug("attribute: " + attribute ); //NOI18N
        if (debug) debug("value: " + value );  //NOI18N
        return new String[]{tag, attribute, value};
    } catch (BadLocationException e) {
    }
    return null;
}
 
Example 15
Source File: JSFEditorUtilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static int[] getConverterDefinition(BaseDocument doc, String byElement, String content){
    try{
        String text = doc.getText(0, doc.getLength());
        //find first possition of text that is the ruleName
        int offset = text.indexOf(content);
        int start = 0;
        int end = 0;
        ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport();
        TokenItem token;
        
        while (offset != -1){
            token = sup.getTokenChain(offset, offset+1);
            if (token != null && token.getTokenID().getNumericID() == JSFEditorUtilities.XML_TEXT){
                // find first xml element before the ruleName
                while (token!=null 
                        && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT
                        && !token.getImage().equals(">")))
                    token = token.getPrevious();
                // is it the rule definition?
                if (token != null && token.getImage().equals("<" + byElement)){
                    // find start of the rule definition
                    while (token != null
                            && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT
                            && token.getImage().equals("<converter")))
                        token = token.getPrevious();
                    if(token != null && token.getImage().equals("<converter")){
                        start = token.getOffset();
                        token = sup.getTokenChain(offset, offset+1);
                        // find the end of the rule definition
                        while (token != null
                                && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT
                                && token.getImage().equals("</converter")))
                            token = token.getNext();
                        if (token!=null && token.getImage().equals("</converter")){
                            while (token != null
                                    && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT
                                    && token.getImage().equals(">")))
                                token = token.getNext();
                            if (token!=null && token.getImage().equals(">")){
                                end = token.getOffset()+1;
                                return new int[]{start, end};
                            }
                        }
                        return new int[]{start, text.length()};
                    }
                }
            }
            offset = text.indexOf(content, offset+content.length());
        }
        
    } catch (BadLocationException e) {
        Exceptions.printStackTrace(e);
    } 
    return new int []{-1,-1};
}
 
Example 16
Source File: StrutsConfigHyperlinkProvider.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void findResourcePath(String path, BaseDocument doc){
    path = path.trim();
    if (debug) debug("path: " + path);
    if (path.indexOf('?') > 0){
        // remove query from the path
        path = path.substring(0, path.indexOf('?'));
    }
    WebModule wm = WebModule.getWebModule(NbEditorUtilities.getFileObject(doc));
    if (wm != null){
        FileObject docBase= wm.getDocumentBase();
        FileObject fo = docBase.getFileObject(path);
        if (fo == null){
            // maybe an action
            String servletMapping = StrutsConfigUtilities.getActionServletMapping(wm.getDeploymentDescriptor());
            if (servletMapping != null){
                String actionPath = null;
                if (servletMapping != null && servletMapping.lastIndexOf('.')>0){
                    // the mapping is in *.xx way
                    String extension = servletMapping.substring(servletMapping.lastIndexOf('.'));
                    if (path.endsWith(extension))
                        actionPath = path.substring(0, path.length()-extension.length());
                    else
                        actionPath = path;
                } else{
                    // the mapping is /xx/* way
                    servletMapping = servletMapping.trim();
                    String prefix = servletMapping.substring(0, servletMapping.length()-2);
                    if (path.startsWith(prefix))
                        actionPath = path.substring(prefix.length(), path.length());
                    else
                        actionPath = path;
                }
                if (debug) debug(" actionPath: " + actionPath);
                if(actionPath != null){
                    ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport();
                    int offset = findDefinitionInSection(sup, "action-mappings","action","path", actionPath);
                    if (offset > 0){
                        JTextComponent target = Utilities.getFocusedComponent();
                        target.setCaretPosition(offset);
                    }
                }
            }
        } else
            openInEditor(fo);
    }
}
 
Example 17
Source File: JSFEditorUtilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static int[] getManagedBeanDefinition(BaseDocument doc, String byElement, String content){
    try{
        String text = doc.getText(0, doc.getLength());
        int offset = text.indexOf(content);
        int start = 0;
        int end = 0;
        ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport();
        TokenItem token;
        
        while (offset != -1){
            token = sup.getTokenChain(offset, offset+1);
            if (token != null && token.getTokenID().getNumericID() == JSFEditorUtilities.XML_TEXT){
                while (token!=null 
                        && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT
                        && !token.getImage().equals(">")))
                    token = token.getPrevious();
                if (token != null && token.getImage().equals("<" + byElement)){    //NOI18N
                    while (token != null
                            && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT
                            && token.getImage().equals("<managed-bean")))
                        token = token.getPrevious();
                    if(token != null && token.getImage().equals("<managed-bean")){
                        start = token.getOffset();
                        token = sup.getTokenChain(offset, offset+1);
                        while (token != null
                                && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT
                                && token.getImage().equals("</managed-bean")))
                            token = token.getNext();
                        if (token!=null && token.getImage().equals("</managed-bean")){
                            while (token != null
                                    && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT
                                    && token.getImage().equals(">")))
                                token = token.getNext();
                            if (token!=null && token.getImage().equals(">")){
                                end = token.getOffset()+1;
                                return new int[]{start, end};
                            }
                        }
                        return new int[]{start, text.length()};
                    }
                }
            }
            offset = text.indexOf(content, offset+content.length());
        }
    }
    catch (BadLocationException e) {
        Exceptions.printStackTrace(e);
    } 
    return new int []{-1,-1};
}
 
Example 18
Source File: AbstractTestCase.java    From netbeans with Apache License 2.0 4 votes vote down vote up
protected XMLSyntaxSupport getSyntaxSupport(String path) throws Exception {
    BaseDocument doc = getResourceAsDocument(path);
    //must set the language inside unit tests
    doc.putProperty(Language.class, XMLTokenId.language());
    return ((XMLSyntaxSupport)doc.getSyntaxSupport());
}
 
Example 19
Source File: JSFEditorUtilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static int[] getNavigationRuleDefinition(BaseDocument doc, String ruleName){
    try{
        String text = doc.getText(0, doc.getLength());
        //find first possition of text that is the ruleName
        int offset = text.indexOf(ruleName);
        int start = 0;
        int end = 0;
        ExtSyntaxSupport sup = (ExtSyntaxSupport)doc.getSyntaxSupport();
        TokenItem token;
        
        while (offset != -1){
            token = sup.getTokenChain(offset, offset+1);
            if (token != null && token.getTokenID().getNumericID() == JSFEditorUtilities.XML_TEXT){
                // find first xml element before the ruleName
                while (token!=null 
                        && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT
                        && !token.getImage().equals(">")))
                    token = token.getPrevious();
                // is it the rule definition?
                if (token != null && token.getImage().equals("<from-view-id")){
                    // find start of the rule definition
                    while (token != null
                            && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT
                            && token.getImage().equals("<navigation-rule")))
                        token = token.getPrevious();
                    if(token != null && token.getImage().equals("<navigation-rule")){
                        start = token.getOffset();
                        token = sup.getTokenChain(offset, offset+1);
                        // find the end of the rule definition
                        while (token != null
                                && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT
                                && token.getImage().equals("</navigation-rule")))
                            token = token.getNext();
                        if (token!=null && token.getImage().equals("</navigation-rule")){
                            while (token != null
                                    && !(token.getTokenID().getNumericID() == JSFEditorUtilities.XML_ELEMENT
                                    && token.getImage().equals(">")))
                                token = token.getNext();
                            if (token!=null && token.getImage().equals(">")){
                                end = token.getOffset()+1;
                                return new int[]{start, end};
                            }
                        }
                        return new int[]{start, text.length()};
                    }
                }
            }
            offset = text.indexOf(ruleName, offset+ruleName.length());
        }
    } catch (BadLocationException e) {
        Exceptions.printStackTrace(e);
    } 
    return new int []{-1,-1};
}
 
Example 20
Source File: JavaElementRefFinder.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public JavaElementRefFinder(DocumentAccess docAccess) {
    this.docAccess = docAccess;
    BaseDocument document = (BaseDocument)docAccess.getDocument();
    syntaxSupport = document.getSyntaxSupport();
}