test/javatestfile.java

changeset 25
f82aa7afe872
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/javatestfile.java	Tue Apr 21 09:47:52 2015 +0200
     1.3 @@ -0,0 +1,176 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + *
     1.7 + * Copyright 2014 Mike Becker. All rights reserved.
     1.8 + *
     1.9 + * Redistribution and use in source and binary forms, with or without
    1.10 + * modification, are permitted provided that the following conditions are met:
    1.11 + *
    1.12 + *   1. Redistributions of source code must retain the above copyright
    1.13 + *      notice, this list of conditions and the following disclaimer.
    1.14 + *
    1.15 + *   2. Redistributions in binary form must reproduce the above copyright
    1.16 + *      notice, this list of conditions and the following disclaimer in the
    1.17 + *      documentation and/or other materials provided with the distribution.
    1.18 + *
    1.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    1.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.29 + * POSSIBILITY OF SUCH DAMAGE.
    1.30 + *
    1.31 + */
    1.32 +
    1.33 +package de.uapcore.sigred.doc.base;
    1.34 +
    1.35 +import de.uapcore.sigred.doc.Resources;
    1.36 +import de.uapcore.sigrapi.impl.Digraph;
    1.37 +import de.uapcore.sigrapi.impl.Graph;
    1.38 +import de.uapcore.sigrapi.IGraph;
    1.39 +import java.io.IOException;
    1.40 +import java.io.InputStream;
    1.41 +import java.io.OutputStream;
    1.42 +import java.util.concurrent.atomic.AtomicBoolean;
    1.43 +import java.util.concurrent.atomic.AtomicReference;
    1.44 +import org.apache.xerces.impl.Constants;
    1.45 +import org.dom4j.Document;
    1.46 +import org.dom4j.DocumentException;
    1.47 +import org.dom4j.DocumentHelper;
    1.48 +import org.dom4j.Element;
    1.49 +import org.dom4j.Namespace;
    1.50 +import org.dom4j.QName;
    1.51 +import org.dom4j.io.OutputFormat;
    1.52 +import org.dom4j.io.SAXReader;
    1.53 +import org.dom4j.io.XMLWriter;
    1.54 +import org.xml.sax.ErrorHandler;
    1.55 +import org.xml.sax.SAXException;
    1.56 +import org.xml.sax.SAXParseException;
    1.57 +
    1.58 +public abstract class AbstractGraphDocument<T extends IGraph>
    1.59 +        extends FileBackedDocument {
    1.60 +    
    1.61 +    protected static final Namespace NAMESPACE = Namespace.get("sigred",
    1.62 +        "http://develop.uap-core.de/sigred/");
    1.63 +    
    1.64 +    private static final
    1.65 +        QName TAG_GRAPHDOC = QName.get("graph-document", NAMESPACE);
    1.66 +    private static final
    1.67 +        QName TAG_GRAPH = QName.get("graph", NAMESPACE);
    1.68 +    private static final
    1.69 +        QName TAG_DIGRAPH = QName.get("digraph", NAMESPACE);
    1.70 +    private static final
    1.71 +        QName TAG_METADATA = QName.get("metadata", NAMESPACE);
    1.72 +    
    1.73 +    protected final T graph;
    1.74 +    
    1.75 +    private final GraphDocumentMetadata metadata;
    1.76 +    
    1.77 +    public AbstractGraphDocument(Class<T> graphType) {
    1.78 +        T g;
    1.79 +        try {
    1.80 +            g = graphType.newInstance();
    1.81 +        } catch (ReflectiveOperationException e) {
    1.82 +            assert false;
    1.83 +            g = null; // for the compiler
    1.84 +        }
    1.85 +        graph = g;
    1.86 +        metadata = new GraphDocumentMetadata();
    1.87 +    }
    1.88 +
    1.89 +    public T getGraph() {
    1.90 +        return graph;
    1.91 +    }
    1.92 +    
    1.93 +    public GraphDocumentMetadata getMetadata() {
    1.94 +        return metadata;
    1.95 +    }
    1.96 +
    1.97 +    protected abstract void writeGraph(Element rootNode) throws IOException;
    1.98 +    protected abstract void readGraph(Element rootNode) throws IOException;
    1.99 +
   1.100 +    @Override
   1.101 +    public void writeTo(OutputStream out) throws IOException {
   1.102 +        Document doc = DocumentHelper.createDocument();
   1.103 +
   1.104 +        Element rootNode = doc.addElement(TAG_GRAPHDOC);
   1.105 +
   1.106 +        Element metadataNode = rootNode.addElement(TAG_METADATA);
   1.107 +
   1.108 +        metadata.write(metadataNode);
   1.109 +
   1.110 +        if (graph instanceof Graph) {
   1.111 +            writeGraph(rootNode.addElement(TAG_GRAPH));
   1.112 +        } else if (graph instanceof Digraph) {
   1.113 +            writeGraph(rootNode.addElement(TAG_DIGRAPH));
   1.114 +        } else {
   1.115 +            throw new IOException("unsupported graph type");
   1.116 +        }
   1.117 +
   1.118 +        XMLWriter writer = new XMLWriter(out, OutputFormat.createPrettyPrint());
   1.119 +        writer.write(doc);
   1.120 +        writer.flush();
   1.121 +    }
   1.122 +
   1.123 +    @Override
   1.124 +    public void readFrom(InputStream in) throws IOException {
   1.125 +        try {
   1.126 +            SAXReader reader = new SAXReader(true);
   1.127 +            reader.setStripWhitespaceText(true);
   1.128 +            
   1.129 +            reader.setFeature(Constants.XERCES_FEATURE_PREFIX+
   1.130 +                Constants.SCHEMA_VALIDATION_FEATURE, true);
   1.131 +            reader.setProperty(Constants.XERCES_PROPERTY_PREFIX +
   1.132 +                Constants.SCHEMA_LOCATION, String.format("%s %s",
   1.133 +                    NAMESPACE.getURI(), Resources.class.getResource(
   1.134 +                        "graph-document.xsd").toExternalForm()));
   1.135 +            
   1.136 +            final AtomicBoolean passed = new AtomicBoolean(true);
   1.137 +            final AtomicReference<SAXParseException> xmlerror = new AtomicReference<>();
   1.138 +            // TODO: we should do more detailed error handling here
   1.139 +            reader.setErrorHandler(new ErrorHandler() {
   1.140 +                @Override
   1.141 +                public void warning(SAXParseException exception) throws SAXException {
   1.142 +                }
   1.143 +
   1.144 +                @Override
   1.145 +                public void error(SAXParseException exception) throws SAXException {
   1.146 +                    xmlerror.set(exception);
   1.147 +                    passed.set(false);
   1.148 +                }
   1.149 +
   1.150 +                @Override
   1.151 +                public void fatalError(SAXParseException exception) throws SAXException {
   1.152 +                    xmlerror.set(exception);
   1.153 +                    passed.set(false);
   1.154 +                }
   1.155 +                
   1.156 +            });
   1.157 +            Document doc = reader.read(in);
   1.158 +            if (!passed.get()) {
   1.159 +                // TODO: provide details (maybe via separate error object?)
   1.160 +                throw xmlerror.get();
   1.161 +            }
   1.162 +            
   1.163 +            doc.normalize();
   1.164 +            
   1.165 +            Element root = doc.getRootElement();
   1.166 +            metadata.read(root.element(TAG_METADATA));
   1.167 +            
   1.168 +            if (graph instanceof Graph) {
   1.169 +                readGraph(root.element(TAG_GRAPH));
   1.170 +            } else if (graph instanceof Digraph) {
   1.171 +                readGraph(root.element(TAG_DIGRAPH));
   1.172 +            } else {
   1.173 +                throw new IOException("unsupported graph type");
   1.174 +            }
   1.175 +        } catch (DocumentException | SAXException ex) {
   1.176 +            throw new IOException(ex);
   1.177 +        }
   1.178 +    }
   1.179 +}

mercurial