All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.xolite.graph.PersonGraphExample Maven / Gradle / Ivy

/*-------------------------------------------------------------------------
 Copyright 2006 Olivier Berlanger

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 -------------------------------------------------------------------------*/
package net.sf.xolite.graph;


import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;

import net.sf.xolite.XMLParseException;
import net.sf.xolite.sax.SaxXMLEventParser;
import net.sf.xolite.utils.ConsoleXMLSerializer;
import net.sf.xolite.utils.SchemaLocation;
import net.sf.xolite.utils.StreamXMLSerializer;

import org.apache.log4j.xml.DOMConfigurator;
import org.xml.sax.InputSource;


public class PersonGraphExample {


    public void run(boolean useSchema) throws Exception {
        // setup the parser
        SaxXMLEventParser parser = (useSchema) ? new SaxXMLEventParser(getSchemaLocation()) : new SaxXMLEventParser();
        parser.setFactory(new PersonObjectFactory());
        // get the input stream
        String resourceName = "PersonGraph.xml";
        InputStream is = getClass().getResourceAsStream(resourceName);
        if (is == null) throw new XMLParseException("Resource '" + resourceName + "' not found");
        // parse the company
        PersonSet set = new PersonSet();
        parser.parse(new InputSource(is), set);
        // display result
        System.out.println("Loaded: set of " + set.size() + " persons");
        // check if the parsed graph is linked
        Person p = set.getPerson("John", "Smith");
        Person spouse = p.getChildren().iterator().next().getMother();
        System.out.println("Spouse of " + p + " is " + spouse);
        // serialize back in the console
        System.out.println("");
        ConsoleXMLSerializer.dump(set);
        // Also write it in a temporary file
        File outputFile = File.createTempFile("personGraphExample_", ".xml");
        OutputStream out = new BufferedOutputStream(new FileOutputStream(outputFile));
        StreamXMLSerializer serializer = new StreamXMLSerializer(out);
        serializer.startPrefixMapping("", Person.PERSON_NAMESPACE_URI); // define the Person namespace as the default one
        serializer.serializeObject(set);
        System.out.println("Written in temporary file: " + outputFile.getAbsolutePath());
    }


    /**
     * The schema location is the schema URI followed the URL pointing to the actual schema XML definition file (separated by a
     * space). Its exactly like the value of the 'xsi:schemaLocation' XML attribute.
     */
    public String getSchemaLocation() {
        return SchemaLocation.getResourceLocation(Person.PERSON_NAMESPACE_URI, getClass(), "PersonGraph.xsd");
    }


    /**
     * Configure log4j using the "log4j.xml" file from the default directory.
     */
    public static void setupLogging() {
        try {
            DOMConfigurator.configure(new File("log4j.xml").toURI().toURL());
        } catch (MalformedURLException mue) {
            throw new IllegalStateException("Invalid log4j configuration file URL", mue);
        }
    }


    public static void main(String[] args) throws Exception {
        setupLogging();
        new PersonGraphExample().run(true);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy