📄 saxmarshaller.java
字号:
//// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v1.0.4-b18-fcs // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> // Any modifications to this file will be lost upon recompilation of the source schema. // Generated on: 2005.07.17 at 02:32:59 EDT //package jwsgrid.xsd.jobhostsearchresult.impl.runtime;import java.util.HashSet;import java.util.Iterator;import java.util.Set;import javax.xml.bind.JAXBException;import javax.xml.bind.ValidationEvent;import javax.xml.bind.ValidationEventHandler;import javax.xml.bind.helpers.NotIdentifiableEventImpl;import javax.xml.bind.helpers.ValidationEventLocatorImpl;import org.xml.sax.ContentHandler;import org.xml.sax.SAXException;import org.xml.sax.helpers.AttributesImpl;import com.sun.xml.bind.JAXBAssertionError;import com.sun.xml.bind.JAXBObject;import com.sun.xml.bind.marshaller.IdentifiableObject;import com.sun.xml.bind.marshaller.Messages;import com.sun.xml.bind.marshaller.NamespacePrefixMapper;import com.sun.xml.bind.serializer.AbortSerializationException;import com.sun.xml.bind.serializer.Util;/** * XMLSerializer that produces SAX2 events. * * To marshal an object, create an instance of SAXMarshaller * and call the serializeElements method of the XMLSerializable * object that you want to marshal. * * @author Kohsuke Kawaguchi */public class SAXMarshaller implements XMLSerializer{ /** * "Attributes" object that is passed to the startElement event. * One object is reused throughout the marshalling. */ private final AttributesImpl attributes = new AttributesImpl(); /** This object receives SAX2 events generated from the marshaller. */ private final ContentHandler writer; /** Marshaller object to which this object belongs. */ private final MarshallerImpl owner; /** Objects referenced through IDREF. */ private final Set idReferencedObjects = new HashSet(); /** Objects with ID. */ private final Set objectsWithId = new HashSet(); /** Object currently marshalling itself. */ private JAXBObject currentTarget; /** * Creates a marshalling context by designating the ContentHandler * that receives generated SAX2 events. */ public SAXMarshaller( ContentHandler _writer, NamespacePrefixMapper prefixMapper, MarshallerImpl _owner ) { this.writer = _writer; this.owner = _owner; this.nsContext = new NamespaceContextImpl( prefixMapper!=null?prefixMapper:defaultNamespacePrefixMapper); } /** namespace context. */ private final NamespaceContextImpl nsContext; public NamespaceContext2 getNamespaceContext() { return nsContext; } // // // name stack // // /** Element name stack implemented as an array of (uri,local) pairs. */ private String[] elementStack = new String[16];; private int elementLen=0; private void pushElement( String uri, String local ) { if(elementStack.length==elementLen) { // reallocate buffer String[] buf = new String[elementStack.length*2]; System.arraycopy( elementStack, 0, buf, 0, elementStack.length ); elementStack = buf; } elementStack[elementLen++] = uri; elementStack[elementLen++] = local; } private void popElement() { elementLen-=2; } private String getCurrentElementUri() { return elementStack[elementLen-2]; } private String getCurrentElementLocal() { return elementStack[elementLen-1]; } /** * Starts marshalling of an element. * Calling this method will push the internal state into the * internal stack. */ public void startElement( String uri, String local ) throws SAXException { boolean isRoot = false; String suggestion = null; if( elementLen==0 ) { isRoot = true; // this is the root element. suggest this as the default namespace suggestion = ""; } writePendingText(); nsContext.startElement(); pushElement(uri,local); // memorize element name // declare this uri nsContext.declareNamespace(uri,suggestion,false); // if this is the root element, declare user-specified namespace URIs. if( isRoot ) { // work defensively. we are calling an user-defined method. String[] uris = nsContext.getNamespacePrefixMapper().getPreDeclaredNamespaceUris(); if( uris!=null ) { for( int i=0; i<uris.length; i++ ) { if( uris[i]!=null ) nsContext.declareNamespace(uris[i],null,false); } } } } private final PrefixCallback startPrefixCallback = new PrefixCallback() { public void onPrefixMapping( String prefix, String nsUri ) throws SAXException { writer.startPrefixMapping(prefix,nsUri); } }; private final PrefixCallback endPrefixCallback = new PrefixCallback() { public void onPrefixMapping( String prefix, String nsUri ) throws SAXException { writer.endPrefixMapping(prefix); } }; public void endNamespaceDecls() throws SAXException { nsContext.endNamespaceDecls(); } /** * Switches to the "marshal child texts/elements" mode. * This method has to be called after the 1st pass is completed. */ public void endAttributes() throws SAXException { // calculate QName of the element String uri = getCurrentElementUri(); String local = getCurrentElementLocal(); String prefix = nsContext.getPrefix(uri); _assert(prefix!=null); // since we've declared it, it should be available String qname; if(prefix.length()!=0 ) qname = prefix+':'+local; else qname = local; // fire startPrefixMapping events nsContext.iterateDeclaredPrefixes(startPrefixCallback); // fire the startElement event writer.startElement( uri, local, qname, attributes ); // reset attributes attributes.clear(); // prepare to collect texts textBuf.setLength(0); } /** * Ends marshalling of an element. * Pops the internal stack. */ public void endElement() throws SAXException { writePendingText(); String uri = getCurrentElementUri(); String local = getCurrentElementLocal(); String prefix = nsContext.getPrefix(uri); _assert(prefix!=null); // we've declared it earlier. String qname; if(prefix.length()!=0) qname = prefix+':'+local; else qname = local; writer.endElement( uri, local, qname ); // pop namespace bindings and // fire endPrefixMapping events nsContext.iterateDeclaredPrefixes(endPrefixCallback); popElement(); // prepare to collect texts textBuf.setLength(0); nsContext.endElement(); } /** Buffer for collecting characters. */ private final StringBuffer textBuf = new StringBuffer(); /** * Marshalls text. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -