当前位置: 首页>>代码示例>>Java>>正文


Java Document.getDoctype方法代码示例

本文整理汇总了Java中org.w3c.dom.Document.getDoctype方法的典型用法代码示例。如果您正苦于以下问题:Java Document.getDoctype方法的具体用法?Java Document.getDoctype怎么用?Java Document.getDoctype使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.w3c.dom.Document的用法示例。


在下文中一共展示了Document.getDoctype方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testDocType

import org.w3c.dom.Document; //导入方法依赖的package包/类
/** Test that read/write DOCTYPE works too. */
public void testDocType() throws Exception {
    String data = "<!DOCTYPE foo PUBLIC \"The foo DTD\" \"http://nowhere.net/foo.dtd\"><foo><x/><x/></foo>";
    Document doc = XMLUtil.parse(new InputSource(new StringReader(data)), true, true, new Handler(), new Resolver());
    DocumentType t = doc.getDoctype();
    assertNotNull(t);
    assertEquals("foo", t.getName());
    assertEquals("The foo DTD", t.getPublicId());
    assertEquals("http://nowhere.net/foo.dtd", t.getSystemId());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLUtil.write(doc, baos, "UTF-8");
    String data2 = baos.toString("UTF-8");
    //System.err.println("data2:\n" + data2);
    assertTrue(data2, data2.indexOf("foo") != -1);
    assertTrue(data2, data2.indexOf('x') != -1);
    assertTrue(data2, data2.indexOf("DOCTYPE") != -1);
    assertTrue(data2, data2.indexOf("The foo DTD") != -1);
    assertTrue(data2, data2.indexOf("http://nowhere.net/foo.dtd") != -1);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:20,代码来源:XMLUtilTest.java

示例2: write

import org.w3c.dom.Document; //导入方法依赖的package包/类
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);
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:24,代码来源:XMLUtil.java

示例3: whichDoctypePublic

import org.w3c.dom.Document; //导入方法依赖的package包/类
/**
 * 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;
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:23,代码来源:OutputFormat.java

示例4: whichDoctypeSystem

import org.w3c.dom.Document; //导入方法依赖的package包/类
/**
 * 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;
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:23,代码来源:OutputFormat.java

示例5: invoke

import org.w3c.dom.Document; //导入方法依赖的package包/类
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    if (method.getName().equals("getDoctype") && args == null) { // NOI18N
        return (DocumentType)proxyDocument;
    } else if (method.getName().equals("getPublicId") && args == null) { // NOI18N
        Document d = getDocumentImpl(false);
        if (d != null) {
            DocumentType doctype = d.getDoctype();
            return doctype == null ? null : doctype.getPublicId();
        } else {
            return obj.getIP().getPublicId();
        }
    } else {
        return method.invoke(getDocumentImpl(true), args);
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:16,代码来源:XMLDataObject.java

示例6: setupEntityMap

import org.w3c.dom.Document; //导入方法依赖的package包/类
/**
 * 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;
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:16,代码来源:DOMValidatorHelper.java

示例7: write

import org.w3c.dom.Document; //导入方法依赖的package包/类
/**
 * Writes a DOM document to a stream.
 * The precise output format is not guaranteed but this method will attempt to indent it sensibly.
 * 
 * <p class="nonnormative"><b>Important</b>: There might be some problems with
 * <code>&lt;![CDATA[ ]]&gt;</code> sections in the DOM tree you pass into this method. Specifically,
 * some CDATA sections my not be written as CDATA section or may be merged with
 * other CDATA section at the same level. Also if plain text nodes are mixed with
 * CDATA sections at the same level all text is likely to end up in one big CDATA section.
 * <br/>
 * For nodes that only have one CDATA section this method should work fine.
 * </p>
 * 
 * @param doc DOM document to be written
 * @param out data sink
 * @param enc XML-defined encoding name (e.g. "UTF-8")
 * @throws IOException if JAXP fails or the stream cannot be written to
 */
