use of java.beans.XMLDecoder in project jaffa-framework by jaffa-projects.
the class JAXBHelper method unmarshalPayload.
/**
* Obtains the contents of the input message, and unmarshals it into the original POJO.
* JAXB is used for unmarshalling, if the dataBeanClass carries the 'XmlRootElement' JAXB annotation.
* Else the XMLDecoder will be used for unmarshalling.
* @param xml the XML to be unmarshalled.
* @param dataBeanClassName the className of the source dataBean that was used to generate the Message.
* @return the contents of the input message, unmarshalled into the original POJO.
* @throws ClassNotFoundException if the dataBean class is not found.
* @throws JAXBException if any error occurs during the unmarshal process.
*/
public static Object unmarshalPayload(String xml, String dataBeanClassName) throws JAXBException, ClassNotFoundException {
if (log.isDebugEnabled()) {
log.debug("Unmarshalling into an instance of " + dataBeanClassName + " the XML\n" + xml);
}
Object payload = null;
Class dataBeanClass = Class.forName(dataBeanClassName);
if (dataBeanClass.isAnnotationPresent(XmlRootElement.class)) {
if (log.isDebugEnabled()) {
log.debug(dataBeanClassName + " has the 'XmlRootElement' JAXB annotation, and hence will be unmarshalled using JAXB");
}
JAXBContext jc = obtainJAXBContext(dataBeanClass);
Unmarshaller unmarshaller = jc.createUnmarshaller();
payload = unmarshaller.unmarshal(new BufferedReader(new StringReader(xml)));
} else if (dataBeanClass.isAnnotationPresent(XmlType.class)) {
if (log.isDebugEnabled())
log.debug(dataBeanClassName + " has the 'XmlType' JAXB annotation, and hence will be unmarshalled using JAXB");
JAXBContext jc = JAXBHelper.obtainJAXBContext(dataBeanClass);
Unmarshaller unmarshaller = jc.createUnmarshaller();
JAXBElement rootElement = unmarshaller.unmarshal(new StreamSource(new BufferedReader(new StringReader(xml))), dataBeanClass);
payload = rootElement.getValue();
} else {
if (log.isDebugEnabled()) {
log.debug(dataBeanClassName + " does not have the 'XmlRootElement' JAXB annotation, and hence will be unmarshalled using XMLDecoder");
}
XMLDecoder d = new XMLDecoder(new BufferedInputStream(new ByteArrayInputStream(xml.getBytes())));
payload = d.readObject();
d.close();
}
if (log.isDebugEnabled()) {
log.debug("Unmarshalled Payload: " + payload);
}
return payload;
}
use of java.beans.XMLDecoder in project hutool by looly.
the class XmlUtil method readObjectFromXml.
/**
* 从XML中读取对象 Reads serialized object from the XML file.
*
* @param <T> 对象类型
* @param source {@link InputSource}
* @return 对象
* @since 3.2.0
*/
@SuppressWarnings("unchecked")
public static <T> T readObjectFromXml(InputSource source) {
Object result;
XMLDecoder xmldec = null;
try {
xmldec = new XMLDecoder(source);
result = xmldec.readObject();
} finally {
IoUtil.close(xmldec);
}
return (T) result;
}
use of java.beans.XMLDecoder in project omegat by omegat-org.
the class Convert20to21 method convertFiltersConfig.
/**
* Convert filters config ('filters.conf') into new format.
*
* @param fromFile
* old config file
* @param toFile
* new config file
* @throws Exception
*/
public static void convertFiltersConfig(final File fromFile, final File toFile) throws Exception {
if (!fromFile.exists()) {
return;
}
String c = read(fromFile);
org.omegat.convert.v20to21.data.Filters filters;
XMLDecoder xmldec = new XMLDecoder(new ByteArrayInputStream(c.getBytes("UTF-8")));
try {
filters = (org.omegat.convert.v20to21.data.Filters) xmldec.readObject();
} finally {
xmldec.close();
}
Filters res = new Filters();
for (org.omegat.convert.v20to21.data.OneFilter f : filters.getFilter()) {
Filter fo = new Filter();
res.getFilters().add(fo);
fo.setClassName(f.getClassName());
fo.setEnabled(f.isOn());
for (org.omegat.convert.v20to21.data.Instance i : f.getInstance()) {
Files io = new Files();
fo.getFiles().add(io);
io.setSourceFilenameMask(i.getSourceFilenameMask());
io.setTargetFilenamePattern(i.getTargetFilenamePattern());
io.setSourceEncoding(i.getSourceEncoding());
io.setTargetEncoding(i.getTargetEncoding());
}
Serializable opts = f.getOptions();
if (opts != null) {
BeanInfo bi = Introspector.getBeanInfo(opts.getClass());
for (PropertyDescriptor prop : bi.getPropertyDescriptors()) {
if ("class".equals(prop.getName())) {
continue;
}
Object value = prop.getReadMethod().invoke(opts);
Filter.Option op = new Filter.Option();
op.setName(prop.getName());
op.setValue(value.toString());
fo.getOption().add(op);
}
}
}
convertTextFilter(res);
convertHTMLFilter2(res);
JAXBContext ctx = JAXBContext.newInstance(Filters.class);
Marshaller m = ctx.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(res, toFile);
}
use of java.beans.XMLDecoder in project antlrworks by antlr.
the class XJDataXML method readData.
@Override
@SuppressWarnings("unchecked")
public void readData() throws IOException {
XMLDecoder d = new XMLDecoder(new BufferedInputStream(new FileInputStream(getFile())));
dictionary = (Map<String, Object>) d.readObject();
customReadData(d);
d.close();
}
use of java.beans.XMLDecoder in project OpenGrok by OpenGrok.
the class IgnoredNamesTest method testEncodeDecode.
/**
* Make sure that encoding and decoding IgnoredNames object is 1:1 operation.
*/
@Test
void testEncodeDecode() throws IOException {
IgnoredNames in = new IgnoredNames();
// Add file and directory to list of ignored items.
in.add("f:foo.txt");
in.add("d:bar");
// Create an exception listener to detect errors while encoding and decoding
final LinkedList<Exception> exceptions = new LinkedList<>();
ExceptionListener listener = exceptions::addLast;
// Actually create the file and directory for much better test coverage.
File tmpdir = Files.createTempDirectory("ignoredNames").toFile();
File foo = new File(tmpdir, "foo.txt");
foo.createNewFile();
assertTrue(foo.isFile());
File bar = new File(tmpdir, "bar");
bar.mkdir();
assertTrue(bar.isDirectory());
// Store the IgnoredNames object as XML file.
File testXML = new File(tmpdir, "Test.xml");
XMLEncoder e = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(testXML)));
e.setExceptionListener(listener);
e.writeObject(in);
e.close();
// Restore the IgnoredNames object from XML file.
XMLDecoder d = new XMLDecoder(new FileInputStream(testXML));
IgnoredNames in2 = (IgnoredNames) d.readObject();
d.close();
// Verify that the XML encoding/decoding did not fail.
if (!exceptions.isEmpty()) {
// Can only chain one of the exceptions. Take the first one.
throw new AssertionError("Got " + exceptions.size() + " exception(s)", exceptions.getFirst());
}
// Make sure the complete list of items is equal after decoding.
// This will is a simple casual test that cannot verify that sub-classes
// are intact. For that there are the following tests.
assertTrue(in.getItems().containsAll(in2.getItems()));
// Use the restored object to test the matching of file and directory.
assertTrue(in2.ignore("foo.txt"));
assertTrue(in2.ignore("bar"));
assertTrue(in2.ignore(foo));
assertTrue(in2.ignore(bar));
// Cleanup.
IOUtils.removeRecursive(tmpdir.toPath());
}
Aggregations