upayavira    2003/09/13 03:20:09

  Modified:    src/java/org/apache/cocoon/bean CocoonBean.java Target.java
  Log:
  Moved source URI arithmetic to Target class
  Properly releasing source after lastModified check
  
  Revision  Changes    Path
  1.23      +50 -86    
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.22
  retrieving revision 1.23
  diff -u -r1.22 -r1.23
  --- CocoonBean.java   10 Sep 2003 19:20:38 -0000      1.22
  +++ CocoonBean.java   13 Sep 2003 10:20:09 -0000      1.23
  @@ -365,32 +365,14 @@
   
           int status = 0;
           
  -        String uri = target.getSourceURI();
           int linkCount = 0;
   
  -        // Get parameters, deparameterized URI and path from URI
  -        final TreeMap parameters = new TreeMap();
  -        final String deparameterizedURI =
  -            NetUtils.deparameterize(uri, parameters);
  -        final String path = NetUtils.getPath(uri);
  -        final String suri =
  -            NetUtils.parameterize(deparameterizedURI, parameters);
  -
  -        // Get file name from URI (without path)
  -        String pageURI = deparameterizedURI;
  -        if (pageURI.indexOf("/") != -1) {
  -            pageURI = pageURI.substring(pageURI.lastIndexOf("/") + 1);
  -            if (pageURI.length() == 0) {
  -                pageURI = "./";
  -            }
  -        }
  -
           String destinationURI;
           if (confirmExtension) {
  -            destinationURI = (String) allTranslatedLinks.get(suri);
  +            destinationURI = (String) 
allTranslatedLinks.get(target.getSourceURI());
               if (destinationURI == null) {
  -                destinationURI = mangle(suri);
  -                final String type = getType(deparameterizedURI, parameters);
  +                destinationURI = mangle(target.getSourceURI());
  +                final String type = 
getType(target.getDeparameterizedSourceURI(), target.getParameters());
                   final String ext = NetUtils.getExtension(destinationURI);
                   final String defaultExt = 
MIMEUtils.getDefaultExtension(type);
                   if (defaultExt != null) {
  @@ -398,10 +380,10 @@
                           destinationURI += defaultExt;
                       }
                   }
  -                allTranslatedLinks.put(suri, destinationURI);
  +                allTranslatedLinks.put(target.getSourceURI(), 
destinationURI);
               }
           } else {
  -            destinationURI = suri;
  +            destinationURI = target.getSourceURI();
           }
           // Store processed URI list to avoid eternal loop
           allProcessedLinks.put(target, target);
  @@ -416,60 +398,48 @@
           final List targets = new ArrayList();
           if (followLinks && confirmExtension) {
               final Iterator i =
  -                this.getLinks(deparameterizedURI, parameters).iterator();
  +                this.getLinks(target.getDeparameterizedSourceURI(), 
target.getParameters()).iterator();
   
               while (i.hasNext()) {
  -                String originalLinkSourceURI = (String) i.next();
  +                String linkURI = (String) i.next();
  +                Target linkTarget = target.getDerivedTarget(linkURI);
   
  -                // Fix relative links starting with "?"
  -                String linkSourceURI = originalLinkSourceURI;
  -                if (linkSourceURI.startsWith("?")) {
  -                    linkSourceURI = pageURI + linkSourceURI;
  +                if (linkTarget == null) {
  +                    System.out.println("Skipping "+ linkURI);
  +                    //@TODO@ Log/report skipped link
  +                    continue;
                   }
   
  -                linkSourceURI =
  -                    NetUtils.normalize(NetUtils.absolutize(path, 
linkSourceURI));
  -                
  -                if (!isIncluded(linkSourceURI)) {
  +                if (!isIncluded(linkTarget.getSourceURI())) {
                       //@TODO@ Log/report skipped link
                       continue;
                   }
   
  -                {
  -                    final TreeMap p = new TreeMap();
  -                    linkSourceURI =
  -                        NetUtils.parameterize(
  -                            NetUtils.deparameterize(linkSourceURI, p),
  -                            p);
  -                }
                   String linkDestinationURI =
  -                    (String) allTranslatedLinks.get(linkSourceURI);
  +                    (String) 
allTranslatedLinks.get(linkTarget.getSourceURI());
                   if (linkDestinationURI == null) {
                       try {
                           linkDestinationURI =
  -                            this.translateURI(linkSourceURI);
  -                        log.info("  Link translated: " + linkSourceURI);
  +                            this.translateURI(linkTarget.getSourceURI());
  +                        log.info("  Link translated: " + 
linkTarget.getSourceURI());
                           allTranslatedLinks.put(
  -                            linkSourceURI,
  +                            linkTarget.getSourceURI(),
                               linkDestinationURI);
                       } catch (ProcessingException pe) {
  -                        this.sendBrokenLinkWarning(linkSourceURI, 
pe.getMessage());
  +                        
this.sendBrokenLinkWarning(linkTarget.getSourceURI(), pe.getMessage());
                       }
                   }
   
                   // AllTranslatedLinks is for preventing retranslation
                   // translartedLinks is for use by the LinkTranslator
                   final String translatedRelativeLink =
  -                    NetUtils.relativize(path, linkDestinationURI);
  -                translatedLinks.put(originalLinkSourceURI, 
translatedRelativeLink);
  +                    NetUtils.relativize(target.getPath(), 
linkDestinationURI);
  +                translatedLinks.put(linkTarget.getOriginalSourceURI(), 
translatedRelativeLink);
   
                   // I have to add also broken links to the absolute links
                   // to be able to generate the "broken link" page
                   // @TODO@ Only do this if broken page generation is required
  -                Target derivedTarget = 
target.getDerivedTarget(linkSourceURI);
  -                if (derivedTarget != null) {
  -                    targets.add(derivedTarget);
  -                }
  +                targets.add(linkTarget);
               }
   
               linkCount = translatedLinks.size();
  @@ -481,9 +451,9 @@
               try {
                   status =
                       getPage(
  -                        deparameterizedURI,
  +                        target.getDeparameterizedSourceURI(),
                           getLastModified(target, destinationURI),
  -                        parameters,
  +                        target.getParameters(),
                           confirmExtension ? translatedLinks : null,
                           gatheredLinks,
                           output);
  @@ -495,36 +465,29 @@
   
                   if (followLinks && !confirmExtension) {
                       for (Iterator it = 
gatheredLinks.iterator();it.hasNext();) {
  -                        String linkSourceURI = (String) it.next();
  -                        if (linkSourceURI.startsWith("?")) {
  -                            linkSourceURI = pageURI + linkSourceURI;
  -                        }
  -                        linkSourceURI =
  -                            NetUtils.normalize(NetUtils.absolutize(path, 
linkSourceURI));
  -                        {
  -                            final TreeMap p = new TreeMap();
  -                            linkSourceURI =
  -                                NetUtils.parameterize(
  -                                    NetUtils.deparameterize(linkSourceURI, 
p),
  -                                    p);
  +                        String linkURI = (String) it.next();
  +                        Target linkTarget = target.getDerivedTarget(linkURI);
  +
  +                        if (linkTarget == null) {
  +                            System.out.println("Skipping "+ linkURI);
  +                            //@TODO@ Log/report skipped link
  +                            continue;
                           }
  -                        if (isIncluded(linkSourceURI)) {
  -                            Target derivedTarget = 
target.getDerivedTarget(linkSourceURI);
  -                            if (derivedTarget != null) { 
  -                                targets.add(derivedTarget);
  -                            }
  -                        } else {
  -                            // @TODO@ Log/report skipped link
  +
  +                        if (!isIncluded(linkTarget.getSourceURI())) {
  +                            //@TODO@ Log/report skipped link
  +                            continue;
                           }
  +                        targets.add(linkTarget);
                       }
                       linkCount = gatheredLinks.size();
                   }
   
  -                pageGenerated(uri, linkCount, 0); // @todo@ get the number 
of pages remaining here
  +                pageGenerated(target.getSourceURI(), linkCount, 0); // 
@todo@ get the number of pages remaining here
               } catch (ProcessingException pe) {
                   output.close();
                   output = null;
  -                this.resourceUnavailable(target, uri, destinationURI);
  +                this.resourceUnavailable(target, target.getSourceURI(), 
destinationURI);
                   this.sendBrokenLinkWarning(
                       destinationURI,
                       DefaultNotifyingBuilder.getRootCause(pe).getMessage());
  @@ -546,8 +509,8 @@
                   }
               }
           } catch (Exception rnfe) {
  -            log.warn("Could not process URI: " + deparameterizedURI);
  -            this.sendBrokenLinkWarning(deparameterizedURI, "URI not found");
  +            log.warn("Could not process URI: " + target.getSourceURI());
  +            this.sendBrokenLinkWarning(target.getSourceURI(), "URI not 
found");
           }
   
   /*  Commenting out timestamp - will reimplement properly using the 
BeanListener interface
  @@ -575,14 +538,12 @@
           String deparameterizedURI = NetUtils.deparameterize(uri, parameters);
   
           String destinationURI = mangle(uri);
  -        if (confirmExtension) {
  -            String type = getType(deparameterizedURI, parameters);
  -            String ext = NetUtils.getExtension(destinationURI);
  -            String defaultExt = MIMEUtils.getDefaultExtension(type);
  -            if (defaultExt != null) {
  -                if ((ext == null) || (!ext.equals(defaultExt))) {
  -                    destinationURI += defaultExt;
  -                }
  +        String type = getType(deparameterizedURI, parameters);
  +        String ext = NetUtils.getExtension(destinationURI);
  +        String defaultExt = MIMEUtils.getDefaultExtension(type);
  +        if (defaultExt != null) {
  +            if ((ext == null) || (!ext.equals(defaultExt))) {
  +                destinationURI += defaultExt;
               }
           }
   
  @@ -660,10 +621,13 @@
       }
   
       public long getLastModified(Target target, String destinationURI) throws 
IOException, ProcessingException {
  -        return getSource(target, destinationURI).getLastModified();
  +        Source src = getSource(target, destinationURI);
  +        long lastModified = src.getLastModified();
  +        this.releaseSource(src);
  +        return lastModified;
       }
           
  -    public void releaseSource(ModifiableSource source) {
  +    public void releaseSource(Source source) {
           sourceResolver.release(source);
       }
       private boolean isIncluded(String uri) {
  
  
  
  1.3       +208 -110  cocoon-2.1/src/java/org/apache/cocoon/bean/Target.java
  
  Index: Target.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/bean/Target.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Target.java       10 Sep 2003 09:12:21 -0000      1.2
  +++ Target.java       13 Sep 2003 10:20:09 -0000      1.3
  @@ -50,6 +50,8 @@
   */
   package org.apache.cocoon.bean;
   
  +import java.util.TreeMap;
  +
   import org.apache.cocoon.util.NetUtils;
   import org.apache.cocoon.ProcessingException;
   
  @@ -63,114 +65,210 @@
    * @version CVS $Id$
    */
   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 getDestinationURI(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;
  -     }
  +    // 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 final String deparameterizedSourceURI;
  +    private final TreeMap parameters;
  +    
  +    private String originalURI; 
  +    
  +    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;
  +        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;
  +        
  +        this.parameters = new TreeMap();
  +        sourceURI = NetUtils.normalize(root + sourceURI);
  +        this.deparameterizedSourceURI = NetUtils.deparameterize(sourceURI, 
this.parameters);
  +        this.sourceURI = 
NetUtils.parameterize(this.deparameterizedSourceURI, this.parameters);
  +    }
  +
  +    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 originalLinkURI)
  +        throws IllegalArgumentException {
  +
  +        String linkURI = originalLinkURI;
  +        // Fix relative links starting with "?"
  +        if (linkURI.startsWith("?")) {
  +            linkURI = this.getPageURI() + linkURI;
  +        }
  +        linkURI =
  +            NetUtils.normalize(NetUtils.absolutize(this.getPath(), linkURI));
  +
  +        // Ignore pages outside the root folder
  +        if (!linkURI.startsWith(this.root)) {
  +            return null;
  +        }
  +        linkURI = linkURI.substring(root.length());
  +        
  +        Target target = new Target(this.type, this.root, linkURI, 
this.destURI);
  +        target.setOriginalURI(originalLinkURI);
  +        return target;
  +    }
  +
  +    /**
  +     * Sets the original URI. This is used to record the URI that
  +     * caused the creation of this Target, for example as a link
  +     * in another page. It is needed for doing link translation, as
  +     * this is the URI that must be replaced by the translated one.
  +     */
  +    public void setOriginalURI(String uri) {
  +        this.originalURI = uri;
  +    }
  +    
  +    /**
  +     * Gets the filename from the source URI, without the path.
  +     * This is used to fill out relative URIs that have
  +     * parameters but no filename such as ?page=123
  +     */
  +    public String getPageURI() {
  +        String pageURI = this.getSourceURI();
  +        if (pageURI.indexOf("/") != -1) {
  +            pageURI = pageURI.substring(pageURI.lastIndexOf("/") + 1);
  +            if (pageURI.length() == 0) {
  +                pageURI = "./";
  +            }
  +        }
  +        return pageURI;
  +    }
  +
  +    /**
  +     * Gets the path from the source URI, without the filename. 
  +     * This is used when absolutizing/relativizing link URIs.
  +     */
  +    public String getPath() {
  +        return NetUtils.getPath(this.getSourceURI());
  +    }
  +
  +    /**
  +     * Calculates the destination URI - the URI to which the generated
  +     * page should be written. This will be a URI that, when resolved
  +     * by a SourceResolver, will return a modifiableSource.
  +     */
  +    public String getDestinationURI(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);
  +        }
  +    }
  +
  +    /**
  +     * Gets the original URI used to create this Target.
  +     * This URI is completely unprocessed.
  +     */
  +    public String getOriginalSourceURI() {
  +        return this.originalURI;
  +    }
  +
  +    /**
  +     * Gets the source URI for this target, after
  +     * the URI has been 'prepared' by normalisation,
  +     * absolutization and deparameterization followed
  +     * by reparameterization. This final step is to 
  +     * ensure that all parameters appear in a consistent
  +     * order. For example page?a=1&b=2 and page?b=2&a=1
  +     * should be considered the same resource, and thus
  +     * have the same sourceURI. 
  +     */
  +    public String getSourceURI() {
  +        return this.sourceURI;
  +    }
  +    /**
  +     * Gets the source URI for this target, with 
  +     * parameters removed. This is the URI that is 
  +     * to be passed to Cocoon in order to generate 
  +     * the page.
  +     */
  +    public String getDeparameterizedSourceURI() {
  +        return this.deparameterizedSourceURI;
  +    }
  +
  +    /**
  +     * Gets the parameters that have been removed from
  +     * the URI. These need to be passed to Cocoon when
  +     * generating a page.
  +     */
  +    public TreeMap getParameters() {
  +        return this.parameters;
  +    }
  +
  +    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