下面列出了org.w3c.dom.Document#setXmlVersion ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
@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));
}
@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 );
}
}
/**
* 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;
}
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;
}
}
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;
}
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;
}
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;
}
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;
}
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);
}
}
/**
* 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;
}