下面列出了怎么用javax.xml.bind.MarshalException的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Convert the given {@code JAXBException} to an appropriate exception
* from the {@code org.springframework.oxm} hierarchy.
* @param ex {@code JAXBException} that occurred
* @return the corresponding {@code XmlMappingException}
*/
protected XmlMappingException convertJaxbException(JAXBException ex) {
if (ex instanceof ValidationException) {
return new ValidationFailureException("JAXB validation exception", ex);
}
else if (ex instanceof MarshalException) {
return new MarshallingFailureException("JAXB marshalling exception", ex);
}
else if (ex instanceof UnmarshalException) {
return new UnmarshallingFailureException("JAXB unmarshalling exception", ex);
}
else {
// fallback
return new UncategorizedMappingException("Unknown JAXB exception", ex);
}
}
/**
* Convert the given {@code JAXBException} to an appropriate exception from the
* {@code org.springframework.oxm} hierarchy.
* @param ex {@code JAXBException} that occurred
* @return the corresponding {@code XmlMappingException}
*/
protected XmlMappingException convertJaxbException(JAXBException ex) {
if (ex instanceof ValidationException) {
return new ValidationFailureException("JAXB validation exception", ex);
}
else if (ex instanceof MarshalException) {
return new MarshallingFailureException("JAXB marshalling exception", ex);
}
else if (ex instanceof UnmarshalException) {
return new UnmarshallingFailureException("JAXB unmarshalling exception", ex);
}
else {
// fallback
return new UncategorizedMappingException("Unknown JAXB exception", ex);
}
}
@Test
public void marshallingProducesNonJaxbException() {
final KeyDataConfig input =
new KeyDataConfig(new PrivateKeyData("VAL", null, null, null, null), PrivateKeyType.UNLOCKED);
IOException exception = new IOException("What you talking about willis?");
OutputStream out =
mock(
OutputStream.class,
(iom) -> {
throw exception;
});
final Throwable throwable = catchThrowable(() -> JaxbUtil.marshal(input, out));
assertThat(throwable)
.isInstanceOf(ConfigException.class)
.hasCauseExactlyInstanceOf(javax.xml.bind.MarshalException.class);
}
/**
* Convert the given {@code JAXBException} to an appropriate exception from the
* {@code org.springframework.oxm} hierarchy.
* @param ex {@code JAXBException} that occured
* @return the corresponding {@code XmlMappingException}
*/
protected XmlMappingException convertJaxbException(JAXBException ex) {
if (ex instanceof ValidationException) {
return new ValidationFailureException("JAXB validation exception", ex);
}
else if (ex instanceof MarshalException) {
return new MarshallingFailureException("JAXB marshalling exception", ex);
}
else if (ex instanceof UnmarshalException) {
return new UnmarshallingFailureException("JAXB unmarshalling exception", ex);
}
else {
// fallback
return new UncategorizedMappingException("Unknown JAXB exception", ex);
}
}
public static byte[] marshalToBytes(PcGtsType page) throws JAXBException {
ValidationEventCollector vec = new ValidationEventCollector();
Marshaller marshaller = createMarshaller(vec);
ObjectFactory objectFactory = new ObjectFactory();
JAXBElement<PcGtsType> je = objectFactory.createPcGts(page);
byte[] data;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
try {
marshaller.marshal(je, out);
data = out.toByteArray();
} finally {
out.close();
}
} catch (Exception e) {
throw new MarshalException(e);
}
String msg=buildMsg(vec, page);
if (!msg.startsWith(NO_EVENTS_MSG))
logger.info(msg);
return data;
}
public static <T> byte[] marshalToBytes(T object, Class<?>... nestedClasses) throws JAXBException {
byte[] data;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try{
try{
marshalToStream(object, out, nestedClasses);
data = out.toByteArray();
} finally {
out.close();
}
} catch (Exception e){
throw new MarshalException(e);
}
return data;
}
/**
* Turns an individual JAXB element into an XML fragment string using the given validation mode.
*
* @throws MarshalException if schema validation failed
*/
public String marshal(JAXBElement<?> element, ValidationMode validationMode)
throws MarshalException {
os.reset();
marshaller.setSchema((validationMode == STRICT) ? schema : null);
try {
marshaller.marshal(element, os);
} catch (JAXBException e) {
throwIfInstanceOf(e, MarshalException.class);
throw new RuntimeException("Mysterious XML exception", e);
}
String fragment = new String(os.toByteArray(), UTF_8);
int endOfFirstLine = fragment.indexOf(">\n");
verify(endOfFirstLine > 0, "Bad XML fragment:\n%s", fragment);
String firstLine = fragment.substring(0, endOfFirstLine + 2);
String rest = fragment.substring(firstLine.length());
return XMLNS_PATTERN.matcher(firstLine).replaceAll("") + rest;
}
/**
* @see javax.xml.bind.annotation.adapters.XmlAdapter#marshal(java.lang.Object)
*/
@Override
public NameAndNamespacePair marshal(NameAndNamespacePair v) throws Exception {
if (v != null) {
if (StringUtils.isBlank(v.getName())) {
throw new MarshalException("Cannot export a name-and-namespace pair with a blank name");
} else if (StringUtils.isBlank(v.getNamespaceCode())) {
throw new MarshalException("Cannot export a name-and-namespace pair with a blank namespace code");
} else if (CoreServiceApiServiceLocator.getNamespaceService().getNamespace(v.getNamespaceCode()) == null) {
throw new MarshalException("Cannot export a name-and-namespace pair with invalid or unknown namespace \"" + v.getNamespaceCode() + "\"");
}
v.setName(new NormalizedStringAdapter().marshal(v.getName()));
v.setNamespaceCode(v.getNamespaceCode());
}
return v;
}
/**
* @see javax.xml.bind.annotation.adapters.XmlAdapter#marshal(java.lang.Object)
*/
@Override
public NameAndNamespacePair marshal(NameAndNamespacePair v) throws Exception {
if (v != null) {
if (StringUtils.isBlank(v.getName())) {
throw new MarshalException("Cannot export a name-and-namespace pair with a blank name");
} else if (StringUtils.isBlank(v.getNamespaceCode())) {
throw new MarshalException("Cannot export a name-and-namespace pair with a blank namespace code");
} else if (CoreServiceApiServiceLocator.getNamespaceService().getNamespace(v.getNamespaceCode()) == null) {
throw new MarshalException("Cannot export a name-and-namespace pair with invalid or unknown namespace \"" + v.getNamespaceCode() + "\"");
}
v.setName(new NormalizedStringAdapter().marshal(v.getName()));
v.setNamespaceCode(v.getNamespaceCode());
}
return v;
}
private OnWire _adaptM(XMLSerializer serializer, InMemory v) throws MarshalException {
XmlAdapter<OnWire,InMemory> a = serializer.getAdapter(adapter);
try {
return a.marshal(v);
} catch (Exception e) {
serializer.handleError(e,v,null);
throw new MarshalException(e);
}
}
void marshal(InMemory o, XMLSerializer out) throws IOException, SAXException, XMLStreamException {
try {
core.marshal(_adaptM( XMLSerializer.getInstance(), o ), out );
} catch (MarshalException e) {
// recover from error by not marshalling this element.
}
}
private OnWire _adaptM(XMLSerializer serializer, InMemory v) throws MarshalException {
XmlAdapter<OnWire,InMemory> a = serializer.getAdapter(adapter);
try {
return a.marshal(v);
} catch (Exception e) {
serializer.handleError(e,v,null);
throw new MarshalException(e);
}
}
void marshal(InMemory o, XMLSerializer out) throws IOException, SAXException, XMLStreamException {
try {
core.marshal(_adaptM( XMLSerializer.getInstance(), o ), out );
} catch (MarshalException e) {
// recover from error by not marshalling this element.
}
}
@Test
public void marshallingProducesError() {
final Exception ex = new Exception();
OutputStream out = mock(OutputStream.class);
final Throwable throwable = catchThrowable(() -> JaxbUtil.marshal(ex, out));
assertThat(throwable).isInstanceOf(ConfigException.class).hasCauseExactlyInstanceOf(MarshalException.class);
}
@Test
public void marshallingNOValidationProducesError() {
final Exception ex = new Exception();
OutputStream out = mock(OutputStream.class);
final Throwable throwable = catchThrowable(() -> JaxbUtil.marshalWithNoValidation(ex, out));
assertThat(throwable).isInstanceOf(ConfigException.class).hasCauseExactlyInstanceOf(MarshalException.class);
}
@Test
void shouldNotMarshallRuleInconsistentWithSchema() {
// given
Rule ruleObject = TestRuleFactory.constructInvalidRule();
// when
assertThatThrownBy(() -> converter.marshal(ruleObject))
// then
.isInstanceOf(RuleConversionException.class).hasCauseExactlyInstanceOf(MarshalException.class);
}
private OnWire _adaptM(XMLSerializer serializer, InMemory v) throws MarshalException {
XmlAdapter<OnWire,InMemory> a = serializer.getAdapter(adapter);
try {
return a.marshal(v);
} catch (Exception e) {
serializer.handleError(e,v,null);
throw new MarshalException(e);
}
}
void marshal(InMemory o, XMLSerializer out) throws IOException, SAXException, XMLStreamException {
try {
core.marshal(_adaptM( XMLSerializer.getInstance(), o ), out );
} catch (MarshalException e) {
// recover from error by not marshalling this element.
}
}
private OnWire _adaptM(XMLSerializer serializer, InMemory v) throws MarshalException {
XmlAdapter<OnWire,InMemory> a = serializer.getAdapter(adapter);
try {
return a.marshal(v);
} catch (Exception e) {
serializer.handleError(e,v,null);
throw new MarshalException(e);
}
}
void marshal(InMemory o, XMLSerializer out) throws IOException, SAXException, XMLStreamException {
try {
core.marshal(_adaptM( XMLSerializer.getInstance(), o ), out );
} catch (MarshalException e) {
// recover from error by not marshalling this element.
}
}
private OnWire _adaptM(XMLSerializer serializer, InMemory v) throws MarshalException {
XmlAdapter<OnWire,InMemory> a = serializer.getAdapter(adapter);
try {
return a.marshal(v);
} catch (Exception e) {
serializer.handleError(e,v,null);
throw new MarshalException(e);
}
}
void marshal(InMemory o, XMLSerializer out) throws IOException, SAXException, XMLStreamException {
try {
core.marshal(_adaptM( XMLSerializer.getInstance(), o ), out );
} catch (MarshalException e) {
// recover from error by not marshalling this element.
}
}
private OnWire _adaptM(XMLSerializer serializer, InMemory v) throws MarshalException {
XmlAdapter<OnWire,InMemory> a = serializer.getAdapter(adapter);
try {
return a.marshal(v);
} catch (Exception e) {
serializer.handleError(e,v,null);
throw new MarshalException(e);
}
}
void marshal(InMemory o, XMLSerializer out) throws IOException, SAXException, XMLStreamException {
try {
core.marshal(_adaptM( XMLSerializer.getInstance(), o ), out );
} catch (MarshalException e) {
// recover from error by not marshalling this element.
}
}
private OnWire _adaptM(XMLSerializer serializer, InMemory v) throws MarshalException {
XmlAdapter<OnWire,InMemory> a = serializer.getAdapter(adapter);
try {
return a.marshal(v);
} catch (Exception e) {
serializer.handleError(e,v,null);
throw new MarshalException(e);
}
}
void marshal(InMemory o, XMLSerializer out) throws IOException, SAXException, XMLStreamException {
try {
core.marshal(_adaptM( XMLSerializer.getInstance(), o ), out );
} catch (MarshalException e) {
// recover from error by not marshalling this element.
}
}
private OnWire _adaptM(XMLSerializer serializer, InMemory v) throws MarshalException {
XmlAdapter<OnWire,InMemory> a = serializer.getAdapter(adapter);
try {
return a.marshal(v);
} catch (Exception e) {
serializer.handleError(e,v,null);
throw new MarshalException(e);
}
}
void marshal(InMemory o, XMLSerializer out) throws IOException, SAXException, XMLStreamException {
try {
core.marshal(_adaptM( XMLSerializer.getInstance(), o ), out );
} catch (MarshalException e) {
// recover from error by not marshalling this element.
}
}
/**
* Turns XJC element into XML fragment, converting {@link MarshalException}s to {@link
* RuntimeException}s.
*/
public String marshalOrDie(JAXBElement<?> element) {
try {
return marshal(element);
} catch (MarshalException e) {
throw new RuntimeException(e);
}
}
private DepositFragment marshalResource(
RdeResourceType type, ImmutableObject resource, JAXBElement<?> element) {
String xml = "";
String error = "";
try {
xml = marshal(element);
} catch (MarshalException e) {
error = String.format("RDE XML schema validation failed: %s\n%s%s\n",
Key.create(resource),
e.getLinkedException(),
getMarshaller().marshalLenient(element));
logger.atSevere().withCause(e).log(error);
}
return DepositFragment.create(type, xml, error);
}