sdedic commented on a change in pull request #481: [NETBEANS-290] Provided 
support for hyperlink on dependencies.
URL: https://github.com/apache/incubator-netbeans/pull/481#discussion_r179222209
 
 

 ##########
 File path: 
maven.grammar/src/org/netbeans/modules/maven/hyperlinks/HyperlinkProviderImpl.java
 ##########
 @@ -453,5 +215,331 @@ private FileObject getPath(FileObject parent, String 
path) {
         }
         return parent.getFileObject(path);
     }
+    
+    private class PomParserRunnable implements Runnable {
+
+        private final PomHyperlinkInfo hyperLinkInfo;
+        private final Document document;
+        private final int offset;
+
+        public PomParserRunnable(PomHyperlinkInfo hyperLinkInfo, Document 
document, int offset) {
+            this.hyperLinkInfo = hyperLinkInfo;
+            this.document = document;
+            this.offset = offset;
+        }
+        
+        @Override
+        public void run() {
+            TokenHierarchy th = TokenHierarchy.get(document);
+            TokenSequence<XMLTokenId> xml = 
th.tokenSequence(XMLTokenId.language());
+            xml.move(offset);
+            xml.moveNext();
+            Token<XMLTokenId> token = xml.token();
+
+            // when it's not a value -> do nothing.
+            if (token == null) {
+                return;
+            }
+
+            if (token.id() == XMLTokenId.TEXT) {
+                hyperLinkInfo.calculateInfo(token, xml);
+            }
+        }   
+    }
 
+    private class PomHyperlinkInfo {
+        final Document doc;
+        final int documentOffset;
+        final FileObject projectFileObject;
+        boolean isText;
+        int ftokenOff;
+        String ftext;
+        
+        String artifactId;
+        String groupId;
+        String version;
+        String type;
+
+        public PomHyperlinkInfo(Document doc, int documentOffset) {
+            this.doc = doc;
+            this.documentOffset = documentOffset;
+            this.projectFileObject = getProjectDir(doc);
+        }
+        
+        boolean isHyperlinkUrl() {
+            return ftext != null &&
+                   (ftext.startsWith("http://";) || //NOI18N
+                   ftext.startsWith("https://";)); //NOI18N;
+        }
+        
+        private boolean isFileSystemLink() {
+            FileObject fo = getProjectDir(doc);
+            return (fo != null && ftext != null) && getPath(fo, ftext) != null;
+        }
+        
+        boolean isMavenProperty() {
+            if (ftext != null) {
+                int ff = documentOffset - ftokenOff;
+                if (ff > -1 && ff < ftext.length()) {
+                    String before = ftext.substring(0, ff);
+                    String after = ftext.substring(ff, ftext.length());
+                    int bo = before.lastIndexOf("${");//NOI18N
+                    int bc = before.lastIndexOf("}");//NOI18N
+                    int ao = after.indexOf("${");//NOI18N
+                    int ac = after.indexOf("}");//NOI18N
+                    if (bo > bc && ac > -1 && (ac < ao || ao == -1)) {
+                        return true;
+                    }
+                }
+            }
+            return false;
+        }
+        
+        boolean isMavenDependency() {
+            return artifactId != null && groupId != null && version != null;
+        }
+        
+        boolean isHyperlinkPoint() {
+            return (isHyperlinkUrl() || isFileSystemLink() || 
isMavenProperty() || isMavenDependency());
+        }
+        
+        private void calculateInfo(Token<XMLTokenId> token, 
TokenSequence<XMLTokenId> xml) {
+            isText = token.id() == XMLTokenId.TEXT;
+            if (isText) {
+                ftokenOff = xml.offset();
+                ftext = token.text().toString();
+                
+                if (projectFileObject != null && getPath(projectFileObject, 
ftext) != null) {
+                    xml.movePrevious();
+                    token = xml.token();
+                    if (token != null && token.id().equals(XMLTokenId.TAG) && 
TokenUtilities.equals(token.text(), ">")) {//NOI18N
+                        xml.movePrevious();
+                        token = xml.token();
+                        if (token != null && 
token.id().equals(XMLTokenId.TAG)) {
+                            if (TokenUtilities.equals(token.text(), 
"<module")) {//NOI18N
+                                if (!ftext.endsWith("/pom.xml")) {
+                                    ftext += "/pom.xml"; //NOI18N
+                                }
+                            }
+                        }
+                    }
+                } else {
+                    xml.movePrevious();
+                    token = xml.token();
+                    if (token != null && token.id().equals(XMLTokenId.TAG) && 
TokenUtilities.equals(token.text(), ">")) { //NOI18N
+                        xml.movePrevious();
+                        token = xml.token();
+                        if (TokenUtilities.equals(token.text(), "<artifactId") 
|| //NOI18N
+                            TokenUtilities.equals(token.text(), "<groupId") || 
//NOI18N
+                            TokenUtilities.equals(token.text(), "<type") || 
//NOI18N
+                            (TokenUtilities.equals(token.text(), "<version") 
&& !ftext.startsWith("${"))) { //NOI18N
+                            resetSequenceToDependencyTagToken(xml);
+                            if (TokenUtilities.equals(xml.token().text(), 
"<dependency")) {
+                                
while(!TokenUtilities.equals(xml.token().text(), "</dependency")) { //NOI18N
+                                    xml.moveNext();
+                                    token = xml.token();
+                                    if (TokenUtilities.equals(token.text(), 
"<artifactId")) { //NOI18N
+                                        xml.moveNext();
+                                        xml.moveNext();
+                                        token = xml.token();
+                                        artifactId = token.text().toString();
+                                        xml.moveNext();
+                                    } else if 
(TokenUtilities.equals(token.text(), "<groupId")) { //NOI18N
 
 Review comment:
   I am not sure if maintaining token.text() as CharSequence is worth the 
trouble: Maybe 
   switch(token.text().toString()) could be more readable. XML tokens are 
rather short

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@netbeans.apache.org
For additional commands, e-mail: notifications-h...@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to