/**
  * This method returns a list of CMDocumentReferences associated with a particular node or subtree
  */
 public List getCMDocumentReferences(Node node, boolean deep) {
   List result = new ArrayList();
   Document document =
       (node.getNodeType() == Node.DOCUMENT_NODE) ? (Document) node : node.getOwnerDocument();
   DocumentType doctype = document.getDoctype();
   // defect 206833 ... here we test for DTDs that are declared inline
   // since we currently have no way of making use of inline DTDs we ingore them
   // so that the implict DTD (if any) can be used
   if (doctype != null && (doctype.getPublicId() != null || doctype.getSystemId() != null)) {
     String uri = resolveGrammarURI(document, doctype.getPublicId(), doctype.getSystemId());
     result.add(new CMDocumentReferenceImpl(doctype.getPublicId(), uri));
   } else if (getImplictDoctype(document) != null) {
     String[] implicitDoctype = getImplictDoctype(document);
     String uri = resolveGrammarURI(document, implicitDoctype[0], implicitDoctype[1]);
     result.add(new CMDocumentReferenceImpl(implicitDoctype[0], uri));
   } else {
     NamespaceTable namespaceTable = new NamespaceTable(document);
     if (node.getNodeType() == Node.ELEMENT_NODE) {
       namespaceTable.addElement((Element) node);
     }
     if (deep) {
       addChildElementsToNamespaceTable(node, namespaceTable);
     }
     List list = namespaceTable.getNamespaceInfoList();
     for (Iterator i = list.iterator(); i.hasNext(); ) {
       NamespaceInfo info = (NamespaceInfo) i.next();
       String uri = resolveGrammarURI(document, info.uri, info.locationHint);
       result.add(new CMDocumentReferenceImpl(info.uri, uri));
     }
   }
   return result;
 }
