You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by tr...@apache.org on 2009/06/18 23:09:35 UTC

svn commit: r786280 - in /jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons: logging/WriterLogWriter.java logging/WriterLogWriterProvider.java nodetype/compact/CompactNodeTypeDefReader.java nodetype/compact/Lexer.java

Author: tripod
Date: Thu Jun 18 21:09:34 2009
New Revision: 786280

URL: http://svn.apache.org/viewvc?rev=786280&view=rev
Log:
Adding convenience classes and methods

Added:
    jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/logging/WriterLogWriter.java
      - copied, changed from r786001, jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/logging/Slf4jLogWriter.java
    jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/logging/WriterLogWriterProvider.java
      - copied, changed from r786001, jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/logging/Slf4jLogWriterProvider.java
Modified:
    jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/nodetype/compact/CompactNodeTypeDefReader.java
    jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/nodetype/compact/Lexer.java

Copied: jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/logging/WriterLogWriter.java (from r786001, jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/logging/Slf4jLogWriter.java)
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/logging/WriterLogWriter.java?p2=jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/logging/WriterLogWriter.java&p1=jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/logging/Slf4jLogWriter.java&r1=786001&r2=786280&rev=786280&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/logging/Slf4jLogWriter.java (original)
+++ jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/logging/WriterLogWriter.java Thu Jun 18 21:09:34 2009
@@ -18,23 +18,27 @@
 
 import java.io.PrintWriter;
 import java.io.StringWriter;
-
-import org.slf4j.Logger;
+import java.io.Writer;
 
 /**
- * {@link LogWriter} implementation which uses SLF4J for persisting log messages.
+ * {@link LogWriter} implementation which uses a {@link Writer} for persisting log messages.
  */
