use of javax.xml.bind.JAXBException in project camel by apache.
the class DefaultBulkApiClient method unmarshalResponse.
private <T> T unmarshalResponse(InputStream response, Request request, Class<T> resultClass) throws SalesforceException {
try {
Unmarshaller unmarshaller = context.createUnmarshaller();
JAXBElement<T> result = unmarshaller.unmarshal(new StreamSource(response), resultClass);
return result.getValue();
} catch (JAXBException e) {
throw new SalesforceException(String.format("Error unmarshaling response {%s:%s} : %s", request.getMethod(), request.getURI(), e.getMessage()), e);
} catch (IllegalArgumentException e) {
throw new SalesforceException(String.format("Error unmarshaling response for {%s:%s} : %s", request.getMethod(), request.getURI(), e.getMessage()), e);
}
}
use of javax.xml.bind.JAXBException in project camel by apache.
the class RecordsUtil method createXMLFile.
public static void createXMLFile() {
File in = new File("target/in/records.xml");
if (in.exists()) {
return;
} else {
if (!in.getParentFile().exists() && !in.getParentFile().mkdirs()) {
throw new RuntimeException("can't create " + in.getParent());
}
}
Records records = new Records();
for (int i = 0; i < 10; i++) {
Record record = new Record();
record.setKey(Integer.toString(i));
record.setValue("#" + i);
records.getRecord().add(record);
}
Marshaller marshaller;
try {
JAXBContext jaxbCtx = JAXBContext.newInstance(Records.class.getPackage().getName());
marshaller = jaxbCtx.createMarshaller();
} catch (JAXBException e) {
throw new RuntimeException(e);
}
FileWriter writer = null;
try {
writer = new FileWriter(in);
marshaller.marshal(records, writer);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (JAXBException e) {
throw new RuntimeException(e);
} finally {
if (writer != null) {
try {
writer.flush();
writer.close();
} catch (IOException e) {
// no-op
}
}
}
}
use of javax.xml.bind.JAXBException in project springside4 by springside.
the class XmlMapper method createMarshaller.
/**
* 创建Marshaller并设定encoding(可为null).
* 线程不安全,需要每次创建或pooling。
*/
public static Marshaller createMarshaller(Class clazz, String encoding) {
try {
JAXBContext jaxbContext = getJaxbContext(clazz);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
if (StringUtils.isNotBlank(encoding)) {
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
}
return marshaller;
} catch (JAXBException e) {
throw ExceptionUtil.unchecked(e);
}
}
use of javax.xml.bind.JAXBException in project springside4 by springside.
the class XmlMapper method toXml.
/**
* Java Collection->Xml with encoding, 特别支持Root Element是Collection的情形.
*/
public static String toXml(Collection<?> root, String rootName, Class clazz, String encoding) {
try {
CollectionWrapper wrapper = new CollectionWrapper();
wrapper.collection = root;
JAXBElement<CollectionWrapper> wrapperElement = new JAXBElement<CollectionWrapper>(new QName(rootName), CollectionWrapper.class, wrapper);
StringWriter writer = new StringWriter();
createMarshaller(clazz, encoding).marshal(wrapperElement, writer);
return writer.toString();
} catch (JAXBException e) {
throw ExceptionUtil.unchecked(e);
}
}
use of javax.xml.bind.JAXBException in project springside4 by springside.
the class XmlMapper method getJaxbContext.
protected static JAXBContext getJaxbContext(Class clazz) {
Validate.notNull(clazz, "'clazz' must not be null");
JAXBContext jaxbContext = jaxbContexts.get(clazz);
if (jaxbContext == null) {
try {
jaxbContext = JAXBContext.newInstance(clazz, CollectionWrapper.class);
jaxbContexts.putIfAbsent(clazz, jaxbContext);
} catch (JAXBException ex) {
throw new RuntimeException("Could not instantiate JAXBContext for class [" + clazz + "]: " + ex.getMessage(), ex);
}
}
return jaxbContext;
}
Aggregations