📄 saxwriter.java
字号:
setEntityResolver( xmlReader.getEntityResolver() ); setErrorHandler( xmlReader.getErrorHandler() ); } /** Looks up the value of a feature. */ public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException { Boolean answer = (Boolean) features.get(name); return answer != null && answer.booleanValue(); } /** This implementation does actually use any features but just * stores them for later retrieval */ public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException { if ( FEATURE_NAMESPACE_PREFIXES.equals( name ) ) { setDeclareNamespaceAttributes( value ); } else if ( FEATURE_NAMESPACE_PREFIXES.equals( name ) ) { if ( ! value ) { throw new SAXNotSupportedException(name + ". namespace feature is always supported in dom4j." ); } } features.put(name, (value) ? Boolean.TRUE : Boolean.FALSE ); } /** Sets the given SAX property */ public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException { for (int i = 0; i < LEXICAL_HANDLER_NAMES.length; i++) { if (LEXICAL_HANDLER_NAMES[i].equals(name)) { setLexicalHandler((LexicalHandler) value); return; } } properties.put(name, value); } /** Gets the given SAX property */ public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException { for (int i = 0; i < LEXICAL_HANDLER_NAMES.length; i++) { if (LEXICAL_HANDLER_NAMES[i].equals(name)) { return getLexicalHandler(); } } return properties.get(name); } /** This method is not supported. */ public void parse(String systemId) throws SAXNotSupportedException { throw new SAXNotSupportedException( "This XMLReader can only accept <dom4j> InputSource objects" ); } /** Parses an XML document. * This method can only accept DocumentInputSource inputs * otherwise a {@link SAXNotSupportedException} exception is thrown. * * @throws SAXNotSupportedException * if the input source is not wrapping a dom4j document */ public void parse(InputSource input) throws SAXException { if (input instanceof DocumentInputSource) { DocumentInputSource documentInput = (DocumentInputSource) input; Document document = documentInput.getDocument(); write( document ); } else { throw new SAXNotSupportedException( "This XMLReader can only accept <dom4j> InputSource objects" ); } } // Implementation methods //------------------------------------------------------------------------- protected void writeContent( Branch branch, NamespaceStack namespaceStack ) throws SAXException { for ( Iterator iter = branch.nodeIterator(); iter.hasNext(); ) { Object object = iter.next(); if ( object instanceof Element ) { write( (Element) object, namespaceStack ); } else if ( object instanceof CharacterData ) { if ( object instanceof Text ) { Text text = (Text) object; write( text.getText() ); } else if ( object instanceof CDATA ) { write( (CDATA) object ); } else if ( object instanceof Comment ) { write( (Comment) object ); } else { throw new SAXException( "Invalid Node in DOM4J content: " + object + " of type: " + object.getClass() ); } } else if ( object instanceof String ) { write( (String) object ); } else if ( object instanceof Entity ) { write( (Entity) object ); } else if ( object instanceof ProcessingInstruction ) { write( (ProcessingInstruction) object ); } else if ( object instanceof Namespace ) { // namespaceStack are written via write of element } else { throw new SAXException( "Invalid Node in DOM4J content: " + object ); } } } /** The {@link org.xml.sax.Locator} is only really useful when parsing a textual * document as its main purpose is to identify the line and column number. * Since we are processing an in memory tree which will probably have * its line number information removed, we'll just use -1 for the line * and column numbers. */ protected void documentLocator(Document document) throws SAXException { LocatorImpl locator = new LocatorImpl(); String publicID = null; String systemID = null; DocumentType docType = document.getDocType(); if (docType != null) { publicID = docType.getPublicID(); systemID = docType.getSystemID(); } if ( publicID != null ) { locator.setPublicId(publicID); } if ( systemID != null ) { locator.setSystemId(systemID); } locator.setLineNumber(-1); locator.setColumnNumber(-1); contentHandler.setDocumentLocator( locator ); } protected void entityResolver(Document document) throws SAXException { if (entityResolver != null) { DocumentType docType = document.getDocType(); if (docType != null) { String publicID = docType.getPublicID(); String systemID = docType.getSystemID(); if ( publicID != null || systemID != null ) { try { entityResolver.resolveEntity( publicID, systemID ); } catch (IOException e) { throw new SAXException( "Could not resolve entity publicID: " + publicID + " systemID: " + systemID, e ); } } } } } /** We do not yet support DTD or XML Schemas so this method does nothing * right now. */ protected void dtdHandler(Document document) throws SAXException { } protected void startDocument() throws SAXException { contentHandler.startDocument(); } protected void endDocument() throws SAXException { contentHandler.endDocument(); } protected void write( Element element, NamespaceStack namespaceStack ) throws SAXException { int stackSize = namespaceStack.size(); AttributesImpl namespaceAttributes = startPrefixMapping(element, namespaceStack); startElement(element, namespaceAttributes); writeContent(element, namespaceStack); endElement(element); endPrefixMapping(namespaceStack, stackSize); } /** Fires a SAX startPrefixMapping event for all the namespaceStack * which have just come into scope */ protected AttributesImpl startPrefixMapping( Element element, NamespaceStack namespaceStack ) throws SAXException { AttributesImpl namespaceAttributes = null; List declaredNamespaces = element.declaredNamespaces(); for ( int i = 0, size = declaredNamespaces.size(); i < size ; i++ ) { Namespace namespace = (Namespace) declaredNamespaces.get(i); if ( ! isIgnoreableNamespace( namespace, namespaceStack ) ) { namespaceStack.push( namespace ); contentHandler.startPrefixMapping( namespace.getPrefix(), namespace.getURI() ); namespaceAttributes = addNamespaceAttribute( namespaceAttributes, namespace ); } } return namespaceAttributes; } /** Fires a SAX endPrefixMapping event for all the namespaceStack which * have gone out of scope */ protected void endPrefixMapping( NamespaceStack namespaceStack, int stackSize ) throws SAXException { while ( namespaceStack.size() > stackSize ) { Namespace namespace = namespaceStack.pop(); if ( namespace != null ) { contentHandler.endPrefixMapping( namespace.getPrefix() ); } } } protected void startElement( Element element, AttributesImpl namespaceAttributes ) throws SAXException { contentHandler.startElement( element.getNamespaceURI(), element.getName(), element.getQualifiedName(), createAttributes( element, namespaceAttributes ) ); } protected void endElement( Element element ) throws SAXException { contentHandler.endElement( element.getNamespaceURI(), element.getName(), element.getQualifiedName() ); } protected Attributes createAttributes( Element element, Attributes namespaceAttributes ) throws SAXException { attributes.clear(); if ( namespaceAttributes != null ) { attributes.setAttributes( namespaceAttributes ); } for ( Iterator iter = element.attributeIterator(); iter.hasNext(); ) { Attribute attribute = (Attribute) iter.next(); attributes.addAttribute( attribute.getNamespaceURI(), attribute.getName(), attribute.getQualifiedName(), "CDATA", attribute.getValue() ); } return attributes; } /** If isDelcareNamespaceAttributes() is enabled then this method will add the * given namespace declaration to the supplied attributes object, creating one if * it does not exist. */ protected AttributesImpl addNamespaceAttribute( AttributesImpl namespaceAttributes, Namespace namespace ) { if ( declareNamespaceAttributes ) { if ( namespaceAttributes == null ) { namespaceAttributes = new AttributesImpl(); } String prefix = namespace.getPrefix(); String qualifiedName = "xmlns"; if ( prefix != null && prefix.length() > 0 ) { qualifiedName = "xmlns:" + prefix; } String uri = ""; String localName = prefix; String type = "CDATA"; String value = namespace.getURI(); namespaceAttributes.addAttribute( uri, localName, qualifiedName, type, value ); } return namespaceAttributes; } /** @return true if the given namespace is an ignorable namespace * (such as AbstractNamespace.NO_NAMESPACE or AbstractNamespace.XML_NAMESPACE) or if the * namespace has already been declared in the current scope */ protected boolean isIgnoreableNamespace( Namespace namespace, NamespaceStack namespaceStack ) { if ( namespace.equals( AbstractNamespace.NO_NAMESPACE ) || namespace.equals( AbstractNamespace.XML_NAMESPACE ) ) { return true; } String uri = namespace.getURI(); if ( uri == null || uri.length() <= 0 ) { return true; } return namespaceStack.contains( namespace ); } /** Ensures non-null content handlers? */ protected void checkForNullHandlers() { }}/* * Redistribution and use of this software and associated documentation * ("Software"), with or without modification, are permitted provided * that the following conditions are met: * * 1. Redistributions of source code must retain copyright * statements and notices. Redistributions must also contain a * copy of this document. * * 2. 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. * * 3. The name "DOM4J" must not be used to endorse or promote * products derived from this Software without prior written * permission of MetaStuff, Ltd. For written permission, * please contact dom4j-info@metastuff.com. * * 4. Products derived from this Software may not be called "DOM4J" * nor may "DOM4J" appear in their names without prior written * permission of MetaStuff, Ltd. DOM4J is a registered * trademark of MetaStuff, Ltd. * * 5. Due credit should be given to the DOM4J Project * (http://dom4j.org/). * * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY 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 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. * * $Id: SAXWriter.java,v 1.4 2003/11/02 18:04:59 per_nyfelt Exp $ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -