Search in sources :

Example 6 with Entity

use of org.w3c.dom.Entity in project j2objc by google.

the class DOM3TreeWalker method isEntityReferneceWellFormed.

/**
     * Checks if an EntityRefernece node is well-formed, by checking it's node name.  Then depending
     * on whether it is referenced in Element content or in an Attr Node, checks if the EntityReference
     * references an unparsed entity or a external entity and if so throws raises the 
     * appropriate well-formedness error.  
     * 
     * @param data The contents of the comment node
     * @parent The parent of the EntityReference Node
     */
protected void isEntityReferneceWellFormed(EntityReference node) {
    // Is the EntityReference name a valid XML name
    if (!isXMLName(node.getNodeName(), fIsXMLVersion11)) {
        String msg = Utils.messages.createMessage(MsgKey.ER_WF_INVALID_CHARACTER_IN_NODE_NAME, new Object[] { "EntityReference", node.getNodeName() });
        if (fErrorHandler != null) {
            fErrorHandler.handleError(new DOMErrorImpl(DOMError.SEVERITY_FATAL_ERROR, msg, MsgKey.ER_WF_INVALID_CHARACTER_IN_NODE_NAME, null, null, null));
        }
    }
    // determine the parent node
    Node parent = node.getParentNode();
    // Traverse the declared entities and check if the nodeName and namespaceURI
    // of the EntityReference matches an Entity.  If so, check the if the notationName
    // is not null, if so, report an error.
    DocumentType docType = node.getOwnerDocument().getDoctype();
    if (docType != null) {
        NamedNodeMap entities = docType.getEntities();
        for (int i = 0; i < entities.getLength(); i++) {
            Entity ent = (Entity) entities.item(i);
            String nodeName = node.getNodeName() == null ? "" : node.getNodeName();
            String nodeNamespaceURI = node.getNamespaceURI() == null ? "" : node.getNamespaceURI();
            String entName = ent.getNodeName() == null ? "" : ent.getNodeName();
            String entNamespaceURI = ent.getNamespaceURI() == null ? "" : ent.getNamespaceURI();
            // WFC: Parsed Entity
            if (parent.getNodeType() == Node.ELEMENT_NODE) {
                if (entNamespaceURI.equals(nodeNamespaceURI) && entName.equals(nodeName)) {
                    if (ent.getNotationName() != null) {
                        String msg = Utils.messages.createMessage(MsgKey.ER_WF_REF_TO_UNPARSED_ENT, new Object[] { node.getNodeName() });
                        if (fErrorHandler != null) {
                            fErrorHandler.handleError(new DOMErrorImpl(DOMError.SEVERITY_FATAL_ERROR, msg, MsgKey.ER_WF_REF_TO_UNPARSED_ENT, null, null, null));
                        }
                    }
                }
            }
            // WFC: No External Entity References
            if (parent.getNodeType() == Node.ATTRIBUTE_NODE) {
                if (entNamespaceURI.equals(nodeNamespaceURI) && entName.equals(nodeName)) {
                    if (ent.getPublicId() != null || ent.getSystemId() != null || ent.getNotationName() != null) {
                        String msg = Utils.messages.createMessage(MsgKey.ER_WF_REF_TO_EXTERNAL_ENT, new Object[] { node.getNodeName() });
                        if (fErrorHandler != null) {
                            fErrorHandler.handleError(new DOMErrorImpl(DOMError.SEVERITY_FATAL_ERROR, msg, MsgKey.ER_WF_REF_TO_EXTERNAL_ENT, null, null, null));
                        }
                    }
                }
            }
        //end if WFC: No External Entity References
        }
    }
}
Also used : Entity(org.w3c.dom.Entity) NamedNodeMap(org.w3c.dom.NamedNodeMap) Node(org.w3c.dom.Node) DocumentType(org.w3c.dom.DocumentType)

Example 7 with Entity

use of org.w3c.dom.Entity in project robovm by robovm.

the class DOMHelper method getUnparsedEntityURI.

/**
   * The getUnparsedEntityURI function returns the URI of the unparsed
   * entity with the specified name in the same document as the context
   * node (see [3.3 Unparsed Entities]). It returns the empty string if
   * there is no such entity.
   * <p>
   * XML processors may choose to use the System Identifier (if one
   * is provided) to resolve the entity, rather than the URI in the
   * Public Identifier. The details are dependent on the processor, and
   * we would have to support some form of plug-in resolver to handle
   * this properly. Currently, we simply return the System Identifier if
   * present, and hope that it a usable URI or that our caller can
   * map it to one.
   * TODO: Resolve Public Identifiers... or consider changing function name.
   * <p>
   * If we find a relative URI 
   * reference, XML expects it to be resolved in terms of the base URI 
   * of the document. The DOM doesn't do that for us, and it isn't 
   * entirely clear whether that should be done here; currently that's
   * pushed up to a higher levelof our application. (Note that DOM Level 
   * 1 didn't store the document's base URI.)
   * TODO: Consider resolving Relative URIs.
   * <p>
   * (The DOM's statement that "An XML processor may choose to
   * completely expand entities before the structure model is passed
   * to the DOM" refers only to parsed entities, not unparsed, and hence
   * doesn't affect this function.)
   *
   * @param name A string containing the Entity Name of the unparsed
   * entity.
   * @param doc Document node for the document to be searched.
   *
   * @return String containing the URI of the Unparsed Entity, or an
   * empty string if no such entity exists.
   */
public String getUnparsedEntityURI(String name, Document doc) {
    String url = "";
    DocumentType doctype = doc.getDoctype();
    if (null != doctype) {
        NamedNodeMap entities = doctype.getEntities();
        if (null == entities)
            return url;
        Entity entity = (Entity) entities.getNamedItem(name);
        if (null == entity)
            return url;
        String notationName = entity.getNotationName();
        if (// then it's unparsed
        null != notationName) {
            // The draft says: "The XSLT processor may use the public 
            // identifier to generate a URI for the entity instead of the URI 
            // specified in the system identifier. If the XSLT processor does 
            // not use the public identifier to generate the URI, it must use 
            // the system identifier; if the system identifier is a relative 
            // URI, it must be resolved into an absolute URI using the URI of 
            // the resource containing the entity declaration as the base 
            // URI [RFC2396]."
            // So I'm falling a bit short here.
            url = entity.getSystemId();
            if (null == url) {
                url = entity.getPublicId();
            } else {
            // This should be resolved to an absolute URL, but that's hard 
            // to do from here.
            }
        }
    }
    return url;
}
Also used : Entity(org.w3c.dom.Entity) NamedNodeMap(org.w3c.dom.NamedNodeMap) DocumentType(org.w3c.dom.DocumentType)

Aggregations

DocumentType (org.w3c.dom.DocumentType)7 Entity (org.w3c.dom.Entity)7 NamedNodeMap (org.w3c.dom.NamedNodeMap)7 XMLString (org.apache.xml.utils.XMLString)3 Document (org.w3c.dom.Document)3 Node (org.w3c.dom.Node)2