📄 nodecreaterule.java
字号:
* @param data the processing instruction data, or null if none was
* supplied
* @throws SAXException if the DOM implementation throws an exception
*/
public void processingInstruction(String target, String data)
throws SAXException {
try {
top.appendChild(doc.createProcessingInstruction(target, data));
} catch (DOMException e) {
throw new SAXException(e.getMessage());
}
}
/**
* Adds a new child {@link org.w3c.dom.Element Element} to the current
* node.
*
* @param namespaceURI the namespace URI
* @param localName the local name
* @param qName the qualified (prefixed) name
* @param atts the list of attributes
* @throws SAXException if the DOM implementation throws an exception
*/
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts)
throws SAXException {
try {
Node previousTop = top;
if ((localName == null) || (localName.length() == 0)) {
top = doc.createElement(qName);
} else {
top = doc.createElementNS(namespaceURI, localName);
}
for (int i = 0; i < atts.getLength(); i++) {
Attr attr = null;
if ((atts.getLocalName(i) == null) ||
(atts.getLocalName(i).length() == 0)) {
attr = doc.createAttribute(atts.getQName(i));
attr.setNodeValue(atts.getValue(i));
((Element)top).setAttributeNode(attr);
} else {
attr = doc.createAttributeNS(atts.getURI(i),
atts.getLocalName(i));
attr.setNodeValue(atts.getValue(i));
((Element)top).setAttributeNodeNS(attr);
}
}
previousTop.appendChild(top);
depth++;
} catch (DOMException e) {
throw new SAXException(e.getMessage());
}
}
}
// ----------------------------------------------------------- Constructors
/**
* Default constructor. Creates an instance of this rule that will create a
* DOM {@link org.w3c.dom.Element Element}.
*/
public NodeCreateRule() throws ParserConfigurationException {
this(Node.ELEMENT_NODE);
}
/**
* Constructor. Creates an instance of this rule that will create a DOM
* {@link org.w3c.dom.Element Element}, but lets you specify the JAXP
* <code>DocumentBuilder</code> that should be used when constructing the
* node tree.
*
* @param documentBuilder the JAXP <code>DocumentBuilder</code> to use
*/
public NodeCreateRule(DocumentBuilder documentBuilder) {
this(Node.ELEMENT_NODE, documentBuilder);
}
/**
* Constructor. Creates an instance of this rule that will create either a
* DOM {@link org.w3c.dom.Element Element} or a DOM
* {@link org.w3c.dom.DocumentFragment DocumentFragment}, depending on the
* value of the <code>nodeType</code> parameter.
*
* @param nodeType the type of node to create, which can be either
* {@link org.w3c.dom.Node#ELEMENT_NODE Node.ELEMENT_NODE} or
* {@link org.w3c.dom.Node#DOCUMENT_FRAGMENT_NODE Node.DOCUMENT_FRAGMENT_NODE}
* @throws IllegalArgumentException if the node type is not supported
*/
public NodeCreateRule(int nodeType) throws ParserConfigurationException {
this(nodeType,
DocumentBuilderFactory.newInstance().newDocumentBuilder());
}
/**
* Constructor. Creates an instance of this rule that will create either a
* DOM {@link org.w3c.dom.Element Element} or a DOM
* {@link org.w3c.dom.DocumentFragment DocumentFragment}, depending on the
* value of the <code>nodeType</code> parameter. This constructor lets you
* specify the JAXP <code>DocumentBuilder</code> that should be used when
* constructing the node tree.
*
* @param nodeType the type of node to create, which can be either
* {@link org.w3c.dom.Node#ELEMENT_NODE Node.ELEMENT_NODE} or
* {@link org.w3c.dom.Node#DOCUMENT_FRAGMENT_NODE Node.DOCUMENT_FRAGMENT_NODE}
* @param documentBuilder the JAXP <code>DocumentBuilder</code> to use
* @throws IllegalArgumentException if the node type is not supported
*/
public NodeCreateRule(int nodeType, DocumentBuilder documentBuilder) {
if (!((nodeType == Node.DOCUMENT_FRAGMENT_NODE) ||
(nodeType == Node.ELEMENT_NODE))) {
throw new IllegalArgumentException(
"Can only create nodes of type DocumentFragment and Element");
}
this.nodeType = nodeType;
this.documentBuilder = documentBuilder;
}
// ----------------------------------------------------- Instance Variables
/**
* The JAXP <code>DocumentBuilder</code> to use.
*/
private DocumentBuilder documentBuilder = null;
/**
* The type of the node that should be created. Must be one of the
* constants defined in {@link org.w3c.dom.Node Node}, but currently only
* {@link org.w3c.dom.Node#ELEMENT_NODE Node.ELEMENT_NODE} and
* {@link org.w3c.dom.Node#DOCUMENT_FRAGMENT_NODE Node.DOCUMENT_FRAGMENT_NODE}
* are allowed values.
*/
private int nodeType = Node.ELEMENT_NODE;
// ----------------------------------------------------------- Rule Methods
/**
* Implemented to replace the content handler currently in use by a
* NodeBuilder.
*
* @param namespaceURI the namespace URI of the matching element, or an
* empty string if the parser is not namespace aware or the element has
* no namespace
* @param name the local name if the parser is namespace aware, or just
* the element name otherwise
* @param attributes The attribute list of this element
* @throws Exception indicates a JAXP configuration problem
*/
public void begin(String namespaceURI, String name, Attributes attributes)
throws Exception {
XMLReader xmlReader = getDigester().getXMLReader();
Document doc = documentBuilder.newDocument();
NodeBuilder builder = null;
if (nodeType == Node.ELEMENT_NODE) {
Element element = null;
if (getDigester().getNamespaceAware()) {
element =
doc.createElementNS(namespaceURI, name);
for (int i = 0; i < attributes.getLength(); i++) {
element.setAttributeNS(attributes.getURI(i),
attributes.getLocalName(i),
attributes.getValue(i));
}
} else {
element = doc.createElement(name);
for (int i = 0; i < attributes.getLength(); i++) {
element.setAttribute(attributes.getQName(i),
attributes.getValue(i));
}
}
builder = new NodeBuilder(doc, element);
} else {
builder = new NodeBuilder(doc, doc.createDocumentFragment());
}
xmlReader.setContentHandler(builder);
}
/**
* Pop the Node off the top of the stack.
*/
public void end() throws Exception {
Object top = digester.pop();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -