dloy avatar dloy committed 6124c63

Add json

Comments (0)

Files changed (4)

             <version>1.0-SNAPSHOT</version>
         </dependency>
         <dependency>
+            <groupId>org.cdlib.mrt</groupId>
+            <artifactId>mrt-json</artifactId>
+            <version>1.0-SNAPSHOT</version>
+        </dependency>
+        <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <version>4.5</version>
             <artifactId>ant</artifactId>
             <version>1.7.0</version>
         </dependency>
+        <dependency>
+            <groupId>jdom</groupId>
+            <artifactId>jdom</artifactId>
+            <version>1.1.1</version>
+        </dependency>
     </dependencies>
 
 </project>

core/src/main/java/org/cdlib/mrt/formatter/FormatterAbs.java

      * @return JSON formatter
      * @throws org.cdlib.mrt.utility.TException
      */
+    public static JSONFormatter getJSONFormatter(String mapperName, LoggerInf logger)
+            throws TException
+    {
+        JSONFormatter formatter = new JSONFormatter(mapperName, logger);
+        formatter.setFormatterType(FormatterInf.Format.json);
+        return formatter;
+    }
+
+    /**
+     * get JSON formatter
+     * @param logger process logger
+     * @return JSON formatter
+     * @throws org.cdlib.mrt.utility.TException
+     */
     public static JSONFormatter getJSONFormatter(LoggerInf logger)
             throws TException
     {

core/src/main/java/org/cdlib/mrt/formatter/JSONFormatter.java

 **********************************************************/
 package org.cdlib.mrt.formatter;
 
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.io.PrintStream;
-import org.cdlib.mrt.utility.JSONUtil;
+import java.util.Properties;
+import javax.xml.transform.*;
+
+import org.json.XML;
+import org.json.JSONObject;
+import org.cdlib.mrt.utility.FileUtil;
+import org.cdlib.mrt.utility.LoggerInf;
 import org.cdlib.mrt.utility.StateInf;
-import org.cdlib.mrt.utility.LoggerInf;
+import org.cdlib.mrt.utility.StringUtil;
 import org.cdlib.mrt.utility.TException;
+import org.cdlib.mrt.utility.XSLTUtil;
 
 /**
  * JSON output display formatter
         extends FormatterAbs
         implements FormatterInf
 {
+
+    protected XMLFormatter xmlFormatter = null;
+    protected String mapperName = null;
+    protected XMLMapper mapper = null;
+    private boolean debug = false;
+
     public JSONFormatter(LoggerInf logger)
             throws TException
     {
         super(logger);
+        xmlFormatter = FormatterAbs.getXMLFormatter(logger);
         formatterType = FormatterInf.Format.json;
     }
 
+    public JSONFormatter(String mapperName, LoggerInf logger)
+            throws TException
+    {
+        super(logger);
+        xmlFormatter = FormatterAbs.getXMLFormatter(mapperName, logger);
+        formatterType = FormatterInf.Format.json;
+        this.mapperName = mapperName;
+    }
+
+    @Override
+    public void format(StateInf stateFile, PrintStream stream)
+            throws TException
+    {
+        File tempFile = null;
+        try {
+            if (StringUtil.isEmpty(mapperName)) {
+                mapperName = "resources/XMLFormatNS.properties";
+            }
+            if (debug) System.out.println("!!!!" + MESSAGE + "format - mapperNameame=" + mapperName);
+            mapper = XMLMapper.getXMLMapper(mapperName, stateFile);
+            tempFile = FileUtil.getTempFile("xml", "xml");
+            FileOutputStream outStream = new FileOutputStream(tempFile);
+            PrintStream xmlStream = new PrintStream(outStream, true, "utf-8");
+            xmlFormatter.format(stateFile, xmlStream);
+            if (debug) System.out.println("!!!!XHTMLFormatter xml=" + FileUtil.file2String(tempFile));
+            File formatFile = transform(tempFile);
+            writeStream(formatFile, stream);
+
+
+        } catch (TException tex) {
+            throw tex;
+
+        } catch (Exception ex) {
+            if (debug) System.out.println("!!!!Exception trace:" + StringUtil.stackTrace(ex));
+            throw new TException.GENERAL_EXCEPTION (
+                    "JSONFormatter exception:" + ex);
+
+        } finally {
+            if (tempFile != null) {
+                try {
+                    tempFile.delete();
+                } catch (Exception ex) {}
+            }
+        }
+    }
+
+    protected void writeStream(
+            File formatFile,
+            PrintStream stream)
+        throws TException
+    {
+        InputStream inputStream = null;
+        try {
+            inputStream = new FileInputStream(formatFile);
+            byte [] buf = new byte[10000];
+            while (true) {
+                int len = inputStream.read(buf);
+                if (len < 0) break;
+                stream.write(buf, 0, len);
+            }
+        } catch (Exception ex) {
+            throw new TException.GENERAL_EXCEPTION(
+                    "JSONFormatter exception:" + ex);
+
+        } finally {
+            if (inputStream != null) {
+                try {
+                    inputStream.close();
+                } catch (Exception ex) { }
+            }
+        }
+
+    }
+
+    protected File transform(
+            File xmlFile)
+        throws TException
+    {
+        try {
+            String xmlS = FileUtil.file2String(xmlFile);
+            JSONObject json = XML.toJSONObject(xmlS);
+
+            File outTemp = FileUtil.getTempFile("json", "jsn");
+            String response = json.toString();
+            if (debug) System.out.println("!!!!JSONFormatter response=" + response);
+            FileUtil.string2File(outTemp, response);
+            return outTemp;
+
+        }  catch (Exception e) {
+            System.out.println(StringUtil.stackTrace(e));
+            throw new TException.GENERAL_EXCEPTION(
+                    "JSONLFormatter exception:" + e);
+
+        }
+  }
+
     @Override
     protected int printBegin(StateInf state, PrintStream stream)
             throws TException
     {
-        writeln("{", stream);
-        return 1;
+        return 0;
     }
 
     @Override
     protected void printEnd(PrintStream stream)
             throws TException
     {
-        writeln(NL + "}", stream);
     }
 
     @Override
     protected void printStart(String name, boolean isFirst, int lvl, PrintStream stream)
             throws TException
     {
-        if (!isFirst) write(",", stream);
-        write(NL, stream);
-        addLvl(lvl, stream);
-        write("\"" + name + "\": {", stream);
     }
 
     @Override
     protected void printClose(String name, int lvl, PrintStream stream)
             throws TException
     {
-        write(NL, stream);
-        addLvl(lvl, stream);
-        write("}", stream);
     }
 
     @Override
             PrintStream stream)
         throws TException
     {
-        if (value == null) value = "";
-        if (!isFirst) write(",", stream);
-        write(NL, stream);
-        addLvl(lvl, stream);
-        value = JSONUtil.encode(value);
-        if (isNumeric)
-            write("\"" + name + "\":" + value, stream);
-        else
-            write("\"" + name + "\":" + "\"" + value +"\"", stream);
     }
 }

core/src/test/java/org/cdlib/mrt/formatter/FormatterTest.java

     //
     // @Test
     // public void hello() {}
-       @Test public void testReference() 
-   {
+    @Test
+    public void testReference()
+    {
        String ret = null;
        try {
            FormatterInf anvl = FormatterAbs.getANVLFormatter(logger);
            System.out.println("*ANVL*" + NL + ret);
            ret = formatIt(xml, testA);
            System.out.println("*XML*" + NL + ret);
-           ret = formatIt(json, testA);
-           System.out.println("*JSON*" + NL + ret);
 
            assertTrue(true);
             
         }
     }
 
-    // TODO add test methods here.
-    // The methods must be annotated with annotation @Test. For example:
-    //
-    // @Test
-    // public void hello() {}
-       @Test public void testXHTML()
+
+   @Test
+   public void testXHTML()
    {
        String ret = null;
        try {
         }
     }
 
+    @Test
+    public void testJSON()
+    {
+       String ret = null;
+       try {
+           FormatterInf json = FormatterAbs.getJSONFormatter(
+                   "testresources/xml-test6.properties",
+                   logger);
+           TestA testA = new TestA();
+           ret = formatIt(json, testA);
+           System.out.println("*XHTML*" + NL + ret);
+
+           assertTrue(true);
+
+        } catch (Exception ex) {
+            System.out.println("Exception:" + ex);
+            System.out.println("Trace:" + StringUtil.stackTrace(ex));
+            assertFalse("Exception:" + ex, true);
+        }
+    }
+
     public String formatIt(
             FormatterInf formatter,
             StateInf responseState)
         MessageDigest digest5 = new MessageDigest("2fd4e1c67a2d28fced849ee1bb76e7391b93eb12", "shaxxx");
     }
 
-    private class TestA implements StateInf
+    private static class TestA implements StateInf
     {
         public String getValue()
         {
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.