bchilds | 25637a6 | 2008-04-30 19:06:32 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2007 IBM Corporation and others. |
| 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | *******************************************************************************/ |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 11 | package org.eclipse.wst.jsdt.web.ui.internal.hyperlink; |
| 12 | |
| 13 | import java.io.File; |
| 14 | import java.net.URI; |
| 15 | import java.util.ArrayList; |
| 16 | import java.util.List; |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 17 | |
| 18 | import org.eclipse.core.resources.IFile; |
| 19 | import org.eclipse.core.resources.ResourcesPlugin; |
| 20 | import org.eclipse.core.runtime.IPath; |
| 21 | import org.eclipse.core.runtime.Path; |
| 22 | import org.eclipse.jface.text.IDocument; |
| 23 | import org.eclipse.jface.text.IRegion; |
| 24 | import org.eclipse.jface.text.ITextViewer; |
| 25 | import org.eclipse.jface.text.Region; |
| 26 | import org.eclipse.jface.text.hyperlink.IHyperlink; |
| 27 | import org.eclipse.jface.text.hyperlink.IHyperlinkDetector; |
| 28 | import org.eclipse.jface.text.hyperlink.URLHyperlink; |
| 29 | import org.eclipse.wst.common.uriresolver.internal.provisional.URIResolverPlugin; |
| 30 | import org.eclipse.wst.sse.core.StructuredModelManager; |
| 31 | import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; |
| 32 | import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion; |
| 33 | import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion; |
| 34 | import org.eclipse.wst.sse.core.utils.StringUtils; |
| 35 | import org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration; |
| 36 | import org.eclipse.wst.xml.core.internal.contentmodel.CMDataType; |
| 37 | import org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration; |
| 38 | import org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery; |
| 39 | import org.eclipse.wst.xml.core.internal.contentmodel.util.DOMNamespaceHelper; |
| 40 | import org.eclipse.wst.xml.core.internal.modelquery.ModelQueryUtil; |
| 41 | import org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr; |
| 42 | import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode; |
| 43 | import org.w3c.dom.Attr; |
| 44 | import org.w3c.dom.DocumentType; |
| 45 | import org.w3c.dom.Element; |
| 46 | import org.w3c.dom.NamedNodeMap; |
| 47 | import org.w3c.dom.Node; |
| 48 | |
bchilds | 133806a | 2007-03-27 15:52:39 +0000 | [diff] [blame] | 49 | import com.ibm.icu.util.StringTokenizer; |
| 50 | |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 51 | /** |
bchilds | 25637a6 | 2008-04-30 19:06:32 +0000 | [diff] [blame] | 52 | * |
| 53 | |
| 54 | * Provisional API: This class/interface is part of an interim API that is still under development and expected to |
| 55 | * change significantly before reaching stability. It is being made available at this early stage to solicit feedback |
| 56 | * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken |
| 57 | * (repeatedly) as the API evolves. |
| 58 | */ |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 59 | public class XMLHyperlinkDetector implements IHyperlinkDetector { |
| 60 | // copies of this class exist in: |
| 61 | // org.eclipse.wst.xml.ui.internal.hyperlink |
| 62 | // org.eclipse.wst.html.ui.internal.hyperlink |
| 63 | // org.eclipse.wst.jsdt.web.ui.internal.hyperlink |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 64 | private final String HTTP_PROTOCOL = "http://";//$NON-NLS-1$ |
| 65 | private final String NO_NAMESPACE_SCHEMA_LOCATION = "noNamespaceSchemaLocation"; //$NON-NLS-1$ |
| 66 | private final String SCHEMA_LOCATION = "schemaLocation"; //$NON-NLS-1$ |
| 67 | private final String XMLNS = "xmlns"; //$NON-NLS-1$ |
| 68 | private final String XSI_NAMESPACE_URI = "http://www.w3.org/2001/XMLSchema-instance"; //$NON-NLS-1$ |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 69 | |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 70 | /** |
| 71 | * Create the appropriate hyperlink |
| 72 | * |
| 73 | * @param uriString |
| 74 | * @param hyperlinkRegion |
| 75 | * @return IHyperlink |
| 76 | */ |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 77 | private IHyperlink createHyperlink(String uriString, IRegion hyperlinkRegion, IDocument document, Node node) { |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 78 | IHyperlink link = null; |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 79 | if (isHttp(uriString)) { |
| 80 | link = new URLHyperlink(hyperlinkRegion, uriString); |
| 81 | } else { |
| 82 | // try to locate the file in the workspace |
| 83 | File systemFile = getFileFromUriString(uriString); |
| 84 | if (systemFile != null) { |
| 85 | String systemPath = systemFile.getPath(); |
| 86 | IFile file = getFile(systemPath); |
| 87 | if (file != null) { |
| 88 | // this is a WorkspaceFileHyperlink since file exists in |
| 89 | // workspace |
| 90 | link = new WorkspaceFileHyperlink(hyperlinkRegion, file); |
| 91 | } else { |
| 92 | // this is an ExternalFileHyperlink since file does not |
| 93 | // exist in workspace |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 94 | link = new ExternalFileHyperlink(hyperlinkRegion, systemFile); |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | } |
| 98 | return link; |
| 99 | } |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 100 | |
| 101 | public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) { |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 102 | // for now, only capable of creating 1 hyperlink |
| 103 | List hyperlinks = new ArrayList(0); |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 104 | if (region != null && textViewer != null) { |
| 105 | IDocument document = textViewer.getDocument(); |
| 106 | Node currentNode = getCurrentNode(document, region.getOffset()); |
| 107 | if (currentNode != null) { |
| 108 | String uriString = null; |
| 109 | if (currentNode.getNodeType() == Node.DOCUMENT_TYPE_NODE) { |
| 110 | // doctype nodes |
| 111 | uriString = getURIString(currentNode, document); |
| 112 | } else if (currentNode.getNodeType() == Node.ELEMENT_NODE) { |
| 113 | // element nodes |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 114 | Attr currentAttr = getCurrentAttrNode(currentNode, region.getOffset()); |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 115 | if (currentAttr != null) { |
| 116 | // try to find link for current attribute |
| 117 | // resolve attribute value |
| 118 | uriString = getURIString(currentAttr, document); |
| 119 | // verify validity of uri string |
| 120 | if (uriString == null || !isValidURI(uriString)) { |
| 121 | // reset current attribute |
| 122 | currentAttr = null; |
| 123 | } |
| 124 | } |
| 125 | if (currentAttr == null) { |
| 126 | // try to find a linkable attribute within element |
| 127 | currentAttr = getLinkableAttr((Element) currentNode); |
| 128 | if (currentAttr != null) { |
| 129 | uriString = getURIString(currentAttr, document); |
| 130 | } |
| 131 | } |
| 132 | currentNode = currentAttr; |
| 133 | } |
| 134 | // try to create hyperlink from information gathered |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 135 | if (uriString != null && currentNode != null && isValidURI(uriString)) { |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 136 | IRegion hyperlinkRegion = getHyperlinkRegion(currentNode); |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 137 | IHyperlink hyperlink = createHyperlink(uriString, hyperlinkRegion, document, currentNode); |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 138 | if (hyperlink != null) { |
| 139 | hyperlinks.add(hyperlink); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | if (hyperlinks.size() == 0) { |
| 145 | return null; |
| 146 | } |
| 147 | return (IHyperlink[]) hyperlinks.toArray(new IHyperlink[0]); |
| 148 | } |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 149 | |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 150 | /** |
| 151 | * Get the base location from the current model (local file system) |
| 152 | */ |
| 153 | private String getBaseLocation(IDocument document) { |
| 154 | String baseLoc = null; |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 155 | // get the base location from the current model |
| 156 | IStructuredModel sModel = null; |
| 157 | try { |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 158 | sModel = StructuredModelManager.getModelManager().getExistingModelForRead(document); |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 159 | if (sModel != null) { |
| 160 | IPath location = new Path(sModel.getBaseLocation()); |
| 161 | if (location.toFile().exists()) { |
| 162 | baseLoc = location.toString(); |
| 163 | } else { |
| 164 | if (location.segmentCount() > 1) { |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 165 | baseLoc = ResourcesPlugin.getWorkspace().getRoot().getFile(location).getLocation().toString(); |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 166 | } else { |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 167 | baseLoc = ResourcesPlugin.getWorkspace().getRoot().getLocation().append(location).toString(); |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | } |
| 171 | } finally { |
| 172 | if (sModel != null) { |
| 173 | sModel.releaseFromRead(); |
| 174 | } |
| 175 | } |
| 176 | return baseLoc; |
| 177 | } |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 178 | |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 179 | /** |
| 180 | * Get the CMElementDeclaration for an element |
| 181 | * |
| 182 | * @param element |
| 183 | * @return CMElementDeclaration |
| 184 | */ |
| 185 | private CMElementDeclaration getCMElementDeclaration(Element element) { |
| 186 | CMElementDeclaration ed = null; |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 187 | ModelQuery mq = ModelQueryUtil.getModelQuery(element.getOwnerDocument()); |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 188 | if (mq != null) { |
| 189 | ed = mq.getCMElementDeclaration(element); |
| 190 | } |
| 191 | return ed; |
| 192 | } |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 193 | |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 194 | /** |
| 195 | * Returns the attribute node within node at offset |
| 196 | * |
| 197 | * @param node |
| 198 | * @param offset |
| 199 | * @return Attr |
| 200 | */ |
| 201 | private Attr getCurrentAttrNode(Node node, int offset) { |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 202 | if ((node instanceof IndexedRegion) && ((IndexedRegion) node).contains(offset) && (node.hasAttributes())) { |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 203 | NamedNodeMap attrs = node.getAttributes(); |
| 204 | // go through each attribute in node and if attribute contains |
| 205 | // offset, return that attribute |
| 206 | for (int i = 0; i < attrs.getLength(); ++i) { |
| 207 | // assumption that if parent node is of type IndexedRegion, |
| 208 | // then its attributes will also be of type IndexedRegion |
| 209 | IndexedRegion attRegion = (IndexedRegion) attrs.item(i); |
| 210 | if (attRegion.contains(offset)) { |
| 211 | return (Attr) attrs.item(i); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | return null; |
| 216 | } |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 217 | |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 218 | /** |
| 219 | * Returns the node the cursor is currently on in the document. null if no |
| 220 | * node is selected |
| 221 | * |
| 222 | * @param offset |
| 223 | * @return Node either element, doctype, text, or null |
| 224 | */ |
| 225 | private Node getCurrentNode(IDocument document, int offset) { |
| 226 | // get the current node at the offset (returns either: element, |
| 227 | // doctype, text) |
| 228 | IndexedRegion inode = null; |
| 229 | IStructuredModel sModel = null; |
| 230 | try { |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 231 | sModel = StructuredModelManager.getModelManager().getExistingModelForRead(document); |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 232 | inode = sModel.getIndexedRegion(offset); |
| 233 | if (inode == null) { |
| 234 | inode = sModel.getIndexedRegion(offset - 1); |
| 235 | } |
| 236 | } finally { |
| 237 | if (sModel != null) { |
| 238 | sModel.releaseFromRead(); |
| 239 | } |
| 240 | } |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 241 | if (inode instanceof Node) { |
| 242 | return (Node) inode; |
| 243 | } |
| 244 | return null; |
| 245 | } |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 246 | |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 247 | /** |
| 248 | * Returns an IFile from the given uri if possible, null if cannot find file |
| 249 | * from uri. |
| 250 | * |
| 251 | * @param fileString |
| 252 | * file system path |
| 253 | * @return returns IFile if fileString exists in the workspace |
| 254 | */ |
| 255 | private IFile getFile(String fileString) { |
| 256 | IFile file = null; |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 257 | if (fileString != null) { |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 258 | IFile[] files = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(new Path(fileString)); |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 259 | for (int i = 0; i < files.length && file == null; i++) { |
| 260 | if (files[i].exists()) { |
| 261 | file = files[i]; |
| 262 | } |
| 263 | } |
| 264 | } |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 265 | return file; |
| 266 | } |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 267 | |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 268 | /** |
| 269 | * Create a file from the given uri string |
| 270 | * |
| 271 | * @param uriString - |
| 272 | * assumes uriString is not http:// |
| 273 | * @return File created from uriString if possible, null otherwise |
| 274 | */ |
| 275 | private File getFileFromUriString(String uriString) { |
| 276 | File file = null; |
| 277 | try { |
| 278 | // first just try to create a file directly from uriString as |
| 279 | // default in case create file from uri does not work |
| 280 | file = new File(uriString); |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 281 | // try to create file from uri |
| 282 | URI uri = new URI(uriString); |
| 283 | file = new File(uri); |
| 284 | } catch (Exception e) { |
| 285 | // if exception is thrown while trying to create File just ignore |
| 286 | // and file will be null |
| 287 | } |
| 288 | return file; |
| 289 | } |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 290 | |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 291 | private IRegion getHyperlinkRegion(Node node) { |
| 292 | IRegion hyperRegion = null; |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 293 | if (node != null) { |
| 294 | short nodeType = node.getNodeType(); |
| 295 | if (nodeType == Node.DOCUMENT_TYPE_NODE) { |
| 296 | // handle doc type node |
| 297 | IDOMNode docNode = (IDOMNode) node; |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 298 | hyperRegion = new Region(docNode.getStartOffset(), docNode.getEndOffset() - docNode.getStartOffset()); |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 299 | } else if (nodeType == Node.ATTRIBUTE_NODE) { |
| 300 | // handle attribute nodes |
| 301 | IDOMAttr att = (IDOMAttr) node; |
| 302 | // do not include quotes in attribute value region |
| 303 | int regOffset = att.getValueRegionStartOffset(); |
| 304 | ITextRegion valueRegion = att.getValueRegion(); |
| 305 | if (valueRegion != null) { |
| 306 | int regLength = valueRegion.getTextLength(); |
| 307 | String attValue = att.getValueRegionText(); |
| 308 | if (StringUtils.isQuoted(attValue)) { |
| 309 | ++regOffset; |
| 310 | regLength = regLength - 2; |
| 311 | } |
| 312 | hyperRegion = new Region(regOffset, regLength); |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | return hyperRegion; |
| 317 | } |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 318 | |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 319 | /** |
| 320 | * Attempts to find an attribute within element that is openable. |
| 321 | * |
| 322 | * @param element - |
| 323 | * cannot be null |
| 324 | * @return Attr attribute that can be used for open on, null if no attribute |
| 325 | * could be found |
| 326 | */ |
| 327 | private Attr getLinkableAttr(Element element) { |
| 328 | CMElementDeclaration ed = getCMElementDeclaration(element); |
| 329 | // get the list of attributes for this node |
| 330 | NamedNodeMap attrs = element.getAttributes(); |
| 331 | for (int i = 0; i < attrs.getLength(); ++i) { |
| 332 | // check if this attribute is "openOn-able" |
| 333 | Attr att = (Attr) attrs.item(i); |
| 334 | if (isLinkableAttr(att, ed)) { |
| 335 | return att; |
| 336 | } |
| 337 | } |
| 338 | return null; |
| 339 | } |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 340 | |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 341 | /** |
| 342 | * Find the location hint for the given namespaceURI if it exists |
| 343 | * |
| 344 | * @param elementNode - |
| 345 | * cannot be null |
| 346 | * @param namespaceURI - |
| 347 | * cannot be null |
| 348 | * @return location hint (systemId) if it was found, null otherwise |
| 349 | */ |
| 350 | private String getLocationHint(Element elementNode, String namespaceURI) { |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 351 | Attr schemaLocNode = elementNode.getAttributeNodeNS(XSI_NAMESPACE_URI, SCHEMA_LOCATION); |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 352 | if (schemaLocNode != null) { |
| 353 | StringTokenizer st = new StringTokenizer(schemaLocNode.getValue()); |
| 354 | while (st.hasMoreTokens()) { |
| 355 | String publicId = st.hasMoreTokens() ? st.nextToken() : null; |
| 356 | String systemId = st.hasMoreTokens() ? st.nextToken() : null; |
| 357 | // found location hint |
| 358 | if (namespaceURI.equalsIgnoreCase(publicId)) { |
| 359 | return systemId; |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | return null; |
| 364 | } |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 365 | |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 366 | /** |
| 367 | * Returns the URI string |
| 368 | * |
| 369 | * @param node - |
| 370 | * assumes not null |
| 371 | */ |
| 372 | private String getURIString(Node node, IDocument document) { |
| 373 | String resolvedURI = null; |
| 374 | // need the base location, publicId, and systemId for URIResolver |
| 375 | String baseLoc = null; |
| 376 | String publicId = null; |
| 377 | String systemId = null; |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 378 | short nodeType = node.getNodeType(); |
| 379 | // handle doc type node |
| 380 | if (nodeType == Node.DOCUMENT_TYPE_NODE) { |
| 381 | baseLoc = getBaseLocation(document); |
| 382 | publicId = ((DocumentType) node).getPublicId(); |
| 383 | systemId = ((DocumentType) node).getSystemId(); |
| 384 | } else if (nodeType == Node.ATTRIBUTE_NODE) { |
| 385 | // handle attribute node |
| 386 | Attr attrNode = (Attr) node; |
| 387 | String attrName = attrNode.getName(); |
| 388 | String attrValue = attrNode.getValue(); |
| 389 | attrValue = StringUtils.strip(attrValue); |
| 390 | if (attrValue != null && attrValue.length() > 0) { |
| 391 | baseLoc = getBaseLocation(document); |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 392 | // handle schemaLocation attribute |
| 393 | String prefix = DOMNamespaceHelper.getPrefix(attrName); |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 394 | String unprefixedName = DOMNamespaceHelper.getUnprefixedName(attrName); |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 395 | if ((XMLNS.equals(prefix)) || (XMLNS.equals(unprefixedName))) { |
| 396 | publicId = attrValue; |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 397 | systemId = getLocationHint(attrNode.getOwnerElement(), publicId); |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 398 | if (systemId == null) { |
| 399 | systemId = attrValue; |
| 400 | } |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 401 | } else if ((XSI_NAMESPACE_URI.equals(DOMNamespaceHelper.getNamespaceURI(attrNode))) && (SCHEMA_LOCATION.equals(unprefixedName))) { |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 402 | // for now just use the first pair |
| 403 | // need to look into being more precise |
| 404 | StringTokenizer st = new StringTokenizer(attrValue); |
| 405 | publicId = st.hasMoreTokens() ? st.nextToken() : null; |
| 406 | systemId = st.hasMoreTokens() ? st.nextToken() : null; |
| 407 | // else check if xmlns publicId = value |
| 408 | } else { |
| 409 | systemId = attrValue; |
| 410 | } |
| 411 | } |
| 412 | } |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 413 | resolvedURI = resolveURI(baseLoc, publicId, systemId); |
| 414 | return resolvedURI; |
| 415 | } |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 416 | |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 417 | /** |
| 418 | * Returns true if this uriString is an http string |
| 419 | * |
| 420 | * @param uriString |
| 421 | * @return true if uriString is http string, false otherwise |
| 422 | */ |
| 423 | private boolean isHttp(String uriString) { |
| 424 | boolean isHttp = false; |
| 425 | if (uriString != null) { |
| 426 | String tempString = uriString.toLowerCase(); |
| 427 | if (tempString.startsWith(HTTP_PROTOCOL)) { |
| 428 | isHttp = true; |
| 429 | } |
| 430 | } |
| 431 | return isHttp; |
| 432 | } |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 433 | |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 434 | /** |
| 435 | * Checks to see if the given attribute is openable. Attribute is openable |
| 436 | * if it is a namespace declaration attribute or if the attribute value is |
| 437 | * of type URI. |
| 438 | * |
| 439 | * @param attr |
| 440 | * cannot be null |
| 441 | * @param cmElement |
| 442 | * CMElementDeclaration associated with the attribute (can be |
| 443 | * null) |
| 444 | * @return true if this attribute is "openOn-able" false otherwise |
| 445 | */ |
| 446 | private boolean isLinkableAttr(Attr attr, CMElementDeclaration cmElement) { |
| 447 | String attrName = attr.getName(); |
| 448 | String prefix = DOMNamespaceHelper.getPrefix(attrName); |
| 449 | String unprefixedName = DOMNamespaceHelper.getUnprefixedName(attrName); |
| 450 | // determine if attribute is namespace declaration |
| 451 | if ((XMLNS.equals(prefix)) || (XMLNS.equals(unprefixedName))) { |
| 452 | return true; |
| 453 | } |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 454 | // determine if attribute contains schema location |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 455 | if ((XSI_NAMESPACE_URI.equals(DOMNamespaceHelper.getNamespaceURI(attr))) && ((SCHEMA_LOCATION.equals(unprefixedName)) || (NO_NAMESPACE_SCHEMA_LOCATION.equals(unprefixedName)))) { |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 456 | return true; |
| 457 | } |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 458 | // determine if attribute value is of type URI |
| 459 | if (cmElement != null) { |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 460 | CMAttributeDeclaration attrDecl = (CMAttributeDeclaration) cmElement.getAttributes().getNamedItem(attrName); |
| 461 | if ((attrDecl != null) && (attrDecl.getAttrType() != null) && (CMDataType.URI.equals(attrDecl.getAttrType().getDataTypeName()))) { |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 462 | return true; |
| 463 | } |
| 464 | } |
| 465 | return false; |
| 466 | } |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 467 | |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 468 | /** |
| 469 | * Checks whether the given uriString is really pointing to a file |
| 470 | * |
| 471 | * @param uriString |
| 472 | * @return boolean |
| 473 | */ |
| 474 | private boolean isValidURI(String uriString) { |
| 475 | boolean isValid = false; |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 476 | if (isHttp(uriString)) { |
| 477 | isValid = true; |
| 478 | } else { |
| 479 | File file = getFileFromUriString(uriString); |
| 480 | if (file != null) { |
| 481 | isValid = file.isFile(); |
| 482 | } |
| 483 | } |
| 484 | return isValid; |
| 485 | } |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 486 | |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 487 | /** |
| 488 | * Resolves the given URI information |
| 489 | * |
| 490 | * @param baseLocation |
| 491 | * @param publicId |
| 492 | * @param systemId |
| 493 | * @return String resolved uri. |
| 494 | */ |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 495 | private String resolveURI(String baseLocation, String publicId, String systemId) { |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 496 | // dont resolve if there's nothing to resolve |
| 497 | if ((baseLocation == null) && (publicId == null) && (systemId == null)) { |
| 498 | return null; |
| 499 | } |
bchilds | a118170 | 2007-06-14 21:47:04 +0000 | [diff] [blame] | 500 | return URIResolverPlugin.createResolver().resolve(baseLocation, publicId, systemId); |
bchilds | 68f36fc | 2007-03-01 22:47:33 +0000 | [diff] [blame] | 501 | } |
| 502 | } |