use of java.beans.XMLDecoder in project OpenGrok by OpenGrok.
the class ProjectTest method testEncodeDecode.
/**
* Test that a {@code Project} instance can be encoded and decoded without
* errors. Bug #3077.
*/
@Test
public void testEncodeDecode() {
// Create an exception listener to detect errors while encoding and
// decoding
final LinkedList<Exception> exceptions = new LinkedList<>();
ExceptionListener listener = exceptions::addLast;
ByteArrayOutputStream out = new ByteArrayOutputStream();
XMLEncoder enc = new XMLEncoder(out);
enc.setExceptionListener(listener);
Project p1 = new Project("foo");
enc.writeObject(p1);
enc.close();
// verify that the write didn't 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());
}
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
XMLDecoder dec = new XMLDecoder(in, null, listener);
Project p2 = (Project) dec.readObject();
assertNotNull(p2);
dec.close();
// verify that the read didn't 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());
}
}
use of java.beans.XMLDecoder in project OpenGrok by OpenGrok.
the class Configuration method decodeObject.
@SuppressWarnings("lgtm[java/unsafe-deserialization]")
private static Configuration decodeObject(InputStream in) throws IOException {
final Object ret;
final LinkedList<Exception> exceptions = new LinkedList<>();
ExceptionListener listener = exceptions::addLast;
try (XMLDecoder d = new XMLDecoder(new BufferedInputStream(in), null, listener, new ConfigurationClassLoader())) {
ret = d.readObject();
}
if (!(ret instanceof Configuration)) {
throw new IOException("Not a valid config file");
}
if (!exceptions.isEmpty()) {
// see addGroup()
if (exceptions.getFirst() instanceof IOException) {
throw (IOException) exceptions.getFirst();
}
throw new IOException(exceptions.getFirst());
}
Configuration conf = ((Configuration) ret);
// Removes all non-root groups.
// This ensures that when the configuration is reloaded then the set
// contains only root groups. Subgroups are discovered again
// as follows below
conf.groups.removeIf(g -> g.getParent() != null);
// Traversing subgroups and checking for duplicates,
// effectively transforms the group tree to a structure (Set)
// supporting an iterator.
TreeSet<Group> copy = new TreeSet<>();
LinkedList<Group> stack = new LinkedList<>(conf.groups);
while (!stack.isEmpty()) {
Group group = stack.pollFirst();
stack.addAll(group.getSubgroups());
if (!copy.add(group)) {
throw new IOException(String.format("Duplicate group name '%s' in configuration.", group.getName()));
}
// populate groups where the current group in in their subtree
Group tmp = group.getParent();
while (tmp != null) {
tmp.addDescendant(group);
tmp = tmp.getParent();
}
}
conf.setGroups(copy);
/*
* Validate any defined canonicalRoot entries, and only include where
* validation succeeds.
*/
if (conf.canonicalRoots != null) {
conf.canonicalRoots = conf.canonicalRoots.stream().filter(s -> {
String problem = CanonicalRootValidator.validate(s, "canonicalRoot element");
if (problem == null) {
return true;
} else {
LOGGER.warning(problem);
return false;
}
}).collect(Collectors.toCollection(HashSet::new));
}
return conf;
}
use of java.beans.XMLDecoder in project omegat by omegat-org.
the class SRX method loadSRX.
/**
* Loads segmentation rules from an XML file. If there's an error loading a
* file, it calls <code>initDefaults</code>.
* <p>
* Since 1.6.0 RC8 it also checks if the version of segmentation rules saved
* is older than that of the current OmegaT, and tries to merge the two sets
* of rules.
*/
public static SRX loadSRX(File configFile) {
if (!configFile.exists()) {
return null;
}
SRX res;
try {
MyExceptionListener myel = new MyExceptionListener();
XMLDecoder xmldec = new XMLDecoder(new FileInputStream(configFile), null, myel);
res = (SRX) xmldec.readObject();
xmldec.close();
if (myel.isExceptionOccured()) {
StringBuilder sb = new StringBuilder();
for (Exception ex : myel.getExceptionsList()) {
sb.append(" ");
sb.append(ex);
sb.append("\n");
}
Log.logErrorRB("CORE_SRX_EXC_LOADING_SEG_RULES", sb.toString());
res = new SRX();
res.initDefaults();
return res;
}
// checking the version
if (CURRENT_VERSION.compareTo(res.getVersion()) > 0) {
// yeap, the segmentation config file is of the older version
// initing defaults
SRX defaults = new SRX();
defaults.initDefaults();
// and merging them into loaded rules
res = merge(res, defaults);
}
} catch (Exception e) {
// silently ignoring FNF
if (!(e instanceof FileNotFoundException)) {
Log.log(e);
}
res = new SRX();
res.initDefaults();
}
return res;
}
use of java.beans.XMLDecoder in project OpenGrok by OpenGrok.
the class FileHistoryCache method readCache.
/**
* Read history from a file.
*/
private static History readCache(File file) throws IOException {
try {
FileInputStream in = new FileInputStream(file);
XMLDecoder d = new XMLDecoder(new BufferedInputStream(new GZIPInputStream(in)));
return (History) d.readObject();
} catch (IOException e) {
throw e;
}
}
use of java.beans.XMLDecoder in project jdk8u_jdk by JetBrains.
the class Test4676532 method main.
public static void main(String[] args) throws Exception {
StringBuilder sb = new StringBuilder(256);
sb.append("file:");
sb.append(System.getProperty("test.src", "."));
sb.append(File.separatorChar);
sb.append("test.jar");
URL[] url = { new URL(sb.toString()) };
URLClassLoader cl = new URLClassLoader(url);
Class type = cl.loadClass("test.Test");
if (type == null) {
throw new Error("could not find class test.Test");
}
InputStream stream = new ByteArrayInputStream(DATA.getBytes());
ExceptionListener el = new ExceptionListener() {
public void exceptionThrown(Exception exception) {
throw new Error("unexpected exception", exception);
}
};
XMLDecoder decoder = new XMLDecoder(stream, null, el, cl);
Object object = decoder.readObject();
decoder.close();
if (!type.equals(object.getClass())) {
throw new Error("unexpected " + object.getClass());
}
}
Aggregations