Java Code Examples for org.w3c.dom.Document#getDoctype()

The following examples show how to use org.w3c.dom.Document#getDoctype() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PayaraDDProvider.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private String getPublicIdFromImpl(RootInterfaceImpl rootProxyImpl) {
    String result = null;

    GraphManager gm = rootProxyImpl.graphManager();
    if (gm != null) {
        Document d = gm.getXmlDocument();
        if (d != null) {
            DocumentType dt = d.getDoctype();
            if (dt != null) {
                result = dt.getPublicId();
            }
        }
    }

    return result;
}
 
Example 2
Source File: XMLUtil.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static void write(Document doc, OutputStream out) throws IOException {
    // XXX note that this may fail to write out namespaces correctly if the document
    // is created with namespaces and no explicit prefixes; however no code in
    // this package is likely to be doing so
    try {
        Transformer t = TransformerFactory.newInstance().newTransformer(
                new StreamSource(new StringReader(IDENTITY_XSLT_WITH_INDENT)));
        DocumentType dt = doc.getDoctype();
        if (dt != null) {
            String pub = dt.getPublicId();
            if (pub != null) {
                t.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, pub);
            }
            t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, dt.getSystemId());
        }
        t.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // NOI18N
        Source source = new DOMSource(doc);
        Result result = new StreamResult(out);
        t.transform(source, result);
    } catch (Exception | TransformerFactoryConfigurationError e) {
        throw new IOException(e);
    }
}
 
Example 3
Source File: DomParser.java    From caja with Apache License 2.0 6 votes vote down vote up
private OpenElementStack makeElementStack(Document doc, MessageQueue mq) {
  Namespaces ns = this.ns;
  DocumentType doctype = doc.getDoctype();
  if (doctype != null) {
    // If we have a DOCTYPE, use its SYSTEM ID to determine the default
    // namespace.
    String sysid = doctype.getSystemId();
    String nsUri = DoctypeMaker.systemIdToNsUri(sysid);
    if (nsUri != null) { ns = new Namespaces(ns, "", nsUri); }
  }
  return asXml
      ? OpenElementStack.Factory.createXmlElementStack(
          doc, needsDebugData, ns, mq)
      : OpenElementStack.Factory.createHtml5ElementStack(
          doc, needsDebugData, mq);
}
 
Example 4
Source File: OutputFormat.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the document type system identifier
 * specified for this document, or null.
 */
public static String whichDoctypeSystem( Document doc )
{
    DocumentType doctype;

    /* DOM Level 2 was introduced into the code base*/
       doctype = doc.getDoctype();
       if ( doctype != null ) {
       // Note on catch: DOM Level 1 does not specify this method
       // and the code will throw a NoSuchMethodError
       try {
       return doctype.getSystemId();
       } catch ( Error except ) { }
       }

    if ( doc instanceof HTMLDocument )
        return DTD.XHTMLSystemId;
    return null;
}
 
Example 5
Source File: OutputFormat.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the document type public identifier
 * specified for this document, or null.
 */
public static String whichDoctypePublic( Document doc )
{
    DocumentType doctype;

       /*  DOM Level 2 was introduced into the code base*/
       doctype = doc.getDoctype();
       if ( doctype != null ) {
       // Note on catch: DOM Level 1 does not specify this method
       // and the code will throw a NoSuchMethodError
       try {
       return doctype.getPublicId();
       } catch ( Error except ) {  }
       }

    if ( doc instanceof HTMLDocument )
        return DTD.XHTMLPublicId;
    return null;
}
 
Example 6
Source File: OutputFormat.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the document type public identifier
 * specified for this document, or null.
 */
public static String whichDoctypePublic( Document doc )
{
    DocumentType doctype;

       /*  DOM Level 2 was introduced into the code base*/
       doctype = doc.getDoctype();
       if ( doctype != null ) {
       // Note on catch: DOM Level 1 does not specify this method
       // and the code will throw a NoSuchMethodError
       try {
       return doctype.getPublicId();
       } catch ( Error except ) {  }
       }

    if ( doc instanceof HTMLDocument )
        return DTD.XHTMLPublicId;
    return null;
}
 
Example 7
Source File: OutputFormat.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the document type public identifier
 * specified for this document, or null.
 */
public static String whichDoctypePublic( Document doc )
{
    DocumentType doctype;

       /*  DOM Level 2 was introduced into the code base*/
       doctype = doc.getDoctype();
       if ( doctype != null ) {
       // Note on catch: DOM Level 1 does not specify this method
       // and the code will throw a NoSuchMethodError
       try {
       return doctype.getPublicId();
       } catch ( Error except ) {  }
       }

    if ( doc instanceof HTMLDocument )
        return DTD.XHTMLPublicId;
    return null;
}
 
Example 8
Source File: DOMValidatorHelper.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Extracts NamedNodeMap of entities. We need this to validate
 * elements and attributes of type xs:ENTITY, xs:ENTITIES or
 * types dervied from them.
 */
private void setupEntityMap(Document doc) {
    if (doc != null) {
        DocumentType docType = doc.getDoctype();
        if (docType != null) {
            fEntities = docType.getEntities();
            return;
        }
    }
    fEntities = null;
}
 
Example 9
Source File: DOMValidatorHelper.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Extracts NamedNodeMap of entities. We need this to validate
 * elements and attributes of type xs:ENTITY, xs:ENTITIES or
 * types dervied from them.
 */
private void setupEntityMap(Document doc) {
    if (doc != null) {
        DocumentType docType = doc.getDoctype();
        if (docType != null) {
            fEntities = docType.getEntities();
            return;
        }
    }
    fEntities = null;
}
 
Example 10
Source File: DOMValidatorHelper.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Extracts NamedNodeMap of entities. We need this to validate
 * elements and attributes of type xs:ENTITY, xs:ENTITIES or
 * types dervied from them.
 */
private void setupEntityMap(Document doc) {
    if (doc != null) {
        DocumentType docType = doc.getDoctype();
        if (docType != null) {
            fEntities = docType.getEntities();
            return;
        }
    }
    fEntities = null;
}
 
Example 11
Source File: XmlDocumentHashtable.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
protected cfData getXmlDocType() {
	Document doc = (Document) nodeData;
	if (doc.getDoctype() != null)
		return new cfXmlData(this, doc.getDoctype(), isCaseSensitive());
	else
		return null;
}
 
Example 12
Source File: SunEjbJarProxy.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private Document removeDocType(Document document){
    if (document != null) {
        org.w3c.dom.Element docElement = document.getDocumentElement();
        if (docElement != null) {
            org.w3c.dom.DocumentType docType = document.getDoctype();
            if (docType != null) {
                document.removeChild(docType); //NOI18N
            }
        }
    }
    return document;
}
 
Example 13
Source File: SunCmpMappingsProxy.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private Document removeDocType(Document document) {
    if (document != null) {
        org.w3c.dom.Element docElement = document.getDocumentElement();
        if (docElement != null) {
            org.w3c.dom.DocumentType docType = document.getDoctype();
            if (docType != null) {
                document.removeChild(docType); //NOI18N
            }
        }
    }
    return document;
}
 
Example 14
Source File: DOMValidatorHelper.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Extracts NamedNodeMap of entities. We need this to validate
 * elements and attributes of type xs:ENTITY, xs:ENTITIES or
 * types dervied from them.
 */
private void setupEntityMap(Document doc) {
    if (doc != null) {
        DocumentType docType = doc.getDoctype();
        if (docType != null) {
            fEntities = docType.getEntities();
            return;
        }
    }
    fEntities = null;
}
 
Example 15
Source File: DOMHelper.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * 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 (null != notationName)  // then it's unparsed
    {
      // 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;
}
 
Example 16
Source File: MCRDOMContent.java    From mycore with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @param dom the W3C DOM XML document to read from 
 */
public MCRDOMContent(Document dom) {
    super();
    this.dom = dom;
    super.docType = dom.getDoctype() == null ? dom.getDocumentElement().getLocalName() : dom.getDoctype().getName();
}
 
Example 17
Source File: XMLValidatingParser.java    From teamengine with Apache License 2.0 4 votes vote down vote up
/**
 * Validates an XML resource against a list of DTD schemas or as indicated by a
 * DOCTYPE declaration. Validation errors are reported to the given handler. If
 * no DTD references are provided the external schema reference in the DOCTYPE
 * declaration is used (Note: an internal subset is ignored).
 * 
 * @param doc
 *            The input Document.
 * @param dtdList
 *            A list of DTD schema references. May be empty but not null.
 * @param errHandler
 *            An ErrorHandler that collects validation errors.
 * @throws Exception
 *             If any errors occur while attempting to validate the document.
 */
private void validateAgainstDTDList(Document doc, ArrayList<Object> dtdList,
		ErrorHandler errHandler) throws Exception {
	jlogger.finer("Validating XML resource from " + doc.getDocumentURI());
	DocumentBuilder db = dtdValidatingDBF.newDocumentBuilder();
	db.setErrorHandler(errHandler);
             // Fortify Mod: prevent external entity injection
         // includes try block to capture exceptions to setFeature.
	TransformerFactory tf = TransformerFactory.newInstance();
	try {
     	        tf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
	    } catch (Exception e) {
	        jlogger.warning("Failed to secure Transformer");
	    }
	// End Fortify Mod
     Transformer copier = tf.newTransformer();
          ByteArrayOutputStream content = new ByteArrayOutputStream();
	Result copy = new StreamResult(content);
	if (dtdList.isEmpty()) {
		DocumentType doctype = doc.getDoctype();
		if (null == doctype) {
			return;
		}
		URI systemId = URI.create(doctype.getSystemId());
		if (!systemId.isAbsolute() && null != doc.getBaseURI()) {
			systemId = URI.create(doc.getBaseURI()).resolve(systemId);
		}
		copier.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
				systemId.toString());
		copier.transform(new DOMSource(doc), copy);
		db.parse(new ByteArrayInputStream(content.toByteArray()));
	} else {
		for (Object dtdRef : dtdList) {
			content.reset();
			copier.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
					dtdRef.toString());
			copier.transform(new DOMSource(doc), copy);
			db.parse(new ByteArrayInputStream(content.toByteArray()));
		}
	}
}
 
Example 18
Source File: DOM2DTM.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
/**
 * 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 level of 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.
 *
 * @return String containing the URI of the Unparsed Entity, or an
 * empty string if no such entity exists.
 */
public String getUnparsedEntityURI(String name)
{

  String url = "";
  Document doc = (m_root.getNodeType() == Node.DOCUMENT_NODE)
      ? (Document) m_root : m_root.getOwnerDocument();

  if (null != doc)
  {
    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 (null != notationName)  // then it's unparsed
      {
        // 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;
}
 
Example 19
Source File: DOMHelper.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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 (null != notationName)  // then it's unparsed
    {
      // 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;
}
 
Example 20
Source File: DOMHelper.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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 (null != notationName)  // then it's unparsed
    {
      // 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;
}