?? dommapperbase.java
字號:
/*Copyright (c) 2004, Dennis M. SosnoskiAll rights reserved.Redistribution and use in source and binary forms, with or without modification,are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of JiBX nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" ANDANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIEDWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AREDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FORANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ONANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THISSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/package org.jibx.extras;import java.io.IOException;import java.util.ArrayList;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import org.jibx.runtime.IXMLReader;import org.jibx.runtime.JiBXException;import org.w3c.dom.Attr;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.NodeList;/** * <p>Base implementation for custom marshaller/unmarshallers to DOM * representation. This provides the basic code used for both single element and * content list handling.</p> * * @author Dennis M. Sosnoski * @version 1.0 */public class DomMapperBase extends DocumentModelMapperBase{ /** Actual document instance (required by DOM). */ protected Document m_document; /** Current default namespace URI (<code>null</code> if not determined). */ protected String m_defaultNamespaceURI; /** Current default namespace index. */ protected int m_defaultNamespaceIndex; /** * Constructor. Initializes the document used by this * marshaller/unmarshaller instance as the owner of all DOM components. * * @throws JiBXException on error creating document */ protected DomMapperBase() throws JiBXException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); try { m_document = dbf.newDocumentBuilder().newDocument(); } catch (ParserConfigurationException e) { throw new JiBXException("Unable to create DOM document", e); } } /** * Get index number for declared namespace. * * @param prefix namespace prefix (<code>null</code> if none) * @param uri namespace URI (empty string if none) * @return namespace index number, or <code>-1</code> if not declared or * masked */ private int findNamespaceIndex(String prefix, String uri) { if ((prefix == null || "".equals(prefix)) && (uri == null || "".equals(uri))) { return 0; } else if ("xml".equals(prefix) && XML_NAMESPACE.equals(uri)) { return 1; } else { if (prefix == null) { if (m_defaultNamespaceURI == null) { int index = m_xmlWriter.getPrefixIndex(""); if (index >= 0) { m_defaultNamespaceURI = getNamespaceUri(index); m_defaultNamespaceIndex = index; if (m_defaultNamespaceURI.equals(uri)) { return index; } else { return -1; } } else { return -1; } } else { return m_defaultNamespaceURI.equals(uri) ? m_defaultNamespaceIndex : -1; } } else { int index = m_xmlWriter.getPrefixIndex(prefix); if (index >= 0) { return getNamespaceUri(index).equals(uri) ? index : -1; } else { return -1; } } } } /** * Marshal node. * * @param node node to be marshalled * @exception JiBXException on error in marshalling * @exception IOException on error writing to output */ protected void marshalNode(Node node) throws JiBXException, IOException { switch (node.getNodeType()) { case Node.CDATA_SECTION_NODE: m_xmlWriter.writeCData(node.getNodeValue()); break; case Node.COMMENT_NODE: m_xmlWriter.writeComment(node.getNodeValue()); break; case Node.ELEMENT_NODE: marshalElement((Element)node); break; case Node.ENTITY_REFERENCE_NODE: m_xmlWriter.writeEntityRef(node.getNodeName()); break; case Node.PROCESSING_INSTRUCTION_NODE: m_xmlWriter.writePI(node.getNodeName(), node.getNodeValue()); break; case Node.TEXT_NODE: m_xmlWriter.writeTextContent(node.getNodeValue()); break; default: break; } } /** * Marshal node list. * * @param content list of nodes to marshal * @exception JiBXException on error in marshalling * @exception IOException on error writing to output */ protected void marshalContent(NodeList content) throws JiBXException, IOException { int size = content.getLength(); for (int i = 0; i < size; i++) { marshalNode(content.item(i)); } } /** * Marshal element with all attributes and content. * * @param element element to be marshalled * @exception JiBXException on error in marshalling * @exception IOException on error writing to output */ protected void marshalElement(Element element) throws JiBXException, IOException { // accumulate all needed namespace declarations String prefix = element.getPrefix(); String uri = element.getNamespaceURI(); int nsi = findNamespaceIndex(prefix, uri); ArrayList nss = null; int defind = -1; String defuri = null; NamedNodeMap attrs = element.getAttributes(); int size = attrs.getLength(); for (int i = 0; i < size; i++) { Attr attr = (Attr)attrs.item(i); if (XMLNS_NAMESPACE.equals(attr.getNamespaceURI())) { // found namespace declaration, convert to simple prefix String declpref = attr.getLocalName(); if ("xmlns".equals(declpref)) { declpref = null; } String decluri = attr.getValue(); if (findNamespaceIndex(declpref, decluri) < 0) { if (nss == null) { nss = new ArrayList(); } nss.add(declpref == null ? "" : declpref); nss.add(decluri == null ? "" : decluri); if (declpref == null) { defind = (nss.size() / 2) - 1;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -