类javax.xml.bind.MarshalException源码实例Demo

下面列出了怎么用javax.xml.bind.MarshalException的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: spring-analysis-note   文件: Jaxb2Marshaller.java
/**
 * 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);
	}
}
 
源代码2 项目: java-technology-stack   文件: Jaxb2Marshaller.java
/**
 * 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);
	}
}
 
源代码3 项目: tessera   文件: JaxbUtilTest.java
@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);
}
 
源代码4 项目: spring4-understanding   文件: Jaxb2Marshaller.java
/**
 * 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);
	}
}
 
源代码5 项目: TranskribusCore   文件: PageXmlUtils.java
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;
}
 
源代码6 项目: TranskribusCore   文件: JaxbUtils.java
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;
}
 
源代码7 项目: nomulus   文件: XmlFragmentMarshaller.java
/**
 * 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;
}
 
源代码10 项目: TencentKona-8   文件: BridgeAdapter.java
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);
    }
}
 
源代码11 项目: TencentKona-8   文件: BridgeAdapter.java
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.
    }
}
 
源代码12 项目: jdk8u60   文件: BridgeAdapter.java
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);
    }
}
 
源代码13 项目: jdk8u60   文件: BridgeAdapter.java
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.
    }
}
 
源代码14 项目: tessera   文件: JaxbUtilTest.java
@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);
}
 
源代码15 项目: tessera   文件: JaxbUtilTest.java
@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);
}
 
源代码16 项目: yare   文件: RuleToXmlConverterTest.java
@Test
void shouldNotMarshallRuleInconsistentWithSchema() {
    // given
    Rule ruleObject = TestRuleFactory.constructInvalidRule();
    // when
    assertThatThrownBy(() -> converter.marshal(ruleObject))
            // then
            .isInstanceOf(RuleConversionException.class).hasCauseExactlyInstanceOf(MarshalException.class);
}
 
源代码17 项目: openjdk-jdk8u   文件: BridgeAdapter.java
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);
    }
}
 
源代码18 项目: openjdk-jdk8u   文件: BridgeAdapter.java
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.
    }
}
 
源代码19 项目: openjdk-jdk8u-backup   文件: BridgeAdapter.java
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);
    }
}
 
源代码20 项目: openjdk-jdk8u-backup   文件: BridgeAdapter.java
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.
    }
}
 
源代码21 项目: openjdk-jdk9   文件: BridgeAdapter.java
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);
    }
}
 
源代码22 项目: openjdk-jdk9   文件: BridgeAdapter.java
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.
    }
}
 
源代码23 项目: hottub   文件: BridgeAdapter.java
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);
    }
}
 
源代码24 项目: hottub   文件: BridgeAdapter.java
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.
    }
}
 
源代码25 项目: openjdk-8-source   文件: BridgeAdapter.java
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);
    }
}
 
源代码26 项目: openjdk-8-source   文件: BridgeAdapter.java
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.
    }
}
 
源代码27 项目: openjdk-8   文件: BridgeAdapter.java
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);
    }
}
 
源代码28 项目: openjdk-8   文件: BridgeAdapter.java
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.
    }
}
 
源代码29 项目: nomulus   文件: RdeMarshaller.java
/**
 * 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);
  }
}
 
源代码30 项目: nomulus   文件: RdeMarshaller.java
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);
}
 
 类所在包