protected CMElementDeclaration getCMElementDeclaration(
      Element targetElement, List list, String publicId, String systemId) {
    CMElementDeclaration currentED = null;
    try {
      int listSize = list.size();
      for (int i = 0; i < listSize; i++) {
        Element element = (Element) list.get(i);

        final String nodeName = element.getNodeName();

        CMElementDeclaration ed = null;

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

        if (ed == null) {
          CMDocument cmDocument = getCMDocument(publicId, systemId, "XSD"); // $NON-NLS-1$
          if (cmDocument != null) {
            ed = (CMElementDeclaration) cmDocument.getElements().getNamedItem(nodeName);
          }
        }
        currentED = ed;
      }
    } catch (Exception e) {
      Logger.logException(
          "exception locating element declaration for " + targetElement, e); // $NON-NLS-1$
    }

    return currentED;
  }
  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;
  }
  /**
   * Returns the CMDocument that corresponds to the DOM Node. or null if no CMDocument is
   * appropriate for the DOM Node.
   */
  public CMDocument getCorrespondingCMDocument(Node node) {
    CMDocument jcmdoc = null;
    if (node instanceof IDOMNode) {
      IDOMModel model = ((IDOMNode) node).getModel();
      String modelPath = model.getBaseLocation();
      if (modelPath != null && !IModelManager.UNMANAGED_MODEL.equals(modelPath)) {
        float version =
            DeploymentDescriptorPropertyCache.getInstance().getJSPVersion(new Path(modelPath));
        jcmdoc = JSPCMDocumentFactory.getCMDocument(version);
      }
    }
    if (jcmdoc == null) {
      jcmdoc = JSPCMDocumentFactory.getCMDocument();
    }

    CMDocument result = null;
    try {
      if (node.getNodeType() == Node.ELEMENT_NODE) {
        String elementName = node.getNodeName();

        // test to see if this node belongs to JSP's CMDocument (case
        // sensitive)
        CMElementDeclaration dec =
            (CMElementDeclaration) jcmdoc.getElements().getNamedItem(elementName);
        if (dec != null) {
          result = jcmdoc;
        }
      }

      String prefix = node.getPrefix();

      if (result == null && prefix != null && prefix.length() > 0 && node instanceof IDOMNode) {
        // check position dependent
        IDOMNode xmlNode = (IDOMNode) node;
        TLDCMDocumentManager tldmgr =
            TaglibController.getTLDCMDocumentManager(xmlNode.getStructuredDocument());
        if (tldmgr != null) {
          List documents = tldmgr.getCMDocumentTrackers(node.getPrefix(), xmlNode.getStartOffset());
          // there shouldn't be more than one cmdocument returned
          if (documents != null && documents.size() > 0) result = (CMDocument) documents.get(0);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return result;
  }
 public static void loadAnnotationsForGrammar(String publicId, CMDocument cmDocument) {
   List annotationFiles = ContentModelManager.getInstance().getAnnotationFilesInfos(publicId);
   AnnotationMap map = (AnnotationMap) cmDocument.getProperty("annotationMap"); // $NON-NLS-1$
   if (map != null) {
     synchronized (annotationFiles) {
       for (Iterator i = annotationFiles.iterator(); i.hasNext(); ) {
         try {
           AnnotationFileInfo annotationFileInfo = (AnnotationFileInfo) i.next();
           AnnotationFileParser parser = new AnnotationFileParser();
           parser.parse(map, annotationFileInfo);
         } catch (Exception e) {
           Logger.logException(e);
         }
       }
     }
   }
 }