use of org.w3c.dom.DOMException in project JMRI by JMRI.
the class JmriConfiguration method removeConfigurationFragment.
@Override
public boolean removeConfigurationFragment(final String elementName, final String namespace, final boolean shared) throws IllegalArgumentException {
synchronized (this) {
File file = this.getConfigurationFile(shared);
if (file.canWrite()) {
try {
Document doc;
try (final InputStream is = new FileInputStream(file)) {
InputSource input = new InputSource(is);
input.setSystemId(file.toURI().toURL().toString());
doc = XMLUtil.parse(input, false, true, null, null);
}
Element root = doc.getDocumentElement();
Element toRemove = XMLUtil.findElement(root, elementName, namespace);
if (toRemove != null) {
root.removeChild(toRemove);
this.backup(shared);
if (root.getElementsByTagName("*").getLength() > 0) {
// NOI18N
try (final OutputStream os = new FileOutputStream(file)) {
// NOI18N
XMLUtil.write(doc, os, "UTF-8");
}
} else if (!file.delete()) {
log.debug("Unable to delete {}", file);
}
return true;
}
} catch (IOException | SAXException | DOMException ex) {
log.error("Cannot remove {} from {}", elementName, file, ex);
}
}
return false;
}
}
use of org.w3c.dom.DOMException in project JMRI by JMRI.
the class XMLUtil method normalize.
/**
* Try to normalize a document by removing nonsignificant whitespace.
*
* @see "#62006"
*/
private static Document normalize(Document orig) throws IOException {
DocumentBuilder builder = null;
DocumentBuilderFactory factory = getFactory(false, false);
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
//NOI18N
throw new IOException("Cannot create parser satisfying configuration parameters: " + e, e);
}
DocumentType doctype = null;
NodeList nl = orig.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
if (nl.item(i) instanceof DocumentType) {
// We cannot import DocumentType's, so we need to manually copy it.
doctype = (DocumentType) nl.item(i);
}
}
Document doc;
if (doctype != null) {
doc = builder.getDOMImplementation().createDocument(orig.getDocumentElement().getNamespaceURI(), orig.getDocumentElement().getTagName(), builder.getDOMImplementation().createDocumentType(orig.getDoctype().getName(), orig.getDoctype().getPublicId(), orig.getDoctype().getSystemId()));
// XXX what about entity decls inside the DOCTYPE?
doc.removeChild(doc.getDocumentElement());
} else {
doc = builder.newDocument();
}
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (!(node instanceof DocumentType)) {
try {
doc.appendChild(doc.importNode(node, true));
} catch (DOMException x) {
// Thrown in NB-Core-Build #2896 & 2898 inside GeneratedFilesHelper.applyBuildExtensions
throw new IOException("Could not import or append " + node + " of " + node.getClass(), x);
}
}
}
doc.normalize();
// NOI18N
nl = doc.getElementsByTagName("*");
for (int i = 0; i < nl.getLength(); i++) {
Element e = (Element) nl.item(i);
removeXmlBase(e);
NodeList nl2 = e.getChildNodes();
for (int j = 0; j < nl2.getLength(); j++) {
Node n = nl2.item(j);
if (n instanceof Text && ((Text) n).getNodeValue().trim().length() == 0) {
e.removeChild(n);
// since list is dynamic
j--;
}
}
}
return doc;
}
use of org.w3c.dom.DOMException in project jdk8u_jdk by JetBrains.
the class TransformXPath method enginePerformTransform.
/**
* Method enginePerformTransform
* @inheritDoc
* @param input
*
* @throws TransformationException
*/
protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input, OutputStream os, Transform transformObject) throws TransformationException {
try {
/**
* If the actual input is an octet stream, then the application MUST
* convert the octet stream to an XPath node-set suitable for use by
* Canonical XML with Comments. (A subsequent application of the
* REQUIRED Canonical XML algorithm would strip away these comments.)
*
* ...
*
* The evaluation of this expression includes all of the document's nodes
* (including comments) in the node-set representing the octet stream.
*/
Element xpathElement = XMLUtils.selectDsNode(transformObject.getElement().getFirstChild(), Constants._TAG_XPATH, 0);
if (xpathElement == null) {
Object[] exArgs = { "ds:XPath", "Transform" };
throw new TransformationException("xml.WrongContent", exArgs);
}
Node xpathnode = xpathElement.getChildNodes().item(0);
String str = XMLUtils.getStrFromNode(xpathnode);
input.setNeedsToBeExpanded(needsCircumvent(str));
if (xpathnode == null) {
throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "Text must be in ds:Xpath");
}
XPathFactory xpathFactory = XPathFactory.newInstance();
XPathAPI xpathAPIInstance = xpathFactory.newXPathAPI();
input.addNodeFilter(new XPathNodeFilter(xpathElement, xpathnode, str, xpathAPIInstance));
input.setNodeSet(true);
return input;
} catch (DOMException ex) {
throw new TransformationException("empty", ex);
}
}
use of org.w3c.dom.DOMException in project jdk8u_jdk by JetBrains.
the class IIOMetadataNode method setAttributeNode.
public Attr setAttributeNode(Attr newAttr) throws DOMException {
Element owner = newAttr.getOwnerElement();
if (owner != null) {
if (owner == this) {
return null;
} else {
throw new DOMException(DOMException.INUSE_ATTRIBUTE_ERR, "Attribute is already in use");
}
}
IIOAttr attr;
if (newAttr instanceof IIOAttr) {
attr = (IIOAttr) newAttr;
attr.setOwnerElement(this);
} else {
attr = new IIOAttr(this, newAttr.getName(), newAttr.getValue());
}
Attr oldAttr = getAttributeNode(attr.getName());
if (oldAttr != null) {
removeAttributeNode(oldAttr);
}
attributes.add(attr);
return oldAttr;
}
use of org.w3c.dom.DOMException in project webservices-axiom by apache.
the class TestAllowedChildren method runTest.
protected void runTest() throws Throwable {
Document doc = dbf.newDocumentBuilder().newDocument();
doc.appendChild(doc.createComment("some comment"));
doc.appendChild(doc.createProcessingInstruction("pi", "data"));
// says that text nodes are not allowed as children of a document.
try {
doc.appendChild(doc.createTextNode(" "));
fail("Expected DOMException");
} catch (DOMException ex) {
assertEquals(DOMException.HIERARCHY_REQUEST_ERR, ex.code);
}
doc.appendChild(doc.createElement("root1"));
// Multiple document elements are not allowed
try {
doc.appendChild(doc.createElement("root2"));
fail("Expected DOMException");
} catch (DOMException ex) {
assertEquals(DOMException.HIERARCHY_REQUEST_ERR, ex.code);
}
// PIs and comments after the document element are allowed
doc.appendChild(doc.createProcessingInstruction("pi", "data"));
doc.appendChild(doc.createComment("some comment"));
// Again, text nodes are not allowed
try {
doc.appendChild(doc.createTextNode(" "));
fail("Expected DOMException");
} catch (DOMException ex) {
assertEquals(DOMException.HIERARCHY_REQUEST_ERR, ex.code);
}
}
Aggregations