下面列出了javax.xml.bind.Unmarshaller#setListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Template method that can be overridden by concrete JAXB marshallers
* for custom initialization behavior. Gets called after creation of JAXB
* {@code Marshaller}, and after the respective properties have been set.
* <p>The default implementation sets the
* {@link #setUnmarshallerProperties defined properties}, the
* {@link #setValidationEventHandler validation event handler}, the
* {@link #setSchemas schemas}, {@link #setUnmarshallerListener listener},
* and {@link #setAdapters adapters}.
*/
protected void initJaxbUnmarshaller(Unmarshaller unmarshaller) throws JAXBException {
if (this.unmarshallerProperties != null) {
for (String name : this.unmarshallerProperties.keySet()) {
unmarshaller.setProperty(name, this.unmarshallerProperties.get(name));
}
}
if (this.unmarshallerListener != null) {
unmarshaller.setListener(this.unmarshallerListener);
}
if (this.validationEventHandler != null) {
unmarshaller.setEventHandler(this.validationEventHandler);
}
if (this.adapters != null) {
for (XmlAdapter<?, ?> adapter : this.adapters) {
unmarshaller.setAdapter(adapter);
}
}
if (this.schema != null) {
unmarshaller.setSchema(this.schema);
}
}
/**
* Template method that can be overridden by concrete JAXB marshallers for custom initialization behavior.
* Gets called after creation of JAXB {@code Marshaller}, and after the respective properties have been set.
* <p>The default implementation sets the {@link #setUnmarshallerProperties(Map) defined properties}, the {@link
* #setValidationEventHandler(ValidationEventHandler) validation event handler}, the {@link #setSchemas(Resource[])
* schemas}, {@link #setUnmarshallerListener(javax.xml.bind.Unmarshaller.Listener) listener}, and
* {@link #setAdapters(XmlAdapter[]) adapters}.
*/
protected void initJaxbUnmarshaller(Unmarshaller unmarshaller) throws JAXBException {
if (this.unmarshallerProperties != null) {
for (String name : this.unmarshallerProperties.keySet()) {
unmarshaller.setProperty(name, this.unmarshallerProperties.get(name));
}
}
if (this.unmarshallerListener != null) {
unmarshaller.setListener(this.unmarshallerListener);
}
if (this.validationEventHandler != null) {
unmarshaller.setEventHandler(this.validationEventHandler);
}
if (this.adapters != null) {
for (XmlAdapter<?, ?> adapter : this.adapters) {
unmarshaller.setAdapter(adapter);
}
}
if (this.schema != null) {
unmarshaller.setSchema(this.schema);
}
}
/**
* Template method that can be overridden by concrete JAXB marshallers for custom initialization behavior.
* Gets called after creation of JAXB {@code Marshaller}, and after the respective properties have been set.
* <p>The default implementation sets the {@link #setUnmarshallerProperties(Map) defined properties}, the {@link
* #setValidationEventHandler(ValidationEventHandler) validation event handler}, the {@link #setSchemas(Resource[])
* schemas}, {@link #setUnmarshallerListener(javax.xml.bind.Unmarshaller.Listener) listener}, and
* {@link #setAdapters(XmlAdapter[]) adapters}.
*/
protected void initJaxbUnmarshaller(Unmarshaller unmarshaller) throws JAXBException {
if (this.unmarshallerProperties != null) {
for (String name : this.unmarshallerProperties.keySet()) {
unmarshaller.setProperty(name, this.unmarshallerProperties.get(name));
}
}
if (this.unmarshallerListener != null) {
unmarshaller.setListener(this.unmarshallerListener);
}
if (this.validationEventHandler != null) {
unmarshaller.setEventHandler(this.validationEventHandler);
}
if (this.adapters != null) {
for (XmlAdapter<?, ?> adapter : this.adapters) {
unmarshaller.setAdapter(adapter);
}
}
if (this.schema != null) {
unmarshaller.setSchema(this.schema);
}
}
public static Unmarshaller createUnmarshaller(ValidationEventCollector vec) throws JAXBException {
JAXBContext jc = createPageJAXBContext();
Unmarshaller u = jc.createUnmarshaller();
try {
u.setProperty("com.sun.xml.internal.bind.ObjectFactory", new TrpObjectFactory());
} catch(PropertyException pe) {
u.setProperty("com.sun.xml.bind.ObjectFactory", new TrpObjectFactory());
}
u.setListener(new TrpPageUnmarshalListener());
if(vec != null) {
u.setEventHandler(vec);
}
return u;
}
protected Unmarshaller createUnmarshaller(Class<?> cls, Type genericType, boolean isCollection)
throws JAXBException {
JAXBContext context = isCollection ? getCollectionContext(cls)
: getJAXBContext(cls, genericType);
Unmarshaller unmarshaller = context.createUnmarshaller();
if (validateInputIfPossible) {
Schema theSchema = getSchema(cls);
if (theSchema != null) {
unmarshaller.setSchema(theSchema);
}
}
if (eventHandler != null) {
unmarshaller.setEventHandler(eventHandler);
}
if (unmarshallerListener != null) {
unmarshaller.setListener(unmarshallerListener);
}
if (uProperties != null) {
for (Map.Entry<String, Object> entry : uProperties.entrySet()) {
unmarshaller.setProperty(entry.getKey(), entry.getValue());
}
}
return unmarshaller;
}
public <T> T fromXML( final InputStream in, final Unmarshaller.Listener listener ) {
try {
final JAXBContext jc = createJAXBContext( additionalClazzes );
final Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setEventHandler( new DefaultValidationEventHandler() );
unmarshaller.setListener( listener );
@SuppressWarnings( "unchecked" )
final T result = (T) unmarshaller.unmarshal( in );
return result;
} catch ( final JAXBException e ) {
throw new RuntimeException( e );
}
}
/**
* Unmarshal the provided XML stream to allocate the corresponding sheet.
*
* @param in the input stream that contains the sheet in XML format.
* The stream is not closed by this method
* @return the allocated sheet.
* @exception JAXBException raised when unmarshalling goes wrong
*/
public static Sheet unmarshal (InputStream in)
throws JAXBException
{
Unmarshaller um = getJaxbContext().createUnmarshaller();
if (constants.useUnmarshalLogger.isSet()) {
um.setListener(new Jaxb.UnmarshalLogger());
}
Sheet sheet = (Sheet) um.unmarshal(in);
logger.debug("Sheet unmarshalled");
return sheet;
}
public static <T> Object unmarshal(final Class<T> type, final InputStream in, final boolean logErrors) throws ParserConfigurationException, SAXException, JAXBException {
final InputSource inputSource = new InputSource(in);
final SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(false);
final SAXParser parser = factory.newSAXParser();
final JAXBContext ctx = getContext(type);
final Unmarshaller unmarshaller = ctx.createUnmarshaller();
unmarshaller.setEventHandler(new ValidationEventHandler() {
public boolean handleEvent(final ValidationEvent validationEvent) {
if (logErrors) {
System.out.println(validationEvent);
}
return false;
}
});
unmarshaller.setListener(new Unmarshaller.Listener() {
public void afterUnmarshal(final Object object, final Object object1) {
super.afterUnmarshal(object, object1);
}
public void beforeUnmarshal(final Object target, final Object parent) {
super.beforeUnmarshal(target, parent);
}
});
final NamespaceFilter xmlFilter = new NamespaceFilter(parser.getXMLReader());
xmlFilter.setContentHandler(unmarshaller.getUnmarshallerHandler());
final SAXSource source = new SAXSource(xmlFilter, inputSource);
return unmarshaller.unmarshal(source, type);
}