Пример #2
0
 /**
  * Returns the namespace prefix for the given URI.
  *
  * @param doc document object model of the XML source file
  * @param namespaceUri namespace URI to examine
  * @return namespace prefix for the given URI
  */
 public static String getPrefixForNamespaceUri(IDOMDocument doc, String namespaceUri) {
   if (doc != null && namespaceUri != null) {
     NamespaceTable table = new NamespaceTable(doc);
     Element elem = doc.getDocumentElement();
     table.addElementLineage(elem);
     return table.getPrefixForURI(namespaceUri);
   }
   return null;
 }
 public CMDocument getCMDocument(Element element, String uri) {
   CMDocument result = null;
   NamespaceTable namespaceTable = new NamespaceTable(element.getOwnerDocument());
   namespaceTable.addElementLineage(element);
   NamespaceInfo namespaceInfo = namespaceTable.getNamespaceInfoForURI(uri);
   if (namespaceInfo != null) {
     result = getCMDocument(namespaceInfo.uri, namespaceInfo.locationHint, "XSD"); // $NON-NLS-1$
   }
   return result;
 }
  protected CMElementDeclaration getCMElementDeclaration(
      Element targetElement, List list, NamespaceTable namespaceTable) {
    CMElementDeclaration currentED = null;
    try {
      int listSize = list.size();
      for (int i = 0; i < listSize; i++) {
        Element element = (Element) list.get(i);

        if (i != 0) {
          namespaceTable.addElement(element);
        }

        String nodeName = element.getNodeName();
        String unprefixedName = DOMNamespaceHelper.getUnprefixedName(nodeName);
        String prefix = DOMNamespaceHelper.getPrefix(nodeName);

        CMElementDeclaration ed = null;

        // see if the element is a local of the currentED
        //
        if (currentED != null) {
          ed = (CMElementDeclaration) currentED.getLocalElements().getNamedItem(unprefixedName);
        }

        if (ed == null) {
          NamespaceInfo namespaceInfo = namespaceTable.getNamespaceInfoForPrefix(prefix);
          if (namespaceInfo != null) {
            CMDocument cmDocument =
                getCMDocument(namespaceInfo.uri, namespaceInfo.locationHint, "XSD"); // $NON-NLS-1$
            if (cmDocument != null) {
              ed = (CMElementDeclaration) cmDocument.getElements().getNamedItem(unprefixedName);
            }
          }
        }
        currentED = ed;

        // handle XSIType
        if (currentED != null) {
          CMElementDeclaration derivedED =
              getDerivedCMElementDeclaration(element, currentED, namespaceTable);
          if (derivedED != null) {
            currentED = derivedED;
          }
        }
      }
    } catch (Exception e) {
      Logger.logException(
          "exception locating element declaration for " + targetElement, e); // $NON-NLS-1$
    }

    return currentED;
  }
  public CMElementDeclaration getCMElementDeclaration(Element element) {
    CMElementDeclaration result = null;
    Document document = element.getOwnerDocument();
    String[] doctypeInfo = getDoctypeInfo(document);
    if (doctypeInfo != null) {
      // we have detected doctype information so we assume that we can locate the
      // CMElementDeclaration
      // in the CMDocument's table of global elements
      CMDocument cmDocument = getCorrespondingCMDocument(element, false);

      // TODO... consider replacing above with
      // CMDocument cmDocument = getCMDocument(document, doctypeInfo[0], doctypeInfo[1]);

      if (cmDocument != null) {
        result =
            (CMElementDeclaration) cmDocument.getElements().getNamedItem(element.getNodeName());

        // this is a hack to get our xsl code assist working... we might want to handle similar
        // grammar behaviour via some established model query setting
        if (result == null && getImplictDoctype(document) != null) {
          Node parent = element.getParentNode();
          if (parent != null && parent.getNodeType() == Node.ELEMENT_NODE) {
            result = getCMElementDeclaration((Element) parent);
          }
        }
      }
    } else {
      // here we use a namespaceTable to consider if the root element has any namespace information
      //
      NamespaceTable namespaceTable = new NamespaceTable(element.getOwnerDocument());
      List list = NamespaceTable.getElementLineage(element);
      Element rootElement = (Element) list.get(0);
      namespaceTable.addElement(rootElement);

      if (namespaceTable.isNamespaceEncountered()) {
        // we assume that this is an XMLSchema style namespace aware document
        result = getCMElementDeclaration(element, list, namespaceTable);
      } else {
        result = checkExternalSchema(element);
        if (result == null) {
          // we assume that this is an inferred CMDocument for a DTD style 'namespaceless' document
          CMDocument cmDocument =
              getCMDocument("", "", "DTD"); // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
          if (cmDocument != null) {
            result =
                (CMElementDeclaration) cmDocument.getElements().getNamedItem(element.getNodeName());
          }
        }
      }
    }
    return result;
  }
 protected CMElementDeclaration getDerivedCMElementDeclaration(
     Element element, CMElementDeclaration ed, NamespaceTable namespaceTable) {
   CMElementDeclaration result = null;
   String xsiPrefix =
       namespaceTable.getPrefixForURI("http://www.w3.org/2001/XMLSchema-instance"); // $NON-NLS-1$
   if (xsiPrefix != null) {
     String xsiTypeValue = element.getAttribute(xsiPrefix + ":type"); // $NON-NLS-1$
     if (xsiTypeValue != null && xsiTypeValue.length() > 0) {
       String typePrefix = DOMNamespaceHelper.getPrefix(xsiTypeValue);
       String typeName = DOMNamespaceHelper.getUnprefixedName(xsiTypeValue);
       String typeURI = namespaceTable.getURIForPrefix(typePrefix);
       String uriQualifiedTypeName = typeName;
       if (typeURI != null && typeURI.length() > 0) {
         uriQualifiedTypeName = "[" + typeURI + "]" + typeName; // $NON-NLS-1$ //$NON-NLS-2$
       }
       result =
           (CMElementDeclaration)
               ed.getProperty("DerivedElementDeclaration=" + uriQualifiedTypeName); // $NON-NLS-1$
       if (result == null) {
         String reference = null;
         NamespaceInfo namespaceInfo = namespaceTable.getNamespaceInfoForPrefix(typePrefix);
         if (namespaceInfo != null) {
           String locationHint =
               resolveGrammarURI(
                   element.getOwnerDocument(), namespaceInfo.uri, namespaceInfo.locationHint);
           if (locationHint != null) {
             reference = "[" + locationHint + "]" + typeName; // $NON-NLS-1$ //$NON-NLS-2$
           }
         }
         if (reference != null) {
           result =
               (CMElementDeclaration)
                   ed.getProperty(
                       "ExternallyDerivedElementDeclaration=" + reference); // $NON-NLS-1$
         }
       }
     }
   }
   return result;
 }
 protected void addChildElementsToNamespaceTable(Node node, NamespaceTable namespaceTable) {
   NodeList nodeList = node.getChildNodes();
   if (nodeList != null) {
     int nodeListLength = nodeList.getLength();
     for (int i = 0; i < nodeListLength; i++) {
       Node childNode = nodeList.item(i);
       if (childNode.getNodeType() == Node.ELEMENT_NODE) {
         namespaceTable.addElement((Element) childNode);
         addChildElementsToNamespaceTable(childNode, namespaceTable);
       }
     }
   }
 }
 protected CMElementDeclaration checkExternalSchema(Element element) {
   final Document document = element.getOwnerDocument();
   if (document instanceof IDOMDocument) {
     final String baseLocation = ((IDOMDocument) document).getModel().getBaseLocation();
     if (baseLocation != null) {
       final IPath basePath = new Path(baseLocation);
       IFile file = null;
       if (basePath.segmentCount() > 1) {
         file = ResourcesPlugin.getWorkspace().getRoot().getFile(basePath);
       }
       final URI uri =
           (file == null || !file.isAccessible())
               ? new File(baseLocation).toURI()
               : file.getLocationURI();
       if (uri != null) {
         IExternalSchemaLocationProvider[] providers =
             ExternalSchemaLocationProviderRegistry.getInstance().getProviders();
         for (int i = 0; i < providers.length; i++) {
           long time = _trace ? System.currentTimeMillis() : 0;
           final Map locations = providers[i].getExternalSchemaLocation(uri);
           if (_trace) {
             long diff = System.currentTimeMillis() - time;
             if (diff > 250)
               Logger.log(
                   Logger.INFO,
                   "Schema location provider took ["
                       + diff
                       + "ms] for URI ["
                       + uri
                       + "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
           }
           if (locations != null && !locations.isEmpty()) {
             Object location =
                 locations.get(IExternalSchemaLocationProvider.NO_NAMESPACE_SCHEMA_LOCATION);
             if (location != null)
               return getCMElementDeclaration(
                   element,
                   NamespaceTable.getElementLineage(element),
                   uri.toString(),
                   location.toString());
           }
         }
       }
     }
   }
   return null;
 }