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

The following examples show how to use org.w3c.dom.Document#setXmlVersion() . 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: EppMessageTest.java    From nomulus with Apache License 2.0 6 votes vote down vote up
@Test
public void xmlDocToStringSuccess() throws Exception {
  Document xml = builder.newDocument();
  Element doc = xml.createElement("doc");
  Element title = xml.createElement("title");
  title.setTextContent("test");
  Element meta = xml.createElement("meta");
  meta.setAttribute("version", "1.0");
  doc.appendChild(title);
  doc.appendChild(meta);
  xml.appendChild(doc);

  // note that setting the version just ensures this will either be the same in the result,
  // or the result won't support the version and this will throw an exception.
  xml.setXmlVersion("1.0");
  // setting stand alone to true removes this from the processing instructions
  xml.setXmlStandalone(true);
  String expected =
      "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
          + "<doc><title>test</title><meta version=\"1.0\"/></doc>";
  String actual = EppMessage.xmlDocToString(xml);
  assertThat(actual).isEqualTo(expected);
}
 
Example 2
Source File: EppMessageTest.java    From nomulus with Apache License 2.0 6 votes vote down vote up
@Test
public void xmlDoctoByteArraySuccess() throws Exception {
  Document xml = builder.newDocument();
  Element doc = xml.createElement("doc");
  Element title = xml.createElement("title");
  title.setTextContent("test");
  Element meta = xml.createElement("meta");
  meta.setAttribute("version", "1.0");
  doc.appendChild(title);
  doc.appendChild(meta);
  xml.appendChild(doc);

  // note that setting the version just ensures this will either be the same in the result,
  // or the result won't support the version and this will throw an exception.
  xml.setXmlVersion("1.0");
  // setting stand alone to true removes this from the processing instructions
  xml.setXmlStandalone(true);
  String expected =
      "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
          + "<doc><title>test</title><meta version=\"1.0\"/></doc>";
  byte[] actual = EppMessage.xmlDocToByteArray(xml);
  assertThat(actual).isEqualTo(expected.getBytes(UTF_8));
}
 
Example 3
Source File: JavaxXmlFactories.java    From attic-polygene-java with Apache License 2.0 6 votes vote down vote up
@Override
public Document newDocumentForSerialization()
{
    try
    {
        DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder();
        Document doc = docBuilder.newDocument();
        doc.setXmlVersion( "1.1" );
        doc.setXmlStandalone( true );
        return doc;
    }
    catch( ParserConfigurationException ex )
    {
        throw new SerializationException( "Unable to create XML document. "
                                          + "Is your javax.xml subsystem correctly set up?", ex );
    }
}
 
Example 4
Source File: XMLFactory.java    From ph-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new document with a document type using a custom document builder.
 *
 * @param aDocBuilder
 *        the document builder to be used. May not be <code>null</code>.
 * @param eVersion
 *        The XML version to use. If <code>null</code> is passed,
 *        {@link EXMLVersion#XML_10} will be used.
 * @param sQualifiedName
 *        The qualified name to use.
 * @param sPublicId
 *        The public ID of the document type.
 * @param sSystemId
 *        The system ID of the document type.
 * @return The created document. Never <code>null</code>.
 */
@Nonnull
public static Document newDocument (@Nonnull final DocumentBuilder aDocBuilder,
                                    @Nullable final EXMLVersion eVersion,
                                    @Nonnull final String sQualifiedName,
                                    @Nullable final String sPublicId,
                                    @Nullable final String sSystemId)
{
  ValueEnforcer.notNull (aDocBuilder, "DocBuilder");

  final DOMImplementation aDomImpl = aDocBuilder.getDOMImplementation ();
  final DocumentType aDocType = aDomImpl.createDocumentType (sQualifiedName, sPublicId, sSystemId);

  final Document aDoc = aDomImpl.createDocument (sSystemId, sQualifiedName, aDocType);
  aDoc.setXmlVersion ((eVersion != null ? eVersion : EXMLVersion.XML_10).getVersion ());
  return aDoc;
}
 
Example 5
Source File: DLNAService.java    From Connect-SDK-Android-Core with Apache License 2.0 5 votes vote down vote up
protected String getMessageXml(String serviceURN, String method, String instanceId, Map<String, String> params) {
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.newDocument();
        doc.setXmlStandalone(true);
        doc.setXmlVersion("1.0");

        Element root = doc.createElement("s:Envelope");
        Element bodyElement = doc.createElement("s:Body");
        Element methodElement = doc.createElementNS(serviceURN, "u:" + method);
        Element instanceElement = doc.createElement("InstanceID");

        root.setAttribute("s:encodingStyle", "http://schemas.xmlsoap.org/soap/encoding/");
        root.setAttribute("xmlns:s", "http://schemas.xmlsoap.org/soap/envelope/");

        doc.appendChild(root);
        root.appendChild(bodyElement);
        bodyElement.appendChild(methodElement);
        if (instanceId != null) {
            instanceElement.setTextContent(instanceId);
            methodElement.appendChild(instanceElement);
        }

        if (params != null) {
            for (Map.Entry<String, String> entry : params.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                Element element = doc.createElement(key);
                element.setTextContent(value);
                methodElement.appendChild(element);
            }
        }
        return xmlToString(doc, true);
    } catch (Exception e) {
        return null;
    }
}
 