public static void write(Document doc, OutputStream out, String enc) throws IOException {
    if (enc == null) {
        throw new NullPointerException("You must set an encoding; use \"UTF-8\" unless you have a good reason not to!"); // NOI18N
    }
    Document doc2 = normalize(doc);
    ClassLoader orig = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() { // #195921
        @Override public ClassLoader run() {
            return new ClassLoader(ClassLoader.getSystemClassLoader().getParent()) {
                @Override public InputStream getResourceAsStream(String name) {
                    if (name.startsWith("META-INF/services/")) {
                        return new ByteArrayInputStream(new byte[0]); // JAXP #6723276
                    }
                    return super.getResourceAsStream(name);
                }
            };
        }
    }));
    try {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer(
                new StreamSource(new StringReader(IDENTITY_XSLT_WITH_INDENT)));
        DocumentType dt = doc2.getDoctype();
        if (dt != null) {
            String pub = dt.getPublicId();
            if (pub != null) {
                t.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, pub);
            }
            String sys = dt.getSystemId();
            if (sys != null) {
                t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, sys);
            }
        }
        t.setOutputProperty(OutputKeys.ENCODING, enc);
        try {
            t.setOutputProperty(ORACLE_IS_STANDALONE, "yes");
        } catch (IllegalArgumentException x) {
            // fine, introduced in JDK 7u4
        }

        // See #123816
        Set<String> cdataQNames = new HashSet<String>();
        collectCDATASections(doc2, cdataQNames);
        if (cdataQNames.size() > 0) {
            StringBuilder cdataSections = new StringBuilder();
            for(String s : cdataQNames) {
                cdataSections.append(s).append(' '); //NOI18N
            }
            t.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, cdataSections.toString());
        }

        Source source = new DOMSource(doc2);
        Result result = new StreamResult(out);
        t.transform(source, result);
    } catch (Exception e) {
        throw new IOException(e);
    } finally {
        Thread.currentThread().setContextClassLoader(orig);
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:79,代码来源:XMLUtil.java

示例8: getUnparsedEntityURI

import org.w3c.dom.Document; //导入方法依赖的package包/类
/**
 * 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;
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:80,代码来源:DOMHelper.java

示例9: testEntities001

import org.w3c.dom.Document; //导入方法依赖的package包/类
/**
 * Equivalence class partitioning with state and input values orientation
 * for public void setParameter(String name, Object value) throws
 * DOMException, <br>
 * <b>pre-conditions</b>: the doc contains one entity and one entity
 * reference, <br>
 * <b>name</b>: entities <br>
 * <b>value</b>: true. <br>
 * <b>Expected results</b>: the entity and the entity reference are left
 * unchanged
 */
@Test
public void testEntities001() {
    Document doc = null;
    try {
        doc = loadDocument(null, test1_xml);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }

    DOMConfiguration config = doc.getDomConfig();
    if (!config.canSetParameter("entities", Boolean.TRUE)) {
        Assert.fail("setting 'entities' to true is not supported");
    }

    Element root = doc.getDocumentElement();
    root.appendChild(doc.createEntityReference("x"));

    config.setParameter("entities", Boolean.TRUE);

    setHandler(doc);
    doc.normalizeDocument();
    Node child = root.getFirstChild();
    if (child == null) {
        Assert.fail("root has no child");
    }
    if (child.getNodeType() != Node.ENTITY_REFERENCE_NODE) {
        Assert.fail("root's child is " + child + ", expected entity reference &x;");
    }

    if (doc.getDoctype() == null) {
        Assert.fail("no doctype found");
    }

    if (doc.getDoctype().getEntities() == null) {
        Assert.fail("no entitiy found");
    }

    if (doc.getDoctype().getEntities().getNamedItem("x") == null) {
        Assert.fail("no entitiy with name 'x' found");
    }

    return; // Status.passed("OK");
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:55,代码来源:DOMConfigurationTest.java


注:本文中的org.w3c.dom.Document.getDoctype方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。