下面列出了org.w3c.dom.TypeInfo#org.w3c.dom.DOMException 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private String readData(Element cell, int col, String s) throws DOMException, FHIRException {
List<Element> data = new ArrayList<Element>();
XMLUtil.getNamedChildren(cell, "Data", data); // cell.getElementsByTagNameNS(XLS_NS, "Data");
if (data.size() == 0)
return "";
check(data.size() == 1, "Multiple Data encountered ("+Integer.toString(data.size())+" @ col "+Integer.toString(col)+" - "+cell.getTextContent()+" ("+s+"))");
Element d = data.get(0);
String type = d.getAttributeNS(XLS_NS, "Type");
if ("Boolean".equals(type)) {
if (d.getTextContent().equals("1"))
return "True";
else
return "False";
} else if ("String".equals(type)) {
return d.getTextContent();
} else if ("Number".equals(type)) {
return d.getTextContent();
} else if ("DateTime".equals(type)) {
return d.getTextContent();
} else if ("Error".equals(type)) {
return null;
} else
throw new FHIRException("Cell Type is not known ("+d.getAttributeNodeNS(XLS_NS, "Type")+") in "+getLocation());
}
/**
* Introduced in DOM Level 2. <p>
*
* Removes an attribute by local name and namespace URI. If the removed
* attribute has a default value it is immediately replaced.
* The replacing attribute has the same namespace URI and local name,
* as well as the original prefix.<p>
*
* @param namespaceURI The namespace URI of the attribute to remove.
*
* @param localName The local name of the attribute to remove.
* @throws NO_MODIFICATION_ALLOWED_ERR: Raised if this
* node is readonly.
* @since WD-DOM-Level-2-19990923
*/
public void removeAttributeNS(String namespaceURI, String localName) {
if (ownerDocument.errorChecking && isReadOnly()) {
String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null);
throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, msg);
}
if (needsSyncData()) {
synchronizeData();
}
if (attributes == null) {
return;
}
attributes.safeRemoveNamedItemNS(namespaceURI, localName);
}
@Override
public byte[] repackData(byte[] context, byte[] data) {
String xmldata = Globals.helpers.bytesToString(context);
InputStream is = new ByteArrayInputStream(context);
try { //copy payload to XML structure and rewrite xml to the query
DocumentBuilder dBuilder = getsafeDB();
Document doc = dBuilder.parse(is);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName(setting[0]);
nList.item(0).setTextContent(Globals.helpers.bytesToString(data));
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(doc), new StreamResult(writer));
xmldata = writer.getBuffer().toString();
}
catch (IOException | IllegalArgumentException | ParserConfigurationException | TransformerException | DOMException | SAXException e) {
Globals.callbacks.printError("XML packing error: ");
Globals.callbacks.printError(e.getMessage());
xmldata = Globals.helpers.bytesToString(context);
}
return xmldata.getBytes();
}
/** Return the current Node. */
public void setCurrentNode(Node node) {
if (node == null) {
String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NOT_SUPPORTED_ERR", null);
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, msg);
}
fCurrentNode = node;
}
/**
* Checks if the given qualified name is legal with respect
* to the version of XML to which this document must conform.
*
* @param prefix prefix of qualified name
* @param local local part of qualified name
*/
protected final void checkQName(String prefix, String local) {
if (!errorChecking) {
return;
}
// check that both prefix and local part match NCName
boolean validNCName = false;
if (!xml11Version) {
validNCName = (prefix == null || XMLChar.isValidNCName(prefix))
&& XMLChar.isValidNCName(local);
}
else {
validNCName = (prefix == null || XML11Char.isXML11ValidNCName(prefix))
&& XML11Char.isXML11ValidNCName(local);
}
if (!validNCName) {
// REVISIT: add qname parameter to the message
String msg =
DOMMessageFormatter.formatMessage(
DOMMessageFormatter.DOM_DOMAIN,
"INVALID_CHARACTER_ERR",
null);
throw new DOMException(DOMException.INVALID_CHARACTER_ERR, msg);
}
}
protected cfData getXmlValue() {
String nv = null;
try {
nv = nodeData.getNodeValue();
} catch (DOMException ex) {
// Just log it
com.nary.Debug.printStackTrace(ex);
}
if (nv == null)
return new cfStringData("");
else
return new cfStringData(nv.trim());
}
public SpinXmlElement append(SpinXmlElement... childElements) {
ensureNotNull("childElements", childElements);
for (SpinXmlElement childElement : childElements) {
ensureNotNull("childElement", childElement);
DomXmlElement spinDomElement = ensureParamInstanceOf("childElement", childElement, DomXmlElement.class);
adoptElement(spinDomElement);
try {
domElement.appendChild(spinDomElement.domElement);
}
catch (DOMException e) {
throw LOG.unableToAppendElementInImplementation(this, childElement, e);
}
}
return this;
}
/**
* Since a Document may contain at most one top-level Element child,
* and at most one DocumentType declaraction, we need to subclass our
* add-children methods to implement this constraint.
* Since appendChild() is implemented as insertBefore(,null),
* altering the latter fixes both.
* <p>
* While I'm doing so, I've taken advantage of the opportunity to
* cache documentElement and docType so we don't have to
* search for them.
*
* REVISIT: According to the spec it is not allowed to alter neither the
* document element nor the document type in any way
*/
public Node insertBefore(Node newChild, Node refChild)
throws DOMException {
// Only one such child permitted
int type = newChild.getNodeType();
if (errorChecking) {
if((type == Node.ELEMENT_NODE && docElement != null) ||
(type == Node.DOCUMENT_TYPE_NODE && docType != null)) {
String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null);
throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, msg);
}
}
// Adopt orphan doctypes
if (newChild.getOwnerDocument() == null &&
newChild instanceof DocumentTypeImpl) {
((DocumentTypeImpl) newChild).ownerDocument = this;
}
super.insertBefore(newChild,refChild);
// If insert succeeded, cache the kid appropriately
if (type == Node.ELEMENT_NODE) {
docElement = (ElementImpl)newChild;
}
else if (type == Node.DOCUMENT_TYPE_NODE) {
docType = (DocumentTypeImpl)newChild;
}
return newChild;
}
/** For DOM2 support. */
public CoreDocumentImpl(DocumentType doctype, boolean grammarAccess) {
this(grammarAccess);
if (doctype != null) {
DocumentTypeImpl doctypeImpl;
try {
doctypeImpl = (DocumentTypeImpl) doctype;
} catch (ClassCastException e) {
String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null);
throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, msg);
}
doctypeImpl.ownerDocument = this;
appendChild(doctype);
}
}
public static void setValue( org.w3c.dom.Node n, String val){
try{
if(n.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE){
n.setTextContent(val);
} else {
n.setNodeValue(val);
}
} catch(DOMException ex){
Logger.error(ex);
}
}
/**
* This DOM Level 3 method is not supported for {@code IIOMetadataNode}
* and will throw a {@code DOMException}.
* @throws DOMException - always.
*/
public void setIdAttributeNode(Attr idAttr,
boolean isId)
throws DOMException {
throw new DOMException(DOMException.NOT_SUPPORTED_ERR,
"Method not supported");
}
public Node setNamedItem(Node arg) {
throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR,
"This NamedNodeMap is read-only!");
}
/**
* This DOM Level 3 method is not supported for {@code IIOMetadataNode}
* and will throw a {@code DOMException}.
* @throws DOMException - always.
*/
public short compareDocumentPosition(Node other)
throws DOMException {
throw new DOMException(DOMException.NOT_SUPPORTED_ERR,
"Method not supported");
}
public void setMaxWidth( String maxWidth ) throws DOMException
{
throw createUnsupportedPropertyException( "text-decoration" );
}
/**
* Introduced in DOM Level 2. <p>
*
* Creates an XML Document object of the specified type with its document
* element.
*
* @param namespaceURI The namespace URI of the document
* element to create, or null.
* @param qualifiedName The qualified name of the document
* element to create.
* @param doctype The type of document to be created or null.<p>
*
* When doctype is not null, its
* Node.ownerDocument attribute is set to
* the document being created.
* @return Document A new Document object.
* @throws DOMException WRONG_DOCUMENT_ERR: Raised if doctype has
* already been used with a different document.
* @since WD-DOM-Level-2-19990923
*/
public Document createDocument(
String namespaceURI,
String qualifiedName,
DocumentType doctype)
throws DOMException {
if (doctype != null && doctype.getOwnerDocument() != null) {
String msg =
DOMMessageFormatter.formatMessage(
DOMMessageFormatter.DOM_DOMAIN,
"WRONG_DOCUMENT_ERR",
null);
throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, msg);
}
CoreDocumentImpl doc = new CoreDocumentImpl(doctype);
Element e = doc.createElementNS(namespaceURI, qualifiedName);
doc.appendChild(e);
return doc;
}
@Override
public EntityReference createEntityReference(String name) throws DOMException {
throw new UnsupportedOperationException();
}
public Node appendChild(Node newChild) throws DOMException {
throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Method not supported");
}
/**
* Substring is more than a convenience function. In some
* implementations of the DOM, where the stored data may exceed the
* length that can be returned in a single string, the only way to
* read it all is to extract it in chunks via this method.
*
* @param offset Zero-based offset of first character to retrieve.
* @param count Number of characters to retrieve.
*
* If the sum of offset and count exceeds the length, all characters
* to end of data are returned.
*
* @throws DOMException(INDEX_SIZE_ERR) if offset is negative or
* greater than length, or if count is negative.
*
* @throws DOMException(WSTRING_SIZE_ERR) In some implementations,
* count may exceed the permitted length of strings. If so,
* substring() will throw this DOMException advising the user to
* instead retrieve the data in smaller chunks.
*/
public String substringData(int offset, int count)
throws DOMException {
if (needsSyncData()) {
synchronizeData();
}
int length = data.length();
if (count < 0 || offset < 0 || offset > length - 1) {
String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INDEX_SIZE_ERR", null);
throw new DOMException(DOMException.INDEX_SIZE_ERR, msg);
}
int tailIndex = Math.min(offset + count, length);
return data.substring(offset, tailIndex);
}
/**
* The DOM doesn't clearly define what setValue(null) means. I've taken it
* as "remove all children", which from outside should appear
* similar to setting it to the empty string.
*/
public void setValue(String newvalue) {
CoreDocumentImpl ownerDocument = ownerDocument();
if (ownerDocument.errorChecking && isReadOnly()) {
String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null);
throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, msg);
}
Element ownerElement = getOwnerElement();
String oldvalue = "";
if (needsSyncData()) {
synchronizeData();
}
if (needsSyncChildren()) {
synchronizeChildren();
}
if (value != null) {
if (ownerDocument.getMutationEvents()) {
// Can no longer just discard the kids; they may have
// event listeners waiting for them to disconnect.
if (hasStringValue()) {
oldvalue = (String) value;
// create an actual text node as our child so
// that we can use it in the event
if (textNode == null) {
textNode = (TextImpl)
ownerDocument.createTextNode((String) value);
}
else {
textNode.data = (String) value;
}
value = textNode;
textNode.isFirstChild(true);
textNode.previousSibling = textNode;
textNode.ownerNode = this;
textNode.isOwned(true);
hasStringValue(false);
internalRemoveChild(textNode, true);
}
else {
oldvalue = getValue();
while (value != null) {
internalRemoveChild((Node) value, true);
}
}
}
else {
if (hasStringValue()) {
oldvalue = (String) value;
}
else {
// simply discard children if any
oldvalue = getValue();
// remove ref from first child to last child
ChildNode firstChild = (ChildNode) value;
firstChild.previousSibling = null;
firstChild.isFirstChild(false);
firstChild.ownerNode = ownerDocument;
}
// then remove ref to current value
value = null;
needsSyncChildren(false);
}
if (isIdAttribute() && ownerElement != null) {
ownerDocument.removeIdentifier(oldvalue);
}
}
// Create and add the new one, generating only non-aggregate events
// (There are no listeners on the new Text, but there may be
// capture/bubble listeners on the Attr.
// Note that aggregate events are NOT dispatched here,
// since we need to combine the remove and insert.
isSpecified(true);
if (ownerDocument.getMutationEvents()) {
// if there are any event handlers create a real node
internalInsertBefore(ownerDocument.createTextNode(newvalue),
null, true);
hasStringValue(false);
// notify document
ownerDocument.modifiedAttrValue(this, oldvalue);
} else {
// directly store the string
value = newvalue;
hasStringValue(true);
changed();
}
if (isIdAttribute() && ownerElement != null) {
ownerDocument.putIdentifier(newvalue, ownerElement);
}
}
@Test(expectedExceptions = DOMException.class)
public void testCreateElementNeg() throws Exception {
Document doc = createNewDocument();
doc.createElement("!nc$%^*(!");
}
public String getNodeValue() throws DOMException {
return null;
}
/**
* This DOM Level 3 method is not supported for {@code IIOMetadataNode}
* and will throw a {@code DOMException}.
* @throws DOMException - always.
*/
public Object getFeature(String feature, String version)
throws DOMException {
throw new DOMException(DOMException.NOT_SUPPORTED_ERR,
"Method not supported");
}
@Override
public void setPrefix(String prefix) throws DOMException {
element.setPrefix(prefix);
}
@Override
public DOMConfiguration getDomConfig() {
throw new DOMException(DOMException.INVALID_ACCESS_ERR, ACTION_NOT_IMPLEMENTED);
}
/**
* Call user data handlers to let them know the nodes they are related to
* are being deleted. The alternative would be to do that on Node but
* because the nodes are used as the keys we have a reference to them that
* prevents them from being gc'ed until the document is. At the same time,
* doing it here has the advantage of avoiding a finalize() method on Node,
* which would affect all nodes and not just the ones that have a user
* data.
*/
// Temporarily comment out this method, because
// 1. It seems that finalizers are not guaranteed to be called, so the
// functionality is not implemented.
// 2. It affects the performance greatly in multi-thread environment.
// -SG
/*public void finalize() {
if (userData == null) {
return;
}
Enumeration nodes = userData.keys();
while (nodes.hasMoreElements()) {
Object node = nodes.nextElement();
Hashtable t = (Hashtable) userData.get(node);
if (t != null && !t.isEmpty()) {
Enumeration keys = t.keys();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
UserDataRecord r = (UserDataRecord) t.get(key);
if (r.fHandler != null) {
r.fHandler.handle(UserDataHandler.NODE_DELETED,
key, r.fData, null, null);
}
}
}
}
}*/
protected final void checkNamespaceWF( String qname, int colon1,
int colon2) {
if (!errorChecking) {
return;
}
// it is an error for NCName to have more than one ':'
// check if it is valid QName [Namespace in XML production 6]
// :camera , nikon:camera:minolta, camera:
if (colon1 == 0 || colon1 == qname.length() - 1 || colon2 != colon1) {
String msg =
DOMMessageFormatter.formatMessage(
DOMMessageFormatter.DOM_DOMAIN,
"NAMESPACE_ERR",
null);
throw new DOMException(DOMException.NAMESPACE_ERR, msg);
}
}
public Node appendChild(Node arg0) throws DOMException {
return membrane.wrap(
underlying.appendChild(membrane.unwrap(arg0, Node.class)),
Node.class);
}
@Override
public String getBaseURI() {
throw new DOMException(DOMException.INVALID_ACCESS_ERR, ACTION_NOT_IMPLEMENTED);
}
private static void parseDocument(Configuration conf, Document doc) throws IOException {
try {
Element root = doc.getDocumentElement();
if (!"configuration".equals(root.getTagName())) {
throw new IOException("bad conf file: top-level element not <configuration>");
}
NodeList props = root.getChildNodes();
for (int i = 0; i < props.getLength(); i++) {
Node propNode = props.item(i);
if (!(propNode instanceof Element)) {
continue;
}
Element prop = (Element) propNode;
if (!"property".equals(prop.getTagName())) {
throw new IOException("bad conf file: element not <property>");
}
NodeList fields = prop.getChildNodes();
String attr = null;
String value = null;
for (int j = 0; j < fields.getLength(); j++) {
Node fieldNode = fields.item(j);
if (!(fieldNode instanceof Element)) {
continue;
}
Element field = (Element) fieldNode;
if ("name".equals(field.getTagName()) && field.hasChildNodes()) {
attr = ((Text) field.getFirstChild()).getData().trim();
}
if ("value".equals(field.getTagName()) && field.hasChildNodes()) {
value = ((Text) field.getFirstChild()).getData();
}
}
if (attr != null && value != null) {
conf.set(attr, value);
}
}
} catch (DOMException e) {
throw new IOException(e);
}
}
@Override
public String getWholeText() {
throw new DOMException(DOMException.INVALID_ACCESS_ERR, SVGDocument.ACTION_NOT_IMPLEMENTED);
}
/***/
public Node removeNamedItem(String name)
throws DOMException {
return internalRemoveNamedItem(name, true);
}