1 package org.hibernate.eclipse.mapper.editors; 2 3 import org.eclipse.jdt.core.IJavaElement; 4 import org.eclipse.jdt.core.IJavaProject; 5 import org.eclipse.jface.text.Assert; 6 import org.eclipse.jface.text.IDocument; 7 import org.eclipse.jface.text.IRegion; 8 import org.eclipse.jface.text.ITextViewer; 9 import org.eclipse.jface.text.Region; 10 import org.eclipse.jface.text.hyperlink.IHyperlink; 11 import org.eclipse.jface.text.hyperlink.IHyperlinkDetector; 12 import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; 13 import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion; 14 import org.eclipse.wst.sse.core.internal.provisional.StructuredModelManager; 15 import org.eclipse.wst.sse.core.internal.util.StringUtils; 16 import org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr; 17 import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode; 18 import org.hibernate.eclipse.mapper.extractor.HBMInfoExtractor; 19 import org.hibernate.eclipse.mapper.extractor.HBMInfoHandler; 20 import org.w3c.dom.Attr ; 21 import org.w3c.dom.NamedNodeMap ; 22 import org.w3c.dom.Node ; 23 24 25 28 public class HBMXMLHyperlinkDetector implements IHyperlinkDetector { 29 30 HBMInfoExtractor infoExtractor = new HBMInfoExtractor(); 31 32 private final IJavaProject project; 33 34 40 public HBMXMLHyperlinkDetector(IJavaProject project) { 41 Assert.isNotNull(project); 42 this.project = project; 43 } 44 45 48 public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) { 49 if (region == null || textViewer == null) { 50 return null; 51 } 52 53 IDocument document = textViewer.getDocument(); 54 Node currentNode = getCurrentNode(document, region.getOffset()); 55 if (currentNode != null) { 56 57 short nodeType = currentNode.getNodeType(); 58 if(nodeType == Node.ATTRIBUTE_NODE) { 59 60 } else if (nodeType == Node.ELEMENT_NODE){ 61 62 Attr currentAttrNode = getCurrentAttrNode(currentNode, region.getOffset()); 63 64 if(currentAttrNode!=null) { 65 String path = currentNode.getNodeName() + ">" + currentAttrNode.getName(); 66 HBMInfoHandler handler = infoExtractor.getAttributeHandler(path); 67 if(handler!=null) { 68 IJavaElement element = handler.getJavaElement(project, currentNode, currentAttrNode); 69 if(element!=null) { 70 return new IHyperlink[] {new HBMXMLHyperlink(getHyperlinkRegion(currentAttrNode), element)}; 71 } else { 72 return null; 73 } 74 75 } 76 } 77 } 78 } 79 80 return null; 81 82 } 83 84 private IRegion getHyperlinkRegion(Node node) { 85 IRegion hyperRegion = null; 86 87 if (node != null) { 88 short nodeType = node.getNodeType(); 89 if (nodeType == Node.DOCUMENT_TYPE_NODE) { 90 IDOMNode docNode = (IDOMNode) node; 92 hyperRegion = new Region(docNode.getStartOffset(), docNode.getEndOffset() - docNode.getStartOffset()); 93 } 94 else if (nodeType == Node.ATTRIBUTE_NODE) { 95 IDOMAttr att = (IDOMAttr) node; 97 int regOffset = att.getValueRegionStartOffset(); 99 int regLength = att.getValueRegion().getTextLength(); 100 String attValue = att.getValueRegionText(); 101 if (StringUtils.isQuoted(attValue)) { 102 regOffset = ++regOffset; 103 regLength = regLength - 2; 104 } 105 hyperRegion = new Region(regOffset, regLength); 106 } 107 } 108 return hyperRegion; 109 } 110 111 private Attr getCurrentAttrNode(Node node, int offset) { 112 if ((node instanceof IndexedRegion) && ((IndexedRegion) node).contains(offset) && (node.hasAttributes())) { 113 NamedNodeMap attrs = node.getAttributes(); 114 for (int i = 0; i < attrs.getLength(); ++i) { 117 IndexedRegion attRegion = (IndexedRegion) attrs.item(i); 120 if (attRegion.contains(offset)) { 121 return (Attr) attrs.item(i); 122 } 123 } 124 } 125 return null; 126 } 127 128 129 136 private Node getCurrentNode(IDocument document, int offset) { 137 IndexedRegion inode = null; 140 IStructuredModel sModel = null; 141 try { 142 sModel = StructuredModelManager.getModelManager().getExistingModelForRead(document); 143 inode = sModel.getIndexedRegion(offset); 144 if (inode == null) 145 inode = sModel.getIndexedRegion(offset - 1); 146 } 147 finally { 148 if (sModel != null) 149 sModel.releaseFromRead(); 150 } 151 152 if (inode instanceof Node) { 153 return (Node) inode; 154 } 155 return null; 156 } 157 } 158 | Popular Tags |