-public class Slf4jLogWriter implements LogWriter {
-    private final Logger log;
+public class WriterLogWriter implements LogWriter {
+
+    private final PrintWriter log;
+
+    private final String category;
 
     /**
-     * Create a new instance which uses the passed SLF4J logger for persisting
+     * Create a new instance which uses the passed writer logger for persisting
      * the log messages.
-     * @param log
+     * @param log writer for output
+     * @param category log category
      */
-    public Slf4jLogWriter(Logger log) {
+    public WriterLogWriter(Writer log, String category) {
         super();
-        this.log = log;
+        this.log = new PrintWriter(log);
+        this.category = category;
     }
 
     /**
@@ -53,9 +57,7 @@
      * {@inheritDoc}
      */
     public void enter(final String methodName, final Object[] args) {
-        if (log.isDebugEnabled()) {
-            log.debug("ENTER(" + systemTime() + ") | " + methodName + "(" + formatArgs(args) + ")");
-        }
+        print("ENTER(" + systemTime() + ") | " + methodName + "(" + formatArgs(args) + ")");
     }
 
     /**
@@ -63,10 +65,8 @@
      * {@inheritDoc}
      */
     public void leave(final String methodName, final Object[] args, final Object result) {
-        if (log.isDebugEnabled()) {
-            log.debug("LEAVE(" + systemTime() + ") | " + methodName + "(" + formatArgs(args) + ") = "
-                    + formatResult(result));
-        }
+        print("LEAVE(" + systemTime() + ") | " + methodName + "(" + formatArgs(args) + ") = "
+                + formatResult(result));
     }
 
     /**
@@ -74,12 +74,16 @@
      * {@inheritDoc}
      */
     public void error(final String methodName, final Object[] args, final Exception e) {
-        if (log.isDebugEnabled()) {
-            log.debug("ERROR(" + systemTime() + ") | " + methodName + "(" + formatArgs(args) + ") | "
-                    + formatException(e));
-        }
+        print("ERROR(" + systemTime() + ") | " + methodName + "(" + formatArgs(args) + ") | "
+                + formatException(e));
     }
 
+    private void print(String msg) {
+        log.print(category);
+        log.print(": ");
+        log.println(msg);
+        log.flush();
+    }
     // -----------------------------------------------------< private >---
 
     private String formatArgs(Object[] args) {
@@ -102,9 +106,9 @@
 
     private void formatArgs(Object[] args, StringBuffer b) {
         String separator = "";
-        for (int k = 0; k < args.length; k++) {
+        for (Object arg : args) {
             b.append(separator);
-            formatArg(args[k], b);
+            formatArg(arg, b);
             separator = ", ";
         }
     }
@@ -120,4 +124,4 @@
         }
     }
 
-}
+}
\ No newline at end of file

Copied: jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/logging/WriterLogWriterProvider.java (from r786001, jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/logging/Slf4jLogWriterProvider.java)
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/logging/WriterLogWriterProvider.java?p2=jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/logging/WriterLogWriterProvider.java&p1=jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/logging/Slf4jLogWriterProvider.java&r1=786001&r2=786280&rev=786280&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/logging/Slf4jLogWriterProvider.java (original)
+++ jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/logging/WriterLogWriterProvider.java Thu Jun 18 21:09:34 2009
@@ -16,6 +16,8 @@
  */
 package org.apache.jackrabbit.spi.commons.logging;
 
+import java.io.Writer;
+
 import org.apache.jackrabbit.spi.Batch;
 import org.apache.jackrabbit.spi.IdFactory;
 import org.apache.jackrabbit.spi.NameFactory;
@@ -23,91 +25,99 @@
 import org.apache.jackrabbit.spi.QValueFactory;
 import org.apache.jackrabbit.spi.RepositoryService;
 import org.apache.jackrabbit.spi.SessionInfo;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
- * {@link LogWriterProvider} instance which provides {@link Slf4jLogWriter}s.
+ * {@link LogWriterProvider} instance which provides {@link WriterLogWriter}s.
  */
-public class Slf4jLogWriterProvider implements LogWriterProvider {
+public class WriterLogWriterProvider implements LogWriterProvider {
+
+    /**
+     * internal writer
+     */
+    private final Writer log;
+
+    /**
+     * Creates a new WriterLogWriterProvider based on the given writer
+     * @param log the writer
+     */
+    public WriterLogWriterProvider(Writer log) {
+        this.log = log;
+    }
 
     /**
-     * Returns a {@link Slf4jLogWriter} if the logger for <code>
+     * Returns a {@link WriterLogWriter} if the logger for <code>
      * service.getClass()</code> has debug level enabled. Returns
      * <code>null</code> otherwise.
      * {@inheritDoc}
      */
     public LogWriter getLogWriter(RepositoryService service) {
-        return getLogWriterInternal(service);
+        return getLogWriterInternal(log, service);
     }
 
     /**
-     * Returns a {@link Slf4jLogWriter} if the logger for <code>
+     * Returns a {@link WriterLogWriter} if the logger for <code>
      * nameFactory.getClass()</code> has debug level enabled. Returns
      * <code>null</code> otherwise.
      * {@inheritDoc}
      */
     public LogWriter getLogWriter(NameFactory nameFactory) {
-        return getLogWriterInternal(nameFactory);
+        return getLogWriterInternal(log, nameFactory);
     }
 
     /**
-     * Returns a {@link Slf4jLogWriter} if the logger for <code>
+     * Returns a {@link WriterLogWriter} if the logger for <code>
      * pathFactory.getClass()</code> has debug level enabled. Returns
      * <code>null</code> otherwise.
      * {@inheritDoc}
      */
     public LogWriter getLogWriter(PathFactory pathFactory) {
-        return getLogWriterInternal(pathFactory);
+        return getLogWriterInternal(log, pathFactory);
     }
 
     /**
-     * Returns a {@link Slf4jLogWriter} if the logger for <code>
+     * Returns a {@link WriterLogWriter} if the logger for <code>
      * idFactory.getClass()</code> has debug level enabled. Returns
      * <code>null</code> otherwise.
      * {@inheritDoc}
      */
     public LogWriter getLogWriter(IdFactory idFactory) {
-        return getLogWriterInternal(idFactory);
+        return getLogWriterInternal(log, idFactory);
     }
 
     /**
-     * Returns a {@link Slf4jLogWriter} if the logger for <code>
+     * Returns a {@link WriterLogWriter} if the logger for <code>
      * valueFactory.getClass()</code> has debug level enabled. Returns
      * <code>null</code> otherwise.
      * {@inheritDoc}
      */
     public LogWriter getLogWriter(QValueFactory valueFactory) {
-        return getLogWriterInternal(valueFactory);
+        return getLogWriterInternal(log, valueFactory);
     }
 
     /**
-     * Returns a {@link Slf4jLogWriter} if the logger for <code>
+     * Returns a {@link WriterLogWriter} if the logger for <code>
      * sessionInfo.getClass()</code> has debug level enabled. Returns
      * <code>null</code> otherwise.
      * {@inheritDoc}
      */
     public LogWriter getLogWriter(SessionInfo sessionInfo) {
-        return getLogWriterInternal(sessionInfo);
+        return getLogWriterInternal(log, sessionInfo);
     }
 
     /**
-     * Returns a {@link Slf4jLogWriter} if the logger for <code>
+     * Returns a {@link WriterLogWriter} if the logger for <code>
      * batch.getClass()</code> has debug level enabled. Returns
      * <code>null</code> otherwise.
      * {@inheritDoc}
      */
     public LogWriter getLogWriter(Batch batch) {
-        return getLogWriterInternal(batch);
+        return getLogWriterInternal(log, batch);
     }
 
     // -----------------------------------------------------< private >---
 
-    private static LogWriter getLogWriterInternal(Object object) {
-        Logger log = LoggerFactory.getLogger(object.getClass());
-        return log.isDebugEnabled()
-            ? new Slf4jLogWriter(log)
-            : null;
+    private static LogWriter getLogWriterInternal(Writer log, Object object) {
+        return new WriterLogWriter(log, object.getClass().getSimpleName());
     }
 
-}
+}
\ No newline at end of file

Modified: jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/nodetype/compact/CompactNodeTypeDefReader.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/nodetype/compact/CompactNodeTypeDefReader.java?rev=786280&r1=786279&r2=786280&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/nodetype/compact/CompactNodeTypeDefReader.java (original)
+++ jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/nodetype/compact/CompactNodeTypeDefReader.java Thu Jun 18 21:09:34 2009
@@ -16,17 +16,22 @@
  */
 package org.apache.jackrabbit.spi.commons.nodetype.compact;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.io.Reader;
+import java.util.HashSet;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Set;
-import java.util.HashSet;
 
 import javax.jcr.NamespaceException;
 import javax.jcr.PropertyType;
 import javax.jcr.RepositoryException;
-import javax.jcr.ValueFormatException;
 import javax.jcr.Session;
+import javax.jcr.ValueFormatException;
 import javax.jcr.nodetype.NodeTypeDefinition;
 import javax.jcr.query.qom.QueryObjectModelConstants;
 import javax.jcr.version.OnParentVersionAction;
@@ -150,7 +155,43 @@
     private final QNodeTypeDefinitionsBuilder builder;
 
     /**
-     * Creates a new CND reader and parses the given stream it directly.
+     * Convenience method that creates a new CND reader and parses the given
+     * file directly.
+     *
+     * @param file A CND file
+     * @return a new 'parsed' reader object
+     * @throws ParseException if an error occurs
+     * @throws IOException if an I/O error occurs.
+     */
+    public static CompactNodeTypeDefReader read(File file)
+            throws ParseException, IOException {
+        InputStream in = null;
+        Reader r = null;
+        try {
+            in = new FileInputStream(file);
+            r = new InputStreamReader(in, "utf8");
+            return new CompactNodeTypeDefReader(r, file.getPath());
+        } finally {
+            if (r != null) {
+                try {
+                    r.close();
+                } catch (IOException e) {
+                    // ignore
+                }
+            }
+            if (in != null) {
+                try {
+                    in.close();
+                } catch (IOException e) {
+                    // ignore
+                }
+            }
+        }
+    }
+
+
+    /**
+     * Creates a new CND reader and parses the given stream directly.
      *
      * @param r a reader to the CND
      * @param systemId a informative id of the given stream
@@ -215,6 +256,14 @@
     }
 
     /**
+     * Returns the previously assigned system id
+     * @return the system id
+     */
+    public String getSystemId() {
+        return lexer.getSystemId();
+    }
+
+    /**
      * Returns the list of parsed QNodeTypeDefinition definitions.
      *
      * @return a collection of QNodeTypeDefinition objects

Modified: jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/nodetype/compact/Lexer.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/nodetype/compact/Lexer.java?rev=786280&r1=786279&r2=786280&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/nodetype/compact/Lexer.java (original)
+++ jackrabbit/trunk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/nodetype/compact/Lexer.java Thu Jun 18 21:09:34 2009
@@ -189,6 +189,14 @@
     }
 
     /**
+     * Returns the system id
+     * @return the system id
+     */
+    public String getSystemId() {
+        return systemId;
+    }
+
+    /**
      * Creates a failure exception including the current line number and systemid.
      * @param message message
      * @throws ParseException the created exception