[logback-dev] svn commit: r1570 - logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/action

noreply.ceki at qos.ch noreply.ceki at qos.ch
Tue Aug 21 21:47:31 CEST 2007


Author: ceki
Date: Tue Aug 21 21:47:31 2007
New Revision: 1570

Added:
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/action/IncludeFileAction.java
      - copied, changed from r1563, /logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/joran/action/IncludeFileAction.java

Log:
inclusion should be a core joran feature

Copied: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/action/IncludeFileAction.java (from r1563, /logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/joran/action/IncludeFileAction.java)
==============================================================================
--- /logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/joran/action/IncludeFileAction.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/action/IncludeFileAction.java	Tue Aug 21 21:47:31 2007
@@ -7,7 +7,7 @@
  * the terms of the GNU Lesser General Public License as published by the Free
  * Software Foundation.
  */
-package ch.qos.logback.classic.joran.action;
+package ch.qos.logback.core.joran.action;
 
 import java.io.FileInputStream;
 import java.io.IOException;
@@ -23,63 +23,54 @@
 import ch.qos.logback.core.joran.spi.ActionException;
 import ch.qos.logback.core.joran.spi.InterpretationContext;
 import ch.qos.logback.core.joran.spi.JoranException;
+import ch.qos.logback.core.util.Loader;
+import ch.qos.logback.core.util.OptionHelper;
 
 public class IncludeFileAction extends Action {
 
   private static final String INCLUDED_TAG = "included";
   private static final String FILE_ATTR = "file";
   private static final String URL_ATTR = "url";
+  private static final String RESOURCE_ATTR = "resource";
   private SaxEventRecorder recorder = new SaxEventRecorder();;
 
   @Override
   public void begin(InterpretationContext ec, String name, Attributes attributes)
       throws ActionException {
 
-    String attFile = attributes.getValue(FILE_ATTR);
-    String attUrl = attributes.getValue(URL_ATTR);
-    // if both are null, report error and do nothing.
-    if (attFile == null && attUrl == null) {
-      addError("One of path and URL attribute must be set.");
+    String fileAttribute = attributes.getValue(FILE_ATTR);
+    String urlAttribute = attributes.getValue(URL_ATTR);
+    String resourceAttribute = attributes.getValue(RESOURCE_ATTR);
+    String attributeInUse = null;
+
+    if(!checkAttributes(fileAttribute, urlAttribute, resourceAttribute)) {
       return;
     }
 
-    String pathToFile = null;
-    if (attFile != null) {
-      if (attFile.startsWith("$")) {
-        pathToFile = ec.subst(attFile);
-      } else {
-        pathToFile = attFile;
-      }
+    InputStream in = null;
+
+    if (!OptionHelper.isEmpty(fileAttribute)) {
+      attributeInUse = ec.subst(fileAttribute);
+      in = getInputStreamByFilePath(attributeInUse);
     }
 
-    URL urlToFile = null;
-    String tmpUrl;
-    if (attUrl != null) {
-      if (attUrl.startsWith("$")) {
-        tmpUrl = ec.subst(attUrl);
-      } else {
-        tmpUrl = attUrl;
-      }
-      try {
-        urlToFile = new URL(tmpUrl);
-      } catch (MalformedURLException mue) {
-        String errMsg = "URL [" + tmpUrl + "] is not well formed.";
-        addError(errMsg, mue);
-        return;
-      }
+    if (!OptionHelper.isEmpty(urlAttribute)) {
+      attributeInUse = ec.subst(urlAttribute);
+      in = getInputStreamByUrl(attributeInUse);
     }
 
-    // we know now that either pathToFile or urlToFile
-    // is not null and correctly formed (in case of urlToFile).
+    if (!OptionHelper.isEmpty(resourceAttribute)) {
+      attributeInUse = ec.subst(resourceAttribute);
+      in = getInputStreamByResource(attributeInUse);
+    }
 
     try {
-      InputStream in = getInputStream(pathToFile, urlToFile);
       if (in != null) {
         parseAndRecord(in);
         in.close();
       }
     } catch (JoranException e) {
-      addError("Error while parsing file " + pathToFile + e);
+      addError("Error while parsing  " + attributeInUse, e);
     } catch (IOException e) {
       // called if in.close did not work
     }
@@ -88,8 +79,8 @@
       return;
     }
 
-    //Let's remove the two <included> events before
-    //adding the events to the player.
+    // Let's remove the two <included> events before
+    // adding the events to the player.
     SaxEvent first = recorder.saxEventList.get(0);
     if (first != null && first.qName.equalsIgnoreCase(INCLUDED_TAG)) {
       recorder.saxEventList.remove(0);
@@ -103,24 +94,73 @@
     ec.getJoranInterpreter().addEvents(recorder.saxEventList);
   }
 
-  private InputStream getInputStream(String pathToFile, URL urlToFile) {
-    if (pathToFile != null) {
-      try {
-        return new FileInputStream(pathToFile);
-      } catch (IOException ioe) {
-        String errMsg = "File [" + pathToFile + "] does not exist.";
-        addError(errMsg, ioe);
-        return null;
-      }
-    } else {
-      try {
-        return urlToFile.openStream();
-      } catch (IOException e) {
-        String errMsg = "URL [" + urlToFile.toString() + "] does not exist.";
-        addError(errMsg, e);
-        return null;
-      }
+  private boolean checkAttributes(String fileAttribute,
+      String urlAttribute, String resourceAttribute) {
+    int count = 0;
+
+    if (!OptionHelper.isEmpty(fileAttribute)) {
+      count++;
+    }
+    if (!OptionHelper.isEmpty(urlAttribute)) {
+      count++;
+    }
+    if (!OptionHelper.isEmpty(resourceAttribute)) {
+      count++;
+    }
+
+    if (count == 0) {
+      addError("One of \"path\", \"resource\" or \"url\" attributes must be set.");
+      return false;
+    } else if (count > 1) {
+      addError("Only one of \"file\", \"url\" or \"resource\" attributes should be set.");
+      return false;
+    } else if (count == 1) {
+      return true;
+    }
+    throw new IllegalStateException("Count value ["+count+"] is not expected");
+  }
+
+  private InputStream getInputStreamByFilePath(String pathToFile) {
+    try {
+      return new FileInputStream(pathToFile);
+    } catch (IOException ioe) {
+      String errMsg = "File [" + pathToFile + "] does not exist.";
+      addError(errMsg, ioe);
+      return null;
+    }
+  }
+
+  private InputStream getInputStreamByUrl(String urlAttribute) {
+    URL url;
+    try {
+      url = new URL(urlAttribute);
+    } catch (MalformedURLException mue) {
+      String errMsg = "URL [" + urlAttribute + "] is not well formed.";
+      addError(errMsg, mue);
+      return null;
+    }
+    return openURL(url);
+  }
+
+  InputStream openURL(URL url) {
+    try {
+      return url.openStream();
+    } catch (IOException e) {
+      String errMsg = "Failed to open [" + url.toString() + "]";
+      addError(errMsg, e);
+      return null;
+    }
+  }
+
+  private InputStream getInputStreamByResource(String resourceAttribute) {
+    URL url = Loader.getResourceByTCL(resourceAttribute);
+    if (url == null) {
+      String errMsg = "Could not find resource corresponding to ["
+          + resourceAttribute + "]";
+      addError(errMsg);
+      return null;
     }
+    return openURL(url);
   }
 
   private void parseAndRecord(InputStream inputSource) throws JoranException {



More information about the logback-dev mailing list