Java tutorial
/** * Squidy Interaction Library is free software: you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation, either version 3 of the License, * or (at your option) any later version. * * Squidy Interaction Library is distributed in the hope that it will be * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with Squidy Interaction Library. If not, see * <http://www.gnu.org/licenses/>. * * 2009 Human-Computer Interaction Group, University of Konstanz. * <http://hci.uni-konstanz.de> * * Please contact info@squidy-lib.de or visit our website * <http://www.squidy-lib.de> for further information. */ package org.squidy.designer.model; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import javax.xml.bind.ValidationEvent; import javax.xml.bind.ValidationEventHandler; import javax.xml.bind.ValidationEventLocator; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.squidy.SquidyException; import org.squidy.common.dynamiccode.DynamicCodeClassLoader; import org.squidy.common.util.ReflectionUtil; import org.squidy.manager.data.Processor; import org.squidy.manager.model.Node; import org.squidy.manager.model.Pipe; import org.squidy.manager.model.Pipeline; import org.squidy.manager.model.Workspace; import org.squidy.manager.scanner.PackageScanner; /** * <code>ModelViewHandler</code>. * * <pre> * Date: Feb 21, 2009 * Time: 11:34:09 PM * </pre> * * @author * Roman R&aumldle<br /> * <a href="mailto:Roman.Raedle@uni-konstanz.de">Roman.Raedle@uni-konstanz.de</a><br /> * Human-Computer Interaction Group<br /> * University of Konstanz * * @version $Id: ModelViewHandler.java 772 2011-09-16 15:39:44Z raedle $ * @since 1.0.0 */ public class ModelViewHandler { // Logger to log info, error, debug,... messages. private static final Log LOG = LogFactory.getLog(ModelViewHandler.class); private static ModelViewHandler instance; /** * Singleton pattern. * * @return Single instance of diagram model parser. */ public static ModelViewHandler getModelViewHandler() { if (instance == null) { instance = new ModelViewHandler(); } return instance; } /** * Use singleton pattern. */ protected ModelViewHandler() { } private static JAXBContext context; /** * Initializes the JAXB context with all required classes. * * @return The initialized JAXB context. * @throws JAXBException * The exception will be thrown if initialization of the JAXB * context has been failed. */ protected JAXBContext getContext() throws JAXBException { if (context != null) { return context; } ClassLoader classLoader = DynamicCodeClassLoader.DYNAMIC_CODE; return context = JAXBContext .newInstance(ReflectionUtil.loadContextClasses(Thread.currentThread().getContextClassLoader(), PackageScanner.findAllClassNamesWithAnnotation(Processor.class), Data.class, WorkspaceShape.class, Workspace.class, PipelineShape.class, Pipeline.class, NodeShape.class, Node.class, PipeShape.class, Pipe.class, LayerDemoShape.class)); } public static void resetJAXBContext(Class<?> type) { // TODO [RR]: This is a DIRTY hack! // PackageScanner.CLASS_CACHE.clear(); if (!PackageScanner.CLASS_CACHE.contains(type)) { PackageScanner.CLASS_CACHE.add(type); } String[] classes = PackageScanner.findAllClassNamesWithAnnotation(Processor.class); List<String> only = new ArrayList<String>(); for (String className : classes) { if (!type.getName().equals(className)) { only.add(className); System.out.println("IN: " + className); } else { System.out.println("REMOVED: " + className); } } try { context = null; ClassLoader classLoader = DynamicCodeClassLoader.DYNAMIC_CODE; context = JAXBContext .newInstance(ReflectionUtil.loadContextClasses(classLoader, only.toArray(new String[0]), type, Data.class, WorkspaceShape.class, Workspace.class, PipelineShape.class, Pipeline.class, NodeShape.class, Node.class, PipeShape.class, Pipe.class, LayerDemoShape.class)); } catch (JAXBException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * Parses a given input stream (should contain model and diagram code as xml * structure) and returns it as an object structure. * * @param inputStream * The input stream should contain model and diagram structure. * @return The parsed XMI document containing model and diagram in object * representation. * @throws SquidyException */ public Data load(InputStream inputStream) throws SquidyException { try { Unmarshaller unmarshaller = getContext().createUnmarshaller(); unmarshaller.setEventHandler(new ValidationEventHandler() { /* (non-Javadoc) * @see javax.xml.bind.ValidationEventHandler#handleEvent(javax.xml.bind.ValidationEvent) */ public boolean handleEvent(ValidationEvent e) { ValidationEventLocator locator = e.getLocator(); if (LOG.isErrorEnabled()) { LOG.error("Error while reading input stream: \"" + e.getMessage() + "\" [line=" + locator.getLineNumber() + ", column=" + locator.getColumnNumber() + "]"); } return true; } }); return (Data) unmarshaller.unmarshal(inputStream); } catch (JAXBException e) { throw new SquidyException("Could not unmarshal input stream.", e); } } /** * @param outputStream * @param xmi */ public void save(OutputStream outputStream, Data data) { try { Marshaller marshaller = getContext().createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new DefaultNamespacePrefixMapper()); marshaller.marshal(data, outputStream); } catch (JAXBException e) { throw new SquidyException("Could not marshal data.", e); } } }