Example 6
Source File: XmlDocumentBuilder.java    From archistar-core with GNU General Public License v2.0 5 votes vote down vote up
public Document listElements(String prefix, String bucketName, int maxKeys, Set<SimpleFile> results) {
    Document doc = this.docBuilder.newDocument();
    doc.setXmlVersion("1.0");

    Element rootElement = doc.createElement("ListBucketResult");
    rootElement.setAttribute("xmlns", "http://doc.s3.amazonaws.com/2006-03-01");
    doc.appendChild(rootElement);

    Element name = doc.createElement("Name");
    name.setTextContent(bucketName);

    rootElement.appendChild(name);
    rootElement.appendChild(doc.createElement("Prefix"));
    rootElement.appendChild(doc.createElement("Marker"));

    rootElement.appendChild(createElement(doc, "MaxKeys", "1000"));
    rootElement.appendChild(createElement(doc, "isTruncated", "false"));

    for (SimpleFile entry : results) {
        Element contents = doc.createElement("Contents");

        String eTag = "could not compute";

        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] thedigest = md.digest(entry.getData());
            eTag = Hex.encodeHex(thedigest);
        } catch (NoSuchAlgorithmException e) {
            assert(false);
        }

        contents.appendChild(createElement(doc, "Key", entry.getPath()));
        contents.appendChild(createElement(doc, "Size", "" + entry.getData().length));
        contents.appendChild(createElement(doc, "LastModified", "2006-02-03T16:41:58.000Z"));
        contents.appendChild(createElement(doc, "ETag", eTag));

        rootElement.appendChild(contents);
    }
    return doc;
}
 
Example 7
Source File: XmlDocumentBuilder.java    From archistar-core with GNU General Public License v2.0 5 votes vote down vote up
public Document bucketNotFound(String bucket) {

        Document doc = this.docBuilder.newDocument();
        doc.setXmlVersion("1.0");

        Element rootElement = doc.createElement("Error");
        rootElement.setAttribute("xmlns", "http://doc.s3.amazonaws.com/2006-03-01");
        doc.appendChild(rootElement);

        rootElement.appendChild(createElement(doc, "Code", "NoSuchBucket"));
        rootElement.appendChild(createElement(doc, "Resource", bucket));
        rootElement.appendChild(createElement(doc, "Resource", bucket));

        return doc;
    }
 
Example 8
Source File: XmlDocumentBuilder.java    From archistar-core with GNU General Public License v2.0 5 votes vote down vote up
public Document listBuckets(Map<String, FakeBucket> bucketList) {
    Document doc = this.docBuilder.newDocument();
    doc.setXmlVersion("1.0");

    Element rootElement = doc.createElement("ListAllMyBucketsResult");
    rootElement.setAttribute("xmlns", "http://doc.s3.amazonaws.com/2006-03-01");
    doc.appendChild(rootElement);

    Element owner = doc.createElement("Owner");
    Element id = doc.createElement("ID");
    id.setTextContent("deadbeef");
    Element name = doc.createElement("DisplayName");
    name.setTextContent("Andreas Happe");

    owner.appendChild(id);
    owner.appendChild(name);
    rootElement.appendChild(owner);

    Element buckets = doc.createElement("Buckets");

    for (Entry<String, FakeBucket> e : bucketList.entrySet()) {

        Element bucket = doc.createElement("Bucket");

        bucket.appendChild(createElement(doc, "Name", e.getKey()));
        bucket.appendChild(createElement(doc, "CreationDate", "1982-07-07T16:41:58.000Z"));

        buckets.appendChild(bucket);
    }
    rootElement.appendChild(buckets);

    return doc;

}
 
Example 9
Source File: XmlDocumentBuilder.java    From archistar-core with GNU General Public License v2.0 5 votes vote down vote up
Document noSuchKey(String id) {
    Document doc = this.docBuilder.newDocument();
    doc.setXmlVersion("1.0");

    Element rootElement = doc.createElement("Error");
    rootElement.setAttribute("xmlns", "http://doc.s3.amazonaws.com/2006-03-01");
    doc.appendChild(rootElement);

    rootElement.appendChild(createElement(doc, "Message", "The resource you requested does not exist"));
    rootElement.appendChild(createElement(doc, "Code", "NoSuchKey"));
    rootElement.appendChild(createElement(doc, "Resource", id));

    return doc;
}
 
Example 10
Source File: PreferencesService.java    From latexdraw with GNU General Public License v3.0 4 votes vote down vote up
public void writePreferences() {
	final Optional<Document> opt = SystemUtils.getInstance().createXMLDocumentBuilder().map(b -> b.newDocument());

	if(opt.isEmpty()) {
		return;
	}

	final Document document = opt.get();
	final Element root = document.createElement(LNamespace.XML_ROOT_PREFERENCES);

	document.setXmlVersion("1.0"); //NON-NLS
	document.setXmlStandalone(true);
	document.appendChild(root);

	final Attr attr = document.createAttribute(LNamespace.XML_VERSION);
	attr.setTextContent(VersionChecker.VERSION);
	root.setAttributeNode(attr);

	SystemUtils.getInstance().createElement(document, LNamespace.XML_PATH_EXPORT, pathExport.get(), root);
	SystemUtils.getInstance().createElement(document, LNamespace.XML_PATH_OPEN, pathOpen.get(), root);
	SystemUtils.getInstance().createElement(document, LNamespace.XML_UNIT, unit.get().name(), root);
	SystemUtils.getInstance().createElement(document, LNamespace.XML_PAGE, page.get().name(), root);
	SystemUtils.getInstance().createElement(document, LNamespace.XML_CHECK_VERSION, String.valueOf(checkVersion.get()), root);
	SystemUtils.getInstance().createElement(document, LNamespace.XML_LANG, lang.get().toLanguageTag(), root);
	SystemUtils.getInstance().createElement(document, LNamespace.XML_MAGNETIC_GRID, String.valueOf(magneticGrid.get()), root);
	SystemUtils.getInstance().createElement(document, LNamespace.XML_GRID_STYLE, gridStyle.get().name(), root);
	SystemUtils.getInstance().createElement(document, LNamespace.XML_GRID_GAP, String.valueOf(gridGap.get()), root);
	SystemUtils.getInstance().createElement(document, LNamespace.XML_LATEX_INCLUDES, includes.get(), root);
	final Element recent = document.createElement(LNamespace.XML_RECENT_FILES);
	root.appendChild(recent);
	recent.setAttribute(LNamespace.XML_NB_RECENT_FILES, String.valueOf(nbRecentFiles.get()));
	recentFileNames.forEach(n -> SystemUtils.getInstance().createElement(document, LNamespace.XML_RECENT_FILE, n, recent));

	try {
		try(final OutputStream fos = Files.newOutputStream(Path.of(getPreferencesPath()))) {
			final Transformer transformer = TransformerFactory.newInstance().newTransformer();
			transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //NON-NLS
			transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); //NON-NLS
			transformer.transform(new DOMSource(document), new StreamResult(fos));
		}
	}catch(final TransformerException | IllegalArgumentException | DOMException | IOException | FactoryConfigurationError ex) {
		BadaboomCollector.INSTANCE.add(ex);
	}
}
 
Example 11
Source File: XMLFactory.java    From ph-commons with Apache License 2.0 3 votes vote down vote up
/**
 * Create a new XML document without document type using a custom document
 * builder.
 *
 * @param aDocBuilder
 *        The document builder to use. May not be <code>null</code>.
 * @param eVersion
 *        The XML version to use. If <code>null</code> is passed,
 *        {@link EXMLVersion#XML_10} will be used.
 * @return The created document. Never <code>null</code>.
 */
@Nonnull
public static Document newDocument (@Nonnull final DocumentBuilder aDocBuilder, @Nullable final EXMLVersion eVersion)
{
  ValueEnforcer.notNull (aDocBuilder, "DocBuilder");

  final Document aDoc = aDocBuilder.newDocument ();
  aDoc.setXmlVersion ((eVersion != null ? eVersion : EXMLVersion.XML_10).getVersion ());
  return aDoc;
}