📄 saxmarshaller.java
字号:
* <p> * This method can be called (i) after the startAttribute method * and (ii) before the endAttribute method, to marshal attribute values. * If the method is called more than once, those texts are considered * as separated by whitespaces. For example, * * <pre> * c.startAttribute(); * c.text("abc"); * c.text("def"); * c.endAttribute("","foo"); * </pre> * * will generate foo="abc def". * * <p> * Similarly, this method can be called after the endAttributes * method to marshal texts inside elements. The same rule about * multiple invokations apply to this case, too. For example, * * <pre> * c.startElement("","foo"); * c.endAttributes(); * c.text("abc"); * c.text("def"); * c.startElement("","bar"); * c.endAttributes(); * c.endElement(); * c.text("ghi"); * c.endElement(); * </pre> * * will generate <code><foo>abc def<bar/>ghi</foo></code>. */ public void text( String text, String fieldName ) throws SAXException { // If the assertion fails, it must be a bug of xjc. // right now, we are not expecting the text method to be called. if(text==null) { reportError(Util.createMissingObjectError(currentTarget,fieldName)); return; } if(textBuf.length()!=0) textBuf.append(' '); textBuf.append(text); } /** * Writes pending text (characters inside elements) to the writer. * This method is called from startElement and endElement. */ private void writePendingText() throws SAXException { // assert(textBuf!=null); int len = textBuf.length(); if(len!=0) writer.characters( textBuf.toString().toCharArray(), 0, len ); } /** * Starts marshalling of an attribute. * * The marshalling of an attribute will be done by * <ol> * <li>call the startAttribute method * <li>call the text method (several times if necessary) * <li>call the endAttribute method * </ol> * * No two attributes can be marshalled at the same time. * Note that the whole attribute marshalling must be happened * after the startElement method and before the endAttributes method. */ public void startAttribute( String uri, String local ) { // initialize the buffer to collect attribute value textBuf.setLength(0); // remember the attribute name. We'll use this value later. this.attNamespaceUri = uri; this.attLocalName = local; } // used to keep attribute names until the endAttribute method is called. private String attNamespaceUri; private String attLocalName; public void endAttribute() { // use CDATA as the attribute type. This preserves // successive processors to collapse whitespaces. // (we cannot prevent characters like #xD to be replaced to // #x20, though). // // strictly speaking, attribute value normalization should be // provessed by XML parser, so it's unclear whether XML writer // uses this type value. // // in any way, CDATA type is the safest choice here. String qname; if(attNamespaceUri.length()==0) { // default namespace. don't need prefix qname = attLocalName; } else { qname = nsContext.declareNamespace(attNamespaceUri,null,true)+':'+attLocalName; } attributes.addAttribute(attNamespaceUri,attLocalName,qname,"CDATA",textBuf.toString()); } public String onID( IdentifiableObject owner, String value ) throws SAXException { objectsWithId.add(owner); return value; } public String onIDREF( IdentifiableObject obj ) throws SAXException { idReferencedObjects.add(obj); String id = obj.____jaxb____getId(); if(id==null) { reportError( new NotIdentifiableEventImpl( ValidationEvent.ERROR, Messages.format(Messages.ERR_NOT_IDENTIFIABLE), new ValidationEventLocatorImpl(obj) ) ); } return id; } void reconcileID() throws AbortSerializationException { // find objects that were not a part of the object graph idReferencedObjects.removeAll(objectsWithId); for( Iterator itr=idReferencedObjects.iterator(); itr.hasNext(); ) { IdentifiableObject o = (IdentifiableObject)itr.next(); reportError( new NotIdentifiableEventImpl( ValidationEvent.ERROR, Messages.format(Messages.ERR_DANGLING_IDREF,o.____jaxb____getId()), new ValidationEventLocatorImpl(o) ) ); } // clear the garbage idReferencedObjects.clear(); objectsWithId.clear(); } public void childAsBody( JAXBObject o, String fieldName ) throws SAXException { if(o==null) { // if null is passed, it usually means that the content tree object // doesn't have some of its required property. reportMissingObjectError(fieldName); // as a marshaller, we should be generous, so we'll continue to marshal // this document by skipping this missing object. return; } JAXBObject oldTarget = currentTarget; currentTarget = o; owner.context.getGrammarInfo().castToXMLSerializable(o).serializeBody(this); currentTarget = oldTarget; } public void childAsAttributes( JAXBObject o, String fieldName ) throws SAXException { if(o==null) { reportMissingObjectError(fieldName); return; } JAXBObject oldTarget = currentTarget; currentTarget = o; owner.context.getGrammarInfo().castToXMLSerializable(o).serializeAttributes(this); currentTarget = oldTarget; } public void childAsURIs( JAXBObject o, String fieldName ) throws SAXException { if(o==null) { reportMissingObjectError(fieldName); return; } JAXBObject oldTarget = currentTarget; currentTarget = o; owner.context.getGrammarInfo().castToXMLSerializable(o).serializeURIs(this); currentTarget = oldTarget; } public void reportError( ValidationEvent ve ) throws AbortSerializationException { ValidationEventHandler handler; try { handler = owner.getEventHandler(); } catch( JAXBException e ) { throw new AbortSerializationException(e); } if(!handler.handleEvent(ve)) { if(ve.getLinkedException() instanceof Exception) throw new AbortSerializationException((Exception)ve.getLinkedException()); else throw new AbortSerializationException(ve.getMessage()); } } public void reportMissingObjectError(String fieldName) throws SAXException { reportError(Util.createMissingObjectError(currentTarget,fieldName)); } private static void _assert( boolean b ) { if(!b) throw new JAXBAssertionError(); } /** * Default {@link NamespacePrefixMapper} implementation used when * it is not specified by the user. */ private static NamespacePrefixMapper defaultNamespacePrefixMapper = new NamespacePrefixMapper() { public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) { if( namespaceUri.equals("http://www.w3.org/2001/XMLSchema-instance") ) return "xsi"; return suggestion; } };}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -