chemistry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1222221 - in /chemistry/playground/moreq2010/moreq2010/src/main: java/org/apache/chemistry/moreq2010/util/CmisClient.java java/org/apache/chemistry/moreq2010/util/Main.java resources/log4j.properties
Date Thu, 22 Dec 2011 13:26:57 GMT
Author: jens
Date: Thu Dec 22 13:26:57 2011
New Revision: 1222221

URL: http://svn.apache.org/viewvc?rev=1222221&view=rev
Log:
Make functionality available from command line. Error handling for validation.

Modified:
    chemistry/playground/moreq2010/moreq2010/src/main/java/org/apache/chemistry/moreq2010/util/CmisClient.java
    chemistry/playground/moreq2010/moreq2010/src/main/java/org/apache/chemistry/moreq2010/util/Main.java
    chemistry/playground/moreq2010/moreq2010/src/main/resources/log4j.properties

Modified: chemistry/playground/moreq2010/moreq2010/src/main/java/org/apache/chemistry/moreq2010/util/CmisClient.java
URL: http://svn.apache.org/viewvc/chemistry/playground/moreq2010/moreq2010/src/main/java/org/apache/chemistry/moreq2010/util/CmisClient.java?rev=1222221&r1=1222220&r2=1222221&view=diff
==============================================================================
--- chemistry/playground/moreq2010/moreq2010/src/main/java/org/apache/chemistry/moreq2010/util/CmisClient.java
(original)
+++ chemistry/playground/moreq2010/moreq2010/src/main/java/org/apache/chemistry/moreq2010/util/CmisClient.java
Thu Dec 22 13:26:57 2011
@@ -109,7 +109,6 @@ public class CmisClient implements Moreq
     @Override
     public void init() {
  
-//        String url = System.getProperty(SYSPROP_URL, "http://localhost:8080/inmemory/atom");

         String url = System.getProperty(SYSPROP_URL, "http://localhost:8080/inmemory/services/RepositoryService?wsdl");
         BindingType binding;
         String username = System.getProperty(SYSPROP_USER, "morequser");        

Modified: chemistry/playground/moreq2010/moreq2010/src/main/java/org/apache/chemistry/moreq2010/util/Main.java
URL: http://svn.apache.org/viewvc/chemistry/playground/moreq2010/moreq2010/src/main/java/org/apache/chemistry/moreq2010/util/Main.java?rev=1222221&r1=1222220&r2=1222221&view=diff
==============================================================================
--- chemistry/playground/moreq2010/moreq2010/src/main/java/org/apache/chemistry/moreq2010/util/Main.java
(original)
+++ chemistry/playground/moreq2010/moreq2010/src/main/java/org/apache/chemistry/moreq2010/util/Main.java
Thu Dec 22 13:26:57 2011
@@ -19,42 +19,132 @@
 package org.apache.chemistry.moreq2010.util;
 
 import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileReader;
+import java.io.IOException;
+import java.net.URL;
 
 import javax.xml.bind.JAXBContext;
-import javax.xml.bind.JAXBElement;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Unmarshaller;
-import javax.xml.stream.EventFilter;
-import javax.xml.stream.XMLEventReader;
-import javax.xml.stream.XMLInputFactory;
-import javax.xml.stream.XMLStreamException;
-import javax.xml.stream.events.XMLEvent;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
 
 import org.apache.chemistry.moreq2010.jaxb.Export;
-import org.apache.chemistry.moreq2010.jaxb.RecordTYPE;
 import org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.w3c.dom.Document;
+import org.xml.sax.ErrorHandler;
 import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
 
 public class Main {
 
     static final Log LOG = LogFactory.getLog(Main.class);
     
+    enum Action { IMPORT, DUMP, VALIDATE, USAGE };
+    
     public static void main( String[] args ) {
-
+        
         LOG.info("Main starting...");
+        Action action = Action.IMPORT;
+        
         String inputFileName = args.length > 0 ? args[0] : null;
-        if (null == inputFileName || inputFileName.length() == 0)
-            inputFileName = "src/test/resources/example1.xml";
+        if (null == inputFileName || inputFileName.length() == 0) {
+            System.out.println("Error: No file name given.");
+            usage();
+        } else {
+            for (String arg : args) {
+                if (arg.equals("-import"))
+                    action = Action.IMPORT;
+                else if (arg.equals("-dump"))
+                    action = Action.DUMP;
+                else if (arg.equals("-validate"))
+                    action = Action.VALIDATE;
+                else if (arg.equals("-usage"))                    
+                    action = Action.USAGE;
+                else if (arg.startsWith("-")) {
+                    System.out.println("Error: Unknown option: " + arg);
+                    usage();
+                } else
+                    inputFileName = arg;                    
+            }
+        }
+        
+        switch (action) {
+        case IMPORT:
+            if (checkFile(inputFileName))
+                importToCMIS(inputFileName);
+            break;
+        case DUMP:
+            if (checkFile(inputFileName))
+                dumpFileAsText(inputFileName);
+            break;
+        case VALIDATE:
+            if (checkFile(inputFileName))
+                validate(inputFileName);
+            break;
+        case USAGE:
+            usage();
+            break;
+        default:
+            System.out.println("Error no action specified.");
+            usage();        
+        }
+        LOG.debug("...Main ended.");
+    }
+    
+    private static boolean checkFile(String fileName) {
+        File f  = new File(fileName);
+        if (!f.exists()) {
+            System.err.println(" File " +  fileName + " does not exist");
+            return false;
+        }
+        if (!f.canRead()) {
+            System.err.println(" File " +  fileName + " cannot be read.");
+            return false;
+        }
+        return true;
+    }
+    
+    public static void usage() {
+        System.out.println("MoReq2010 Import tool for CMIS.");
+        System.out.println("This tool takes a MoReq2010 compliant XML file and imports it
into a CMIS");
+        System.out.println("repository. Please note that the target CMIS repository must
have the correct");
+        System.out.println("MoReq2010 type definitions. Otherwise it will not work.");
+        System.out.println("This tool can also dump the content of a Moreq2010 file in readable
text form");
+        System.out.println("and validate a file against the MoReq2010 XML schema.");
+        System.out.println();
+        System.out.println("Usage: [-option] file");
+        System.out.println("   option is one of -import, -dump, -validate, -usage:");
+        System.out.println("   - import: import a file to CMIS (requires additional connection
parameters)");
+        System.out.println("   - dump: print the content of the XML file as plain text");
+        System.out.println("   - validate: validates a file against the MoReq2010 XML schema.");
+        System.out.println("   - usage: this message");
+        System.out.println("   file is a MoReq2010 XML export file");
+        System.out.println("If no option is specified, import is the default action.");
+        System.out.println("Connection to a CMIS repository requires the following connection
parameters:");
+        System.out.println("-Dcmis.url=<connection string to repository>:");
+        System.out.println("-Dcmis.binding=<webservices or atompub>:");
+        System.out.println("-Dcmis.repository=<CMIS repository id>:");
+        System.out.println("-Dcmis.user=<user for authentication>:");
+        System.out.println("-Dcmis.password=<password for authentication>:");
+        System.out.println("-Dcmis.password=<password for authentication>:");
+        System.out.println("Example:");        
+        System.out.println("-Dcmis.url=http://localhost:8080/inmemory/services/RepositoryService?wsdl");
+        System.out.println("-Dcmis.binding=webservices:");
+        System.out.println("-Dcmis.repository=A1");
+        System.out.println("-Dcmis.user=moreq2010user");
+        System.out.println("-Dcmis.password=secret");
+    }
+    
+    public static void importToCMIS( String xmlFileName ) {
         try {
             MoreqParserCallback cmisClient = ParserCallbackFactory.createParserCallback(CmisClient.class.getName());
             MoreqXmlParser parser = new MoreqXmlParser(cmisClient);
             try {
                 cmisClient.init();
-                parser.parse(inputFileName);
+                parser.parse(xmlFileName);
             } catch (CmisBaseException e) {
                 System.err.println("Error connecting to CMIS repository :\n" + e.getErrorContent());
             }
@@ -62,36 +152,55 @@ public class Main {
         } catch (Exception e) {
             System.err.println("Error during parsing file: " + e);
             e.printStackTrace();
-        }
-        LOG.debug("...Main ended.");
+        }        
     }
     
-    public static void main2( String[] args ) {
+    
+    public static void dumpFileAsText( String xmlFileName ) {
 
-        String inputFileName = "src/test/resources/example1.xml";
         try {
             Main main = new Main();
-            Export export = main.parseMemory(inputFileName);
+            Export export = main.parseMemory(xmlFileName);
             Dump dump = new Dump();
-            dump.dumpInfo(export, inputFileName);
+            dump.dumpInfo(export, xmlFileName);
         } catch (Exception e) {
             System.err.println("Error during parsing file: " + e.getMessage());
             e.printStackTrace();
         }
     }
     
-    public static void main3( String[] args ) {
+    public static void validate(String xmlFileName) {
+        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+        URL schema = Main.class.getResource("/export.xsd");
+
+        factory.setValidating(true);
+        factory.setNamespaceAware(true);
+        factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
+                "http://www.w3.org/2001/XMLSchema");
+        factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource",
+                schema.toString());
 
-        String inputFileName = "src/test/resources/example2.xml";
+        LOG.debug("Schema file is: " + schema);
+        
         try {
-            Main main = new Main();
-            Export export = main.parseMemory(inputFileName);
-            inputFileName = "src/test/resources/example2.xml";
-            export = main.parseMemory(inputFileName);
-            Dump dump = new Dump();
-            dump.dumpInfo(export, inputFileName);
-        } catch (Exception e) {
-            System.err.println("Error during parsing file: " + e.getMessage());
+            DocumentBuilder parser = factory.newDocumentBuilder();
+            JaxbErrorHandler errHandler = new JaxbErrorHandler();
+            parser.setErrorHandler(errHandler);
+            Document doc = parser.parse(xmlFileName);
+            System.out.println();
+            if (errHandler.getErrors() == 0) {
+                System.out.println("Parsing of file " + xmlFileName + " succeded.");
+            } else {
+                System.out.println("Parsing of file " + xmlFileName + " failed.");
+            }
+            System.out.println("(" + errHandler.getErrors() + " errors, "
+                    + errHandler.getWarnings() + " warnings)");
+        } catch (ParserConfigurationException e) {
+            System.out.println("Parser not configured: " + e.getMessage());
+        } catch (SAXException e) {
+            System.out.print("Parsing XML failed due to a " + e.getClass().getName() + ":");
+            System.out.println(e.getMessage());
+        } catch (IOException e) {
             e.printStackTrace();
         }
     }
@@ -107,4 +216,36 @@ public class Main {
         return export;
     }
     
+    private static class JaxbErrorHandler implements ErrorHandler {
+        
+        int errors = 0;
+        int warnings = 0;
+
+        public void error(SAXParseException e) throws SAXException {
+            System.err.println("Error (Line: " + e.getLineNumber() + ", Col: " + e.getColumnNumber()
+ "): ");
+            System.err.println(e.getMessage());
+            ++errors;
+        }
+
+        public void fatalError(SAXParseException e) throws SAXException {
+            System.err.println("Fatal Error (Line: " + e.getLineNumber() + ", Col: " + e.getColumnNumber()
+ "): ");
+            System.err.println(e.getMessage());
+            ++errors;
+        }
+
+        public void warning(SAXParseException e) throws SAXException {
+            System.err.println("Warning (Line: " + e.getLineNumber() + ", Col: " + e.getColumnNumber()
+ "):");
+            System.err.println(e.getMessage());
+            ++warnings;
+        }
+        
+        public int getErrors() {
+            return errors;
+        }
+
+        public int getWarnings() {
+            return warnings;
+        }
+    }
+
 }
\ No newline at end of file

Modified: chemistry/playground/moreq2010/moreq2010/src/main/resources/log4j.properties
URL: http://svn.apache.org/viewvc/chemistry/playground/moreq2010/moreq2010/src/main/resources/log4j.properties?rev=1222221&r1=1222220&r2=1222221&view=diff
==============================================================================
--- chemistry/playground/moreq2010/moreq2010/src/main/resources/log4j.properties (original)
+++ chemistry/playground/moreq2010/moreq2010/src/main/resources/log4j.properties Thu Dec 22
13:26:57 2011
@@ -15,17 +15,17 @@
 
 # sample log4j.properties
 
-log4j.rootCategory=DEBUG, R, O
+log4j.rootCategory=WARN, R, O
 
 # Stdout
 log4j.appender.O=org.apache.log4j.ConsoleAppender
 
 # File
 log4j.appender.R=org.apache.log4j.RollingFileAppender
-log4j.appender.R.File=log4j.log
+log4j.appender.R.File=moreq2010tool.log
 
 # Control the maximum log file size
-log4j.appender.R.MaxFileSize=100KB
+log4j.appender.R.MaxFileSize=500KB
 
 # Archive log files (one backup file here)
 log4j.appender.R.MaxBackupIndex=1



Mime
View raw message