use of org.w3c.dom.bootstrap.DOMImplementationRegistry in project qi4j-sdk by Qi4j.
the class QuikitResolver method getLSInput.
private LSInput getLSInput() throws Exception {
DOMImplementationLS impl;
DOMImplementation docImpl = builder.getDOMImplementation();
// defaulting to the sun implementation.
if (docImpl != null && docImpl.hasFeature("LS", "3.0")) {
impl = (DOMImplementationLS) docImpl.getFeature("LS", "3.0");
} else {
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
if (impl == null) {
System.setProperty(DOMImplementationRegistry.PROPERTY, "com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl");
registry = DOMImplementationRegistry.newInstance();
impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
}
}
return impl.createLSInput();
}
use of org.w3c.dom.bootstrap.DOMImplementationRegistry in project camel by apache.
the class XmlSignatureHelper method transformNonTextNodeToOutputStream.
/**
* Serializes a node using a certain character encoding.
*
* @param node
* DOM node to serialize
* @param os
* output stream, to which the node is serialized
* @param omitXmlDeclaration
* indicator whether to omit the XML declaration or not
* @param encoding
* character encoding, can be <code>null</code>, if
* <code>null</code> then "UTF-8" is used
* @throws Exception
*/
public static void transformNonTextNodeToOutputStream(Node node, OutputStream os, boolean omitXmlDeclaration, String encoding) throws Exception {
// therefore we switched to DOMImplementationLS
if (encoding == null) {
encoding = "UTF-8";
}
DOMImplementationRegistry domImplementationRegistry = DOMImplementationRegistry.newInstance();
DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementationRegistry.getDOMImplementation("LS");
LSOutput lsOutput = domImplementationLS.createLSOutput();
lsOutput.setEncoding(encoding);
lsOutput.setByteStream(os);
LSSerializer lss = domImplementationLS.createLSSerializer();
lss.getDomConfig().setParameter("xml-declaration", !omitXmlDeclaration);
lss.write(node, lsOutput);
}
use of org.w3c.dom.bootstrap.DOMImplementationRegistry in project languagetool by languagetool-org.
the class PatternRuleXmlCreator method getDocument.
private Document getDocument(InputStream is) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
LSParser parser = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
// we need to ignore whitespace here so the nodeToString() method will be able to indent it properly:
parser.setFilter(new IgnoreWhitespaceFilter());
LSInput domInput = impl.createLSInput();
domInput.setByteStream(is);
return parser.parse(domInput);
}
use of org.w3c.dom.bootstrap.DOMImplementationRegistry in project jdk8u_jdk by JetBrains.
the class ItxtUtf8Test method runTest.
public static void runTest(boolean dump, boolean truncate) throws Exception {
String format = "javax_imageio_png_1.0";
BufferedImage img = new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);
ImageWriter iw = ImageIO.getImageWritersByMIMEType("image/png").next();
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageOutputStream ios = new MemoryCacheImageOutputStream(os);
iw.setOutput(ios);
IIOMetadata meta = iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), null);
DOMImplementationRegistry registry;
registry = DOMImplementationRegistry.newInstance();
DOMImplementation impl = registry.getDOMImplementation("XML 3.0");
Document doc = impl.createDocument(null, format, null);
Element root, itxt, entry;
root = doc.getDocumentElement();
root.appendChild(itxt = doc.createElement("iTXt"));
itxt.appendChild(entry = doc.createElement("iTXtEntry"));
entry.setAttribute("keyword", "verbatim");
entry.setAttribute("compressionFlag", "false");
entry.setAttribute("compressionMethod", "0");
entry.setAttribute("languageTag", "x-circled");
entry.setAttribute("translatedKeyword", VERBATIM);
entry.setAttribute("text", TEXT);
itxt.appendChild(entry = doc.createElement("iTXtEntry"));
entry.setAttribute("keyword", "compressed");
entry.setAttribute("compressionFlag", "true");
entry.setAttribute("compressionMethod", "0");
entry.setAttribute("languageTag", "x-circled");
entry.setAttribute("translatedKeyword", COMPRESSED);
entry.setAttribute("text", TEXT);
meta.mergeTree(format, root);
iw.write(new IIOImage(img, null, meta));
iw.dispose();
byte[] bytes = os.toByteArray();
if (dump)
System.out.write(bytes);
if (findBytes(VBYTES, bytes) < 0)
throw new AssertionError("verbatim block not found");
if (findBytes(CBYTES, bytes) < 0)
throw new AssertionError("compressed block not found");
int length = bytes.length;
if (truncate)
length = findBytes(VBYTES, bytes) + 32;
ImageReader ir = ImageIO.getImageReader(iw);
ByteArrayInputStream is = new ByteArrayInputStream(bytes, 0, length);
ImageInputStream iis = new MemoryCacheImageInputStream(is);
ir.setInput(iis);
meta = ir.getImageMetadata(0);
Node node = meta.getAsTree(format);
for (node = node.getFirstChild(); !"iTXt".equals(node.getNodeName()); node = node.getNextSibling()) ;
boolean verbatimSeen = false, compressedSeen = false;
for (node = node.getFirstChild(); node != null; node = node.getNextSibling()) {
entry = (Element) node;
String keyword = entry.getAttribute("keyword");
String translatedKeyword = entry.getAttribute("translatedKeyword");
String text = entry.getAttribute("text");
if ("verbatim".equals(keyword)) {
if (verbatimSeen)
throw new AssertionError("Duplicate");
verbatimSeen = true;
if (!VERBATIM.equals(translatedKeyword))
throw new AssertionError("Wrong translated keyword");
if (!TEXT.equals(text))
throw new AssertionError("Wrong text");
} else if ("compressed".equals(keyword)) {
if (compressedSeen)
throw new AssertionError("Duplicate");
compressedSeen = true;
if (!COMPRESSED.equals(translatedKeyword))
throw new AssertionError("Wrong translated keyword");
if (!TEXT.equals(text))
throw new AssertionError("Wrong text");
} else {
throw new AssertionError("Unexpected keyword");
}
}
if (!(verbatimSeen && compressedSeen))
throw new AssertionError("Missing chunk");
}
use of org.w3c.dom.bootstrap.DOMImplementationRegistry in project zxing by zxing.
the class HtmlAssetTranslator method translateOneFile.
private static void translateOneFile(String language, Path targetHtmlDir, Path sourceFile, String translationTextTranslated) throws IOException {
Path destFile = targetHtmlDir.resolve(sourceFile.getFileName());
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Document document;
try {
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(sourceFile.toFile());
} catch (ParserConfigurationException pce) {
throw new IllegalStateException(pce);
} catch (SAXException sae) {
throw new IOException(sae);
}
Element rootElement = document.getDocumentElement();
rootElement.normalize();
Queue<Node> nodes = new LinkedList<>();
nodes.add(rootElement);
while (!nodes.isEmpty()) {
Node node = nodes.poll();
if (shouldTranslate(node)) {
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
nodes.add(children.item(i));
}
}
if (node.getNodeType() == Node.TEXT_NODE) {
String text = node.getTextContent();
if (!text.trim().isEmpty()) {
text = StringsResourceTranslator.translateString(text, language);
node.setTextContent(' ' + text + ' ');
}
}
}
Node translateText = document.createTextNode(translationTextTranslated);
Node paragraph = document.createElement("p");
paragraph.appendChild(translateText);
Node body = rootElement.getElementsByTagName("body").item(0);
body.appendChild(paragraph);
DOMImplementationRegistry registry;
try {
registry = DOMImplementationRegistry.newInstance();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
LSSerializer writer = impl.createLSSerializer();
String fileAsString = writer.writeToString(document);
// Replace default XML header with HTML DOCTYPE
fileAsString = fileAsString.replaceAll("<\\?xml[^>]+>", "<!DOCTYPE HTML>");
Files.write(destFile, Collections.singleton(fileAsString), StandardCharsets.UTF_8);
}
Aggregations