javax.xml.bind.JAXBIntrospector Java Examples

The following examples show how to use javax.xml.bind.JAXBIntrospector. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JAXBContextImpl.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public JAXBIntrospector createJAXBIntrospector() {
    return new JAXBIntrospector() {
        public boolean isElement(Object object) {
            return getElementName(object)!=null;
        }

        public QName getElementName(Object jaxbElement) {
            try {
                return JAXBContextImpl.this.getElementName(jaxbElement);
            } catch (JAXBException e) {
                return null;
            }
        }
    };
}
 
Example #2
Source File: JAXBContextImpl.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public JAXBIntrospector createJAXBIntrospector() {
    return new JAXBIntrospector() {
        public boolean isElement(Object object) {
            return getElementName(object)!=null;
        }

        public QName getElementName(Object jaxbElement) {
            try {
                return JAXBContextImpl.this.getElementName(jaxbElement);
            } catch (JAXBException e) {
                return null;
            }
        }
    };
}
 
Example #3
Source File: JAXBContextImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Override
public JAXBIntrospector createJAXBIntrospector() {
    return new JAXBIntrospector() {
        public boolean isElement(Object object) {
            return getElementName(object)!=null;
        }

        public QName getElementName(Object jaxbElement) {
            try {
                return JAXBContextImpl.this.getElementName(jaxbElement);
            } catch (JAXBException e) {
                return null;
            }
        }
    };
}
 
Example #4
Source File: ConversionService.java    From validator with Apache License 2.0 6 votes vote down vote up
public <T> String writeXml(final T model, final Schema schema, final ValidationEventHandler handler) {
    if (model == null) {
        throw new ConversionExeption("Can not serialize null");
    }
    try ( final StringWriter w = new StringWriter() ) {
        final JAXBIntrospector introspector = getJaxbContext().createJAXBIntrospector();
        final Marshaller marshaller = getJaxbContext().createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        marshaller.setSchema(schema);
        marshaller.setEventHandler(handler);
        final XMLOutputFactory xof = XMLOutputFactory.newFactory();
        final XMLStreamWriter xmlStreamWriter = xof.createXMLStreamWriter(w);
        if (null == introspector.getElementName(model)) {
            final JAXBElement jaxbElement = new JAXBElement(createQName(model), model.getClass(), model);
            marshaller.marshal(jaxbElement, xmlStreamWriter);
        } else {
            marshaller.marshal(model, xmlStreamWriter);
        }
        xmlStreamWriter.flush();
        return w.toString();
    } catch (final JAXBException | IOException | XMLStreamException e) {
        throw new ConversionExeption(String.format("Error serializing Object %s", model.getClass().getName()), e);
    }
}
 
Example #5
Source File: JAXBContextImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Override
public JAXBIntrospector createJAXBIntrospector() {
    return new JAXBIntrospector() {
        public boolean isElement(Object object) {
            return getElementName(object)!=null;
        }

        public QName getElementName(Object jaxbElement) {
            try {
                return JAXBContextImpl.this.getElementName(jaxbElement);
            } catch (JAXBException e) {
                return null;
            }
        }
    };
}
 
Example #6
Source File: JAXBContextImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public JAXBIntrospector createJAXBIntrospector() {
    return new JAXBIntrospector() {
        public boolean isElement(Object object) {
            return getElementName(object)!=null;
        }

        public QName getElementName(Object jaxbElement) {
            try {
                return JAXBContextImpl.this.getElementName(jaxbElement);
            } catch (JAXBException e) {
                return null;
            }
        }
    };
}
 
Example #7
Source File: JAXBContextImpl.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@Override
public JAXBIntrospector createJAXBIntrospector() {
    return new JAXBIntrospector() {
        public boolean isElement(Object object) {
            return getElementName(object)!=null;
        }

        public QName getElementName(Object jaxbElement) {
            try {
                return JAXBContextImpl.this.getElementName(jaxbElement);
            } catch (JAXBException e) {
                return null;
            }
        }
    };
}
 
Example #8
Source File: JAXBContextImpl.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
@Override
public JAXBIntrospector createJAXBIntrospector() {
    return new JAXBIntrospector() {
        public boolean isElement(Object object) {
            return getElementName(object)!=null;
        }

        public QName getElementName(Object jaxbElement) {
            try {
                return JAXBContextImpl.this.getElementName(jaxbElement);
            } catch (JAXBException e) {
                return null;
            }
        }
    };
}
 
Example #9
Source File: JAXBContextImpl.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public JAXBIntrospector createJAXBIntrospector() {
    return new JAXBIntrospector() {
        public boolean isElement(Object object) {
            return getElementName(object)!=null;
        }

        public QName getElementName(Object jaxbElement) {
            try {
                return JAXBContextImpl.this.getElementName(jaxbElement);
            } catch (JAXBException e) {
                return null;
            }
        }
    };
}
 
Example #10
Source File: QNameBuilder.java    From wadl-tools with Apache License 2.0 6 votes vote down vote up
private QName discoverQNameFromJaxb(Class<?> classType) {
    QName qName = null;
    try {
        final JAXBIntrospector jaxbIntrospector = JAXBContext.newInstance(classType).createJAXBIntrospector();
        qName = jaxbIntrospector.getElementName(classType.getConstructor().newInstance());

    } catch (Exception e) {
        // Add e to the logger message because JAXB Exceptions has a lot of information in the toString().
        // and some loggers implementations just print the getMessage();
        logger.warn("Cannot discover QName from JAXB annotations for class: " + classType.getName()
                + ". Preparing generic QName." + e, e);
    }

    if (qName == null) {
        // Could be null if getElementName returned null, or a exception was thrown.
        return EMPTY_Q_NAME;
    }
    return qName;
}