upayavira    2003/08/27 12:18:18

  Modified:    src/java/org/apache/cocoon Main.java
               src/java/org/apache/cocoon/bean CocoonBean.java
                        CocoonWrapper.java
  Added:       src/java/org/apache/cocoon/bean Target.java
  Log:
  Moved Target into a separate class (ready for holding more complex URI 
arithmetic that I will extract from the CocoonBean)
  Moved error detection to correct place in Main from Wrapper
  
  Revision  Changes    Path
  1.12      +17 -3     cocoon-2.1/src/java/org/apache/cocoon/Main.java
  
  Index: Main.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/Main.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- Main.java 21 Jul 2003 09:37:40 -0000      1.11
  +++ Main.java 27 Aug 2003 19:18:18 -0000      1.12
  @@ -325,10 +325,24 @@
           }
   
           if (line.hasOption(WORK_DIR_OPT)) {
  -            cocoon.setWorkDir(line.getOptionValue(WORK_DIR_OPT));
  +            String workDir = line.getOptionValue(WORK_DIR_OPT);
  +            if (workDir.equals("")) {
  +                listener.messageGenerated(
  +                    "Careful, you must specify a work dir when using the 
-w/--workDir argument");
  +                System.exit(1);
  +            } else {
  +                cocoon.setWorkDir(line.getOptionValue(WORK_DIR_OPT));
  +            }
           }
           if (line.hasOption(CONTEXT_DIR_OPT)) {
  -            cocoon.setContextDir(line.getOptionValue(CONTEXT_DIR_OPT));
  +            String contextDir = line.getOptionValue(CONTEXT_DIR_OPT);
  +            if (contextDir.equals("")) {
  +                listener.messageGenerated(
  +                    "Careful, you must specify a configuration file when 
using the -c/--contextDir argument");
  +                System.exit(1);
  +            } else {  
  +                cocoon.setContextDir(contextDir);
  +            }
           }
           if (line.hasOption(CONFIG_FILE_OPT)) {
               cocoon.setConfigFile(line.getOptionValue(CONFIG_FILE_OPT));
  
  
  
  1.17      +26 -138   
cocoon-2.1/src/java/org/apache/cocoon/bean/CocoonBean.java
  
  Index: CocoonBean.java
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.1/src/java/org/apache/cocoon/bean/CocoonBean.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- CocoonBean.java   17 Aug 2003 13:50:45 -0000      1.16
  +++ CocoonBean.java   27 Aug 2003 19:18:18 -0000      1.17
  @@ -454,7 +454,7 @@
                   status =
                       getPage(
                           deparameterizedURI,
  -                        target.getLastModified(filename),
  +                        getLastModified(target, filename),
                           parameters,
                           confirmExtension ? translatedLinks : null,
                           gatheredLinks,
  @@ -498,7 +498,7 @@
               } finally {
                   if (output != null && status != -1) {
   
  -                    ModifiableSource source = target.getSource(filename);
  +                    ModifiableSource source = getSource(target, filename);
                       try {
                           OutputStream stream = source.getOutputStream();
   
  @@ -508,7 +508,7 @@
                       } catch (IOException ioex) {
                           log.warn(ioex.toString());
                       } finally {
  -                        target.releaseSource(source);
  +                        releaseSource(source);
                       }
                   }
                   
  @@ -585,7 +585,7 @@
               n.addExtraDescription(Notifying.EXTRA_REQUESTURI, uri);
               n.addExtraDescription("missing-file", uri);
   
  -            ModifiableSource source = target.getSource(filename);
  +            ModifiableSource source = getSource(target, filename);
               try {
                   OutputStream stream = source.getOutputStream();
   
  @@ -594,7 +594,7 @@
                   out.flush();
                   out.close();
               } finally {
  -                target.releaseSource(source);
  +                releaseSource(source);
               }
           }
       }
  @@ -620,137 +620,25 @@
           }
           return uri;
       }
  -
  -    public class Target {
  -        // Defult type is append
  -        private static final String APPEND_TYPE = "append";
  -        private static final String REPLACE_TYPE = "replace";
  -        private static final String INSERT_TYPE = "insert";
  -
  -        private final String type;
  -        private final String root;
  -        private final String sourceURI;
  -        private final String destURI;
  -
  -        private transient int _hashCode;
  -        private transient String _toString;
  -
  -        public Target(
  -            String type,
  -            String root,
  -            String sourceURI,
  -            String destURI)
  -            throws IllegalArgumentException {
  -            this.type = type;
  -            this.root = root;
  -            this.sourceURI = NetUtils.normalize(sourceURI);
  -            if (destURI == null || destURI.length() == 0) {
  -                throw new IllegalArgumentException("You must specify a 
destination directory when defining a target");
  -            }
  -            if (!destURI.endsWith("/")) {
  -                destURI += "/";
  -            }
  -            this.destURI = destURI;
  -        }
  -
  -        public Target(String type, String sourceURI, String destURI)
  -            throws IllegalArgumentException {
  -            this(type, "", sourceURI, destURI);
  -        }
  -
  -        public Target(String sourceURI, String destURI)
  -            throws IllegalArgumentException {
  -            this(APPEND_TYPE, "", sourceURI, destURI);
  -        }
  -
  -        public Target getDerivedTarget(String newURI)
  -            throws IllegalArgumentException {
  -            if (!newURI.startsWith(root)) {
  -                return null;
  -            }
  -            newURI = newURI.substring(root.length());
  -            return new Target(this.type, this.root, newURI, this.destURI);
  -        }
  -
  -        public String getFinalURI(String actualSourceURI)
  -            throws ProcessingException {
  -            if (!actualSourceURI.startsWith(root)) {
  -                throw new ProcessingException(
  -                    "Derived target does not share same root: "
  -                        + actualSourceURI);
  -            }
  -            actualSourceURI = actualSourceURI.substring(root.length());
  -
  -            if (APPEND_TYPE.equals(this.type)) {
  -                return destURI + actualSourceURI;
  -            } else if (REPLACE_TYPE.equals(this.type)) {
  -                return destURI;
  -            } else if (INSERT_TYPE.equals(this.type)) {
  -                int starPos = destURI.indexOf("*");
  -                if (starPos == -1) {
  -                    throw new ProcessingException("Missing * in replace 
mapper uri");
  -                } else if (starPos == destURI.length() - 1) {
  -                    return destURI.substring(0, starPos) + actualSourceURI;
  -                } else {
  -                    return destURI.substring(0, starPos)
  -                        + actualSourceURI
  -                        + destURI.substring(starPos + 1);
  -                }
  -            } else {
  -                throw new ProcessingException(
  -                    "Unknown mapper type: " + this.type);
  -            }
  -        }
  -
  -        public String getSourceURI() {
  -            return root + sourceURI;
  -        }
  -
  -        public ModifiableSource getSource(String filename)
  -            throws IOException, ProcessingException {
  -            final String finalDestinationURI = this.getFinalURI(filename);
  -            Source src = sourceResolver.resolveURI(finalDestinationURI);
  -            if (!(src instanceof ModifiableSource)) {
  -                sourceResolver.release(src);
  -                throw new ProcessingException(
  -                    "Source is not Modifiable: " + finalDestinationURI);
  -            }
  -            return (ModifiableSource) src;
  -        }
  -
  -        public long getLastModified(String filename) throws IOException, 
ProcessingException {
  -            return getSource(filename).getLastModified();
  -        }
  +    
  +     public ModifiableSource getSource(Target target, String filename)
  +             throws IOException, ProcessingException {
  +             final String finalDestinationURI = target.getFinalURI(filename);
  +             Source src = sourceResolver.resolveURI(finalDestinationURI);
  +             if (!(src instanceof ModifiableSource)) {
  +                     sourceResolver.release(src);
  +                     throw new ProcessingException(
  +                             "Source is not Modifiable: " + 
finalDestinationURI);
  +             }
  +             return (ModifiableSource) src;
  +     }
  +
  +     public long getLastModified(Target target, String filename) throws 
IOException, ProcessingException {
  +             return getSource(target, filename).getLastModified();
  +     }
           
  -        public void releaseSource(ModifiableSource source) {
  -            sourceResolver.release(source);
  -        }
  -
  -        public boolean equals(Object o) {
  -            return (o instanceof Target) && o.toString().equals(toString());
  -        }
  -
  -        public int hashCode() {
  -            if (_hashCode == 0) {
  -                return _hashCode = toString().hashCode();
  -            }
  -            return _hashCode;
  -        }
  -
  -        public String toString() {
  -            if (_toString == null) {
  -                return _toString =
  -                    "<"
  -                        + type
  -                        + "|"
  -                        + root
  -                        + "|"
  -                        + sourceURI
  -                        + "|"
  -                        + destURI
  -                        + ">";
  -            }
  -            return _toString;
  -        }
  -    }
  +     public void releaseSource(ModifiableSource source) {
  +             sourceResolver.release(source);
  +     }
  +    
   }
  
  
  
  1.2       +3 -3      
cocoon-2.1/src/java/org/apache/cocoon/bean/CocoonWrapper.java
  
  Index: CocoonWrapper.java
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.1/src/java/org/apache/cocoon/bean/CocoonWrapper.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- CocoonWrapper.java        17 Aug 2003 13:50:45 -0000      1.1
  +++ CocoonWrapper.java        27 Aug 2003 19:18:18 -0000      1.2
  @@ -124,11 +124,11 @@
       private Map attributes = new HashMap();
       private HashMap empty = new HashMap();
   
  -    private boolean initialized;
  +    private boolean initialized = false;
  +
       //
       // INITIALISATION METHOD
       //
  -
       public void initialize() throws Exception {
           // @todo@ when does the logger get initialised? uv
           // @todo@ these should log then throw exceptions back to the caller, 
not use system.exit()
  
  
  
  1.1                  cocoon-2.1/src/java/org/apache/cocoon/bean/Target.java
  
  Index: Target.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Apache Cocoon" and  "Apache Software Foundation" must  not  be
      used to  endorse or promote  products derived from  this software without
      prior written permission. For written permission, please contact
      [EMAIL PROTECTED]
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation and was  originally created by
   Stefano Mazzocchi  <[EMAIL PROTECTED]>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.cocoon.bean;
  
  import org.apache.cocoon.util.NetUtils;
  import org.apache.cocoon.ProcessingException;
  
  /**
   * A Target is a single page for generation. It encapsulates the URI 
   * arithmetic required to transform the URI of the page to be generated 
   * (the source URI) into the URI to which the resulting page should be 
   * written (the destination URI).
   *
   * @author <a href="mailto:[EMAIL PROTECTED]">Upayavira</a>
   * @version CVS $Id: Target.java,v 1.1 2003/08/27 19:18:18 upayavira Exp $
   */
  public class Target {
        // Defult type is append
        private static final String APPEND_TYPE = "append";
        private static final String REPLACE_TYPE = "replace";
        private static final String INSERT_TYPE = "insert";
  
        private final String type;
        private final String root;
        private final String sourceURI;
        private final String destURI;
  
        private transient int _hashCode;
        private transient String _toString;
  
        public Target(
                String type,
                String root,
                String sourceURI,
                String destURI)
                throws IllegalArgumentException {
                this.type = type;
                this.root = root;
                this.sourceURI = NetUtils.normalize(sourceURI);
                if (destURI == null || destURI.length() == 0) {
                        throw new IllegalArgumentException("You must specify a 
destination directory when defining a target");
                }
                if (!destURI.endsWith("/")) {
                        destURI += "/";
                }
                this.destURI = destURI;
        }
  
        public Target(String type, String sourceURI, String destURI)
                throws IllegalArgumentException {
                this(type, "", sourceURI, destURI);
        }
  
        public Target(String sourceURI, String destURI)
                throws IllegalArgumentException {
                this(APPEND_TYPE, "", sourceURI, destURI);
        }
  
        public Target getDerivedTarget(String newURI)
                throws IllegalArgumentException {
                if (!newURI.startsWith(root)) {
                        return null;
                }
                newURI = newURI.substring(root.length());
                return new Target(this.type, this.root, newURI, this.destURI);
        }
  
        public String getFinalURI(String actualSourceURI)
                throws ProcessingException {
                if (!actualSourceURI.startsWith(root)) {
                        throw new ProcessingException(
                                "Derived target does not share same root: "
                                        + actualSourceURI);
                }
                actualSourceURI = actualSourceURI.substring(root.length());
  
                if (APPEND_TYPE.equals(this.type)) {
                        return destURI + actualSourceURI;
                } else if (REPLACE_TYPE.equals(this.type)) {
                        return destURI;
                } else if (INSERT_TYPE.equals(this.type)) {
                        int starPos = destURI.indexOf("*");
                        if (starPos == -1) {
                                throw new ProcessingException("Missing * in 
replace mapper uri");
                        } else if (starPos == destURI.length() - 1) {
                                return destURI.substring(0, starPos) + 
actualSourceURI;
                        } else {
                                return destURI.substring(0, starPos)
                                        + actualSourceURI
                                        + destURI.substring(starPos + 1);
                        }
                } else {
                        throw new ProcessingException(
                                "Unknown mapper type: " + this.type);
                }
        }
  
        public String getSourceURI() {
                return root + sourceURI;
        }
  
        public boolean equals(Object o) {
                return (o instanceof Target) && o.toString().equals(toString());
        }
  
        public int hashCode() {
                if (_hashCode == 0) {
                        return _hashCode = toString().hashCode();
                }
                return _hashCode;
        }
  
        public String toString() {
                if (_toString == null) {
                        return _toString =
                                "<"
                                        + type
                                        + "|"
                                        + root
                                        + "|"
                                        + sourceURI
                                        + "|"
                                        + destURI
                                        + ">";
                }
                return _toString;
        }
  }
  
  

Reply via email to