下面列出了怎么用javax.xml.bind.DataBindingException的API类实例代码及写法,或者点击链接到github查看源代码。
public static ModsDefinition unmarshalModsType(Source source) {
try {
Object unmarshaled = defaultUnmarshaller().unmarshal(source);
if (unmarshaled instanceof JAXBElement) {
unmarshaled = ((JAXBElement) unmarshaled).getValue();
}
ModsDefinition mods;
if (unmarshaled instanceof ModsCollectionDefinition) {
ModsCollectionDefinition mc = (ModsCollectionDefinition) unmarshaled;
mods = mc.getMods().get(0);
} else if (unmarshaled instanceof ModsDefinition) {
mods = (ModsDefinition) unmarshaled;
} else {
throw new IllegalStateException(String.valueOf(unmarshaled));
}
return mods;
} catch (JAXBException ex) {
throw new DataBindingException(ex);
}
}
@Override
public void setMetadataAsXml(DescriptionMetadata<String> xmlData, String message, String typeRecord) throws DigitalObjectException {
ModsDefinition mods;
String modelId = handler.getModel().getPid();
if (xmlData.getData() != null) {
ValidationErrorHandler errHandler = new ValidationErrorHandler();
try {
String data = xmlData.getData();
xmlData.setData(data);
Validator validator = ModsUtils.getSchema().newValidator();
validator.setErrorHandler(errHandler);
validator.validate(new StreamSource(new StringReader(xmlData.getData())));
checkValidation(errHandler, xmlData);
mods = ModsUtils.unmarshalModsType(new StreamSource(new StringReader(xmlData.getData())));
} catch (DataBindingException | SAXException | IOException ex) {
checkValidation(errHandler, xmlData);
throw new DigitalObjectValidationException(xmlData.getPid(),
xmlData.getBatchId(), ModsStreamEditor.DATASTREAM_ID, null, ex)
.addValidation("mods", ex.getMessage());
}
} else {
mods = createDefault(modelId);
}
write(modelId, mods, xmlData, message, typeRecord);
}
@Override
public Source build() {
try {
if (marshaller == null) {
createDefaultMarshaller();
}
final Object jaxbObject = getPreparedJaxbObject();
final JAXBSource jaxbSource = new JAXBSource(marshaller, jaxbObject);
// the fake InputSource cannot be used (the Convert.java
// will create a working one if it is null)
jaxbSource.setInputSource(null);
return jaxbSource;
} catch (final JAXBException e) {
throw new DataBindingException(e);
}
}
@Override
public String print(final SampleObject object) {
try(StringWriter writer = new StringWriter()) {
Marshaller marshaller = context.createMarshaller();
marshaller.setSchema(schema);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(object, writer);
// JAXB.marshal(object, writer);
writer.flush();
String text = writer.toString();
return text;
} catch(IOException | JAXBException | DataBindingException e) {
throw new TextPrintException(object, e);
}
}
static <T> T execute(JaxbCallback<T> callback) {
try {
return callback.doExecute();
} catch (JAXBException ex) {
throw new DataBindingException(ex);
}
}
@Test
public void executeThrowsJAXException() {
final JAXBException exception = new JAXBException("GURU Meditation 22");
final Throwable throwable = catchThrowable(() ->
JaxbCallback.execute(() -> {
throw exception;
})
);
assertThat(throwable).isNotNull().isInstanceOf(DataBindingException.class).hasCause(exception);
}
public static void marshal(Result target, Object aesElement, boolean indent) {
try {
Marshaller m = defaultMarshaller(indent);
m.marshal(aesElement, target);
} catch (JAXBException ex) {
throw new DataBindingException(ex);
}
}
public static <T> T unmarshal(Source source, Class<T> type) {
try {
JAXBElement<T> item = defaultUnmarshaller().unmarshal(source, type);
return item.getValue();
} catch (JAXBException ex) {
throw new DataBindingException(ex);
}
}
public static String toString(Import imp) {
StringWriter dump = new StringWriter();
try {
defaultRegistrationContext().createMarshaller().marshal(imp, dump);
} catch (JAXBException ex) {
throw new DataBindingException(ex);
}
return dump.toString();
}
public static void marshal(Result target, Object object, boolean indent) {
try {
Marshaller m = defaultMarshaller(indent);
m.marshal(object, target);
} catch (JAXBException ex) {
throw new DataBindingException(ex);
}
}
public static <T> T unmarshal(Source source, Class<T> type) {
try {
JAXBElement<T> item = defaultUnmarshaller().unmarshal(source, type);
return item.getValue();
} catch (JAXBException ex) {
throw new DataBindingException(ex);
}
}
public static void marshal(Result target, Object mixElement, boolean indent) {
try {
Marshaller m = defaultMarshaller(indent);
m.marshal(mixElement, target);
} catch (JAXBException ex) {
throw new DataBindingException(ex);
}
}
public static <T> T unmarshal(Source source, Class<T> type) {
try {
JAXBElement<T> item = defaultUnmarshaller().unmarshal(source, type);
return item.getValue();
} catch (JAXBException ex) {
throw new DataBindingException(ex);
}
}
public static void marshal(Result target, Object mixElement, boolean indent) {
try {
Marshaller m = defaultMarshaller(indent);
m.marshal(mixElement, target);
} catch (JAXBException ex) {
throw new DataBindingException(ex);
}
}
public static <T> T unmarshal(Source source, Class<T> type) {
try {
JAXBElement<T> item = defaultUnmarshaller().unmarshal(source, type);
return item.getValue();
} catch (JAXBException ex) {
throw new DataBindingException(ex);
}
}
public static void marshal(Result target, Rdf rdf, boolean indent) {
try {
Marshaller m = defaultMarshaller(indent);
m.marshal(rdf, target);
} catch (JAXBException ex) {
throw new DataBindingException(ex);
}
}
public static <T> T unmarshal(Source source, Class<T> type) {
try {
JAXBElement<T> item = defaultUnmarshaller().unmarshal(source, type);
return item.getValue();
} catch (JAXBException ex) {
throw new DataBindingException(ex);
}
}
public static void marshal(Result target, DigitalObject dobj, boolean indent) {
try {
Marshaller m = defaultMarshaller(indent);
m.marshal(dobj, target);
} catch (JAXBException ex) {
throw new DataBindingException(ex);
}
}
public static <T> T unmarshal(Source source, Class<T> type) {
try {
JAXBElement<T> item = defaultUnmarshaller().unmarshal(source, type);
return item.getValue();
} catch (JAXBException ex) {
throw new DataBindingException(ex);
}
}
public static void marshal(Result target, OaiDcType oaidc, boolean indent) {
try {
Marshaller m = defaultMarshaller(indent);
m.marshal(new ObjectFactory().createDc(oaidc), target);
} catch (JAXBException ex) {
throw new DataBindingException(ex);
}
}
public static <T> T unmarshal(Source source, Class<T> type) {
try {
JAXBElement<T> item = defaultUnmarshaller().unmarshal(source, type);
return item.getValue();
} catch (JAXBException ex) {
throw new DataBindingException(ex);
}
}
/**
* @see cz.cas.lib.proarc.mods.package-info.java contains name space prefix mapping.
*/
public static void marshal(Result target, ModsDefinition mods, boolean indent) {
try {
Marshaller m = defaultMarshaller(indent);
m.marshal(new ObjectFactory().createMods(mods), target);
} catch (JAXBException ex) {
throw new DataBindingException(ex);
}
}
public static void marshal(Result target, Object modsElement, boolean indent) {
try {
Marshaller m = defaultMarshaller(indent);
m.marshal(modsElement, target);
} catch (JAXBException ex) {
throw new DataBindingException(ex);
}
}
public static <T> T unmarshal(Source source, Class<T> type) {
try {
JAXBElement<T> item = defaultUnmarshaller().unmarshal(source, type);
return item.getValue();
} catch (JAXBException ex) {
throw new DataBindingException(ex);
}
}
/**
* XMLを読み込み、{@link AnnotationMappingInfo}として取得する。
* @param in
* @return
* @throws XmlOperateException XMLの読み込みに失敗した場合。
* @throws IllegalArgumentException in is null.
*/
public static AnnotationMappingInfo load(final InputStream in) throws XmlOperateException {
ArgUtils.notNull(in, "in");
final AnnotationMappingInfo xmlInfo;
try {
xmlInfo = JAXB.unmarshal(in, AnnotationMappingInfo.class);
} catch (DataBindingException e) {
throw new XmlOperateException("fail load xml with JAXB.", e);
}
return xmlInfo;
}
/**
* XMLを読み込み、{@link AnnotationMappingInfo}として取得する。
* @since 0.5
* @param reader
* @return
* @throws XmlOperateException XMLの読み込みに失敗した場合。
* @throws IllegalArgumentException in is null.
*/
public static AnnotationMappingInfo load(final Reader reader) throws XmlOperateException {
ArgUtils.notNull(reader, "reader");
final AnnotationMappingInfo xmlInfo;
try {
xmlInfo = JAXB.unmarshal(reader, AnnotationMappingInfo.class);
} catch (DataBindingException e) {
throw new XmlOperateException("fail load xml with JAXB.", e);
}
return xmlInfo;
}
/**
* XMLをファイルに保存する。
* @since 1.1
* @param xmlInfo XML情報。
* @param out
* @throws XmlOperateException XMLの書き込みに失敗した場合。
* @throws IllegalArgumentException xmlInfo is null.
* @throws IllegalArgumentException writer is null.
*/
public static void save(final AnnotationMappingInfo xmlInfo, final OutputStream out) throws XmlOperateException {
ArgUtils.notNull(xmlInfo, "xmlInfo");
ArgUtils.notNull(out, "out");
try {
JAXB.marshal(xmlInfo, out);
} catch (DataBindingException e) {
throw new XmlOperateException("fail save xml with JAXB.", e);
}
}
/**
* XMLをファイルに保存する。
* @since 1.1
* @param xmlInfo XML情報。
* @param writer
* @throws XmlOperateException XMLの書き込みに失敗した場合。
* @throws IllegalArgumentException xmlInfo is null.
* @throws IllegalArgumentException writer is null.
*/
public static void save(final AnnotationMappingInfo xmlInfo, final Writer writer) throws XmlOperateException {
ArgUtils.notNull(xmlInfo, "xmlInfo");
ArgUtils.notNull(writer, "writer");
try {
JAXB.marshal(xmlInfo, writer);
} catch (DataBindingException e) {
throw new XmlOperateException("fail save xml with JAXB.", e);
}
}
@Override
public SampleObject parse(final String text) {
try {
Unmarshaller unmashaller = context.createUnmarshaller();
unmashaller.setSchema(schema);
// SampleObject object = JAXB.unmarshal(new StringReader(text), SampleObject.class);
SampleObject object = (SampleObject) unmashaller.unmarshal(new StringReader(text));
return object;
} catch(JAXBException | DataBindingException e) {
throw new TextParseException(text, SampleObject.class);
}
}
/**
* Return a MameSystem object corresponding to the given system name
*/
public Machine findByName(String machineName)
throws IOException, InterruptedException, MachineDoesntExistException {
// Call MameRuntime to get Xml data of the given system,
// then unmarshall it
String[] mameCommandLine = {"-listxml", machineName};
InputStream is;
MameXmlContainer ms = null;
try {
is = this.mame.executeAndReturnStdoutAsInputStream(mameCommandLine);
ms = JAXB.unmarshal(is, MameXmlContainer.class);
} catch (MameExecutionException | DataBindingException e) {
throw (MachineDoesntExistException) new MachineDoesntExistException(
String.format("The machine '%s' doesn't exist or is not "
+ "supported by the provided Mame version", machineName))
.initCause(e);
}
Machine machine = null;
Set<Machine> subMachines = new HashSet<>();
for (Machine m : ms.getMachines()) {
if (m.getName().equals(machineName.toLowerCase())) {
machine = m;
String cloneof = m.getCloneof();
if (cloneof != null) {
m.setClonedMachine(this.findByName(cloneof));
}
String romof = m.getRomof();
if (romof != null) {
m.setRomOfMachine(this.findByName(romof));
}
} else {
// Others machines of the set are considered "subMachines"
subMachines.add(m);
}
}
if (machine == null) {
throw new RuntimeException(String
.format("Unhandled case: Mame returned no errors while searching "
+ "for machine %s but the machine has not been found "
+ "on the XML content", machineName));
}
machine.setSubMachines(subMachines);
return machine;
}