Search in sources :

Example 31 with XMLDecoder

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());
    }
}
Also used : XMLEncoder(java.beans.XMLEncoder) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLDecoder(java.beans.XMLDecoder) ExceptionListener(java.beans.ExceptionListener) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LinkedList(java.util.LinkedList) Test(org.junit.jupiter.api.Test)

Example 32 with XMLDecoder

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;
}
Also used : IOException(java.io.IOException) PatternSyntaxException(java.util.regex.PatternSyntaxException) IOException(java.io.IOException) LinkedList(java.util.LinkedList) BufferedInputStream(java.io.BufferedInputStream) TreeSet(java.util.TreeSet) XMLDecoder(java.beans.XMLDecoder) ExceptionListener(java.beans.ExceptionListener) HashSet(java.util.HashSet)

Example 33 with XMLDecoder

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;
}
Also used : XMLDecoder(java.beans.XMLDecoder) FileNotFoundException(java.io.FileNotFoundException) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) FileNotFoundException(java.io.FileNotFoundException)

Example 34 with XMLDecoder

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;
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) XMLDecoder(java.beans.XMLDecoder) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 35 with XMLDecoder

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());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) URLClassLoader(java.net.URLClassLoader) XMLDecoder(java.beans.XMLDecoder) ExceptionListener(java.beans.ExceptionListener) URL(java.net.URL)

Aggregations

XMLDecoder (java.beans.XMLDecoder)54 ByteArrayInputStream (java.io.ByteArrayInputStream)28 IOException (java.io.IOException)19 BufferedInputStream (java.io.BufferedInputStream)18 XMLEncoder (java.beans.XMLEncoder)15 FileInputStream (java.io.FileInputStream)14 LinkedList (java.util.LinkedList)13 ExceptionListener (java.beans.ExceptionListener)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 File (java.io.File)8 FileOutputStream (java.io.FileOutputStream)5 InputStream (java.io.InputStream)5 AssertionFailedError (junit.framework.AssertionFailedError)5 Test (org.junit.Test)5 Test (org.junit.jupiter.api.Test)5 FileNotFoundException (java.io.FileNotFoundException)4 List (java.util.List)4 PatternSyntaxException (java.util.regex.PatternSyntaxException)4 BufferedOutputStream (java.io.BufferedOutputStream)3 Serializable (java.io.Serializable)2