Search in sources :

Example 66 with DocumentType

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

the class DOMImplementationCreateDocument method testCreateDocument5.

public void testCreateDocument5() throws Throwable {
    Document doc;
    DOMImplementation domImpl;
    String namespaceURI = "http://www.w3.org/xml/1998/namespace";
    String qualifiedName = "xml:root";
    DocumentType docType = null;
    doc = (Document) load("staffNS", builder);
    domImpl = doc.getImplementation();
    {
        boolean success = false;
        try {
            domImpl.createDocument(namespaceURI, qualifiedName, docType);
        } catch (DOMException ex) {
            success = (ex.code == DOMException.NAMESPACE_ERR);
        }
        assertTrue("domimplementationcreatedocument05", success);
    }
}
Also used : DOMException(org.w3c.dom.DOMException) DOMImplementation(org.w3c.dom.DOMImplementation) DocumentType(org.w3c.dom.DocumentType) Document(org.w3c.dom.Document)

Example 67 with DocumentType

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

the class NodeHasAttributes method testHasAttributes2.

public void testHasAttributes2() throws Throwable {
    Document doc;
    DocumentType docType;
    boolean hasAttributes;
    doc = (Document) load("staffNS", builder);
    docType = doc.getDoctype();
    hasAttributes = docType.hasAttributes();
    assertFalse("nodehasattributes02", hasAttributes);
}
Also used : DocumentType(org.w3c.dom.DocumentType) Document(org.w3c.dom.Document)

Example 68 with DocumentType

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

the class DocumentBuilderImpl method parse.

@Override
public Document parse(InputSource source) throws SAXException, IOException {
    if (source == null) {
        throw new IllegalArgumentException("source == null");
    }
    String namespaceURI = null;
    String qualifiedName = null;
    DocumentType doctype = null;
    String inputEncoding = source.getEncoding();
    String systemId = source.getSystemId();
    DocumentImpl document = new DocumentImpl(dom, namespaceURI, qualifiedName, doctype, inputEncoding);
    document.setDocumentURI(systemId);
    KXmlParser parser = new KXmlParser();
    try {
        parser.keepNamespaceAttributes();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, namespaceAware);
        if (source.getByteStream() != null) {
            parser.setInput(source.getByteStream(), inputEncoding);
        } else if (source.getCharacterStream() != null) {
            parser.setInput(source.getCharacterStream());
        } else if (systemId != null) {
            URL url = new URL(systemId);
            URLConnection urlConnection = url.openConnection();
            urlConnection.connect();
            // TODO: if null, extract the inputEncoding from the Content-Type header?
            parser.setInput(urlConnection.getInputStream(), inputEncoding);
        } else {
            throw new SAXParseException("InputSource needs a stream, reader or URI", null);
        }
        if (parser.nextToken() == XmlPullParser.END_DOCUMENT) {
            throw new SAXParseException("Unexpected end of document", null);
        }
        parse(parser, document, document, XmlPullParser.END_DOCUMENT);
        parser.require(XmlPullParser.END_DOCUMENT, null, null);
    } catch (XmlPullParserException ex) {
        if (ex.getDetail() instanceof IOException) {
            throw (IOException) ex.getDetail();
        }
        if (ex.getDetail() instanceof RuntimeException) {
            throw (RuntimeException) ex.getDetail();
        }
        LocatorImpl locator = new LocatorImpl();
        locator.setPublicId(source.getPublicId());
        locator.setSystemId(systemId);
        locator.setLineNumber(ex.getLineNumber());
        locator.setColumnNumber(ex.getColumnNumber());
        SAXParseException newEx = new SAXParseException(ex.getMessage(), locator);
        if (errorHandler != null) {
            errorHandler.error(newEx);
        }
        throw newEx;
    } finally {
        IoUtils.closeQuietly(parser);
    }
    return document;
}
Also used : KXmlParser(org.kxml2.io.KXmlParser) SAXParseException(org.xml.sax.SAXParseException) DocumentType(org.w3c.dom.DocumentType) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) LocatorImpl(org.xml.sax.helpers.LocatorImpl) IOException(java.io.IOException) DocumentImpl(org.apache.harmony.xml.dom.DocumentImpl) URL(java.net.URL) URLConnection(java.net.URLConnection)

Example 69 with DocumentType

use of org.w3c.dom.DocumentType 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)

Example 70 with DocumentType

use of org.w3c.dom.DocumentType in project JMRI by JMRI.

the class XMLUtil method write.

/**
     * 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 (for example, "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) {
        // NOI18N
        throw new NullPointerException("You must set an encoding; use \"UTF-8\" unless you have a good reason not to!");
    }
    Document doc2 = normalize(doc);
    ClassLoader orig = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(AccessController.doPrivileged(new // #195921
    PrivilegedAction<ClassLoader>() {

        @Override
        public ClassLoader run() {
            return new ClassLoader(ClassLoader.getSystemClassLoader().getParent()) {

                @Override
                public InputStream getResourceAsStream(String name) {
                    if (name.startsWith("META-INF/services/")) {
                        // JAXP #6723276
                        return new ByteArrayInputStream(new byte[0]);
                    }
                    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) {
                //NOI18N
                cdataSections.append(s).append(' ');
            }
            t.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, cdataSections.toString());
        }
        Source source = new DOMSource(doc2);
        Result result = new StreamResult(out);
        t.transform(source, result);
    } catch (javax.xml.transform.TransformerException | RuntimeException e) {
        // catch anything that happens
        throw new IOException(e);
    } finally {
        Thread.currentThread().setContextClassLoader(orig);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) DocumentType(org.w3c.dom.DocumentType) IOException(java.io.IOException) Document(org.w3c.dom.Document) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) PrivilegedAction(java.security.PrivilegedAction) ByteArrayInputStream(java.io.ByteArrayInputStream) StringReader(java.io.StringReader) HashSet(java.util.HashSet)

Aggregations

DocumentType (org.w3c.dom.DocumentType)75 Document (org.w3c.dom.Document)57 DOMImplementation (org.w3c.dom.DOMImplementation)28 DOMException (org.w3c.dom.DOMException)18 NamedNodeMap (org.w3c.dom.NamedNodeMap)12 Node (org.w3c.dom.Node)12 Element (org.w3c.dom.Element)11 NodeList (org.w3c.dom.NodeList)8 ArrayList (java.util.ArrayList)7 Entity (org.w3c.dom.Entity)7 IOException (java.io.IOException)5 Attr (org.w3c.dom.Attr)4 Text (org.w3c.dom.Text)4 SAXParseException (org.xml.sax.SAXParseException)4 URL (java.net.URL)3 URLConnection (java.net.URLConnection)3 DocumentBuilder (javax.xml.parsers.DocumentBuilder)3 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)3 DocumentImpl (org.apache.harmony.xml.dom.DocumentImpl)3 XMLString (org.apache.xml.utils.XMLString)3