You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by sy...@apache.org on 2002/02/23 00:26:31 UTC

cvs commit: xml-cocoon2/src/scratchpad/src/org/apache/cocoon/treeprocessor MapStackResolver.java

sylvain     02/02/22 15:26:31

  Modified:    src/scratchpad/src/org/apache/cocoon/transformation
                        FileWritingTransformer.java
               src/scratchpad/src/org/apache/cocoon/treeprocessor
                        MapStackResolver.java
  Log:
  Update FileWritingTransformer to use WriteableSource
  
  Revision  Changes    Path
  1.7       +101 -73   xml-cocoon2/src/scratchpad/src/org/apache/cocoon/transformation/FileWritingTransformer.java
  
  Index: FileWritingTransformer.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/src/scratchpad/src/org/apache/cocoon/transformation/FileWritingTransformer.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- FileWritingTransformer.java	22 Feb 2002 21:49:54 -0000	1.6
  +++ FileWritingTransformer.java	22 Feb 2002 23:26:31 -0000	1.7
  @@ -64,6 +64,7 @@
   import org.apache.cocoon.caching.Cacheable;
   import org.apache.cocoon.environment.Source;
   import org.apache.cocoon.environment.SourceResolver;
  +import org.apache.cocoon.environment.WriteableSource;
   import org.apache.cocoon.ProcessingException;
   import org.apache.cocoon.ResourceNotFoundException;
   import org.xml.sax.Attributes;
  @@ -71,8 +72,7 @@
   import org.xml.sax.helpers.AttributesImpl;
   import java.lang.SecurityException;
   import java.io.IOException;
  -import java.io.File;
  -import java.io.FileOutputStream;
  +import java.io.OutputStream;
   import java.util.Map;
   import java.util.Properties;
   
  @@ -160,7 +160,7 @@
       private String target = null;
   
       /** Current FileOutputStream. */
  -    private FileOutputStream fos = null;
  +    private OutputStream fos = null;
   
       /** Current status of outputting the file. */
       private boolean failed = true;
  @@ -172,10 +172,10 @@
       private String action = null;
   
       /** Target file's final destination. */
  -    private File target_file = null;
  +//    private File target_file = null;
   
       /** Temporary target file. */
  -    private File temp_target_file = null;
  +//    private File temp_target_file = null;
   
   
       /** True when inside <write> element. */
  @@ -526,32 +526,40 @@
        * cloase the file output stream
        */
       private void closeFos() {
  -			try {
  -				this.fos.close();
  -				this.fos = null;
  -				if (!this.failed) {
  -					if (target_file.exists()) {
  -						target_file.delete();
  -						this.action = FRT_ACTION_OVERWRITE;
  -					} else {
  -						this.action = FRT_ACTION_NEW;
  -					}
  -					temp_target_file.renameTo(target_file);
  -				} else {
  -					if (temp_target_file.exists()) {
  -						temp_target_file.delete();
  -					}
  -				}
  -			} catch (Exception e) {
  -				getLogger().error("FileWritingTransformer failed, could not close the file", e);
  -				this.failed = true;
  -				this.message = "could not close the file";
  -				try {
  -					temp_target_file.delete();
  -				} catch (SecurityException se) {
  -					getLogger().error("FileWritingTransformer failed, could not delete the temp file: " + temp_target_file.toString(), e);
  -				}
  -			}
  +        try {
  +            this.fos.close();
  +            this.fos = null;
  +        } catch(Exception e) {
  +            getLogger().warn("Failed to close source", e);
  +            this.message = "Failed to close source";
  +            this.failed = true;
  +        }
  +//			try {
  +//				this.fos.close();
  +//				this.fos = null;
  +//				if (!this.failed) {
  +//					if (target_file.exists()) {
  +//						target_file.delete();
  +//						this.action = FRT_ACTION_OVERWRITE;
  +//					} else {
  +//						this.action = FRT_ACTION_NEW;
  +//					}
  +//					temp_target_file.renameTo(target_file);
  +//				} else {
  +//					if (temp_target_file.exists()) {
  +//						temp_target_file.delete();
  +//					}
  +//				}
  +//			} catch (Exception e) {
  +//				getLogger().error("FileWritingTransformer failed, could not close the file", e);
  +//				this.failed = true;
  +//				this.message = "could not close the file";
  +//				try {
  +//					temp_target_file.delete();
  +//				} catch (SecurityException se) {
  +//					getLogger().error("FileWritingTransformer failed, could not delete the temp file: " + temp_target_file.toString(), e);
  +//				}
  +//			}
   		}
   
       /**
  @@ -564,49 +572,69 @@
        * by not overwriting the temporary file.
        */
       private void openFos() {
  +        
  +        Source source = null;
  +        try {
  +            this.message = "The src attribute could not be resolved";
  +            source = this.sourceResolver.resolve(this.target);
  +            
  +            this.message = "The src attribute doesn't resolve to a writeable source";
  +            WriteableSource wsource = (WriteableSource)source;
  +            
  +            this.message = "Could not open the source for writing";
  +            this.fos = wsource.getOutputStream();
  +            
  +        } catch(Exception e) {
  +            getLogger().warn(this.message, e);
  +            this.failed = true;
  +        } finally {
  +            if (source != null) {
  +                source.recycle();
  +            }
  +        }
   
  -			// open the file
  -			if (!this.target.startsWith(FRT_PROTOCOL)) {
  -				getLogger().error("FileWritingTransformer failed, the src parameter  did not resolve to a file:");
  -				this.failed = true;
  -				this.message = "the src parameter did not resolve to a file";
  -			} else {
  -				try {
  -					target_file = new File (this.target.substring(5));
  -					temp_target_file = new File (this.target.substring(5) + ".tmp");
  -					if (!target_file.exists()) {
  -						File dir = target_file.getParentFile();
  -						if (!dir.exists() && dir.mkdirs() == true) {
  -								getLogger().warn("FileWritingTransformer: made new directories: " + dir.toString());
  -						}
  -					} else if (target_file.isDirectory()) {
  -						getLogger().error("FileWritingTransformer failed, the src parameter cannot point to a directory");
  -						this.failed = true;
  -						this.message = "the src parameter pointed to a directory";
  -					}				
  -					if (!this.failed) {
  -						if (temp_target_file.createNewFile() == true) {
  -							this.fos = new java.io.FileOutputStream(temp_target_file);
  -						} else {
  -							getLogger().error("FileWritingTransformer failed, the file was busy");
  -							this.failed = true;
  -							this.message = "the file was busy";
  -						}
  -					}
  -				} catch (SecurityException se) {
  -					getLogger().error("FileWritingTransformer failed, did not have the required file permissions for writing", se);
  -					this.failed = true;
  -					this.message = "could not open the file for writing";
  -				} catch (IOException ioe) {
  -					getLogger().error("FileWritingTransformer failed, could not open the file for writing", ioe);
  -					this.failed = true;
  -					this.message = "could not open the file for writing";
  -				} catch (NullPointerException npe) {
  -					getLogger().error("FileWritingTransformer failed, could not resolve the target", npe);
  -					this.failed = true;
  -					this.message = "could not resolve the target";
  -				}
  -			}
  +//			// open the file
  +//			if (!this.target.startsWith(FRT_PROTOCOL)) {
  +//				getLogger().error("FileWritingTransformer failed, the src parameter  did not resolve to a file:");
  +//				this.failed = true;
  +//				this.message = "the src parameter did not resolve to a file";
  +//			} else {
  +//				try {
  +//					target_file = new File (this.target.substring(5));
  +//					temp_target_file = new File (this.target.substring(5) + ".tmp");
  +//					if (!target_file.exists()) {
  +//						File dir = target_file.getParentFile();
  +//						if (!dir.exists() && dir.mkdirs() == true) {
  +//								getLogger().warn("FileWritingTransformer: made new directories: " + dir.toString());
  +//						}
  +//					} else if (target_file.isDirectory()) {
  +//						getLogger().error("FileWritingTransformer failed, the src parameter cannot point to a directory");
  +//						this.failed = true;
  +//						this.message = "the src parameter pointed to a directory";
  +//					}				
  +//					if (!this.failed) {
  +//						if (temp_target_file.createNewFile() == true) {
  +//							this.fos = new java.io.FileOutputStream(temp_target_file);
  +//						} else {
  +//							getLogger().error("FileWritingTransformer failed, the file was busy");
  +//							this.failed = true;
  +//							this.message = "the file was busy";
  +//						}
  +//					}
  +//				} catch (SecurityException se) {
  +//					getLogger().error("FileWritingTransformer failed, did not have the required file permissions for writing", se);
  +//					this.failed = true;
  +//					this.message = "could not open the file for writing";
  +//				} catch (IOException ioe) {
  +//					getLogger().error("FileWritingTransformer failed, could not open the file for writing", ioe);
  +//					this.failed = true;
  +//					this.message = "could not open the file for writing";
  +//				} catch (NullPointerException npe) {
  +//					getLogger().error("FileWritingTransformer failed, could not resolve the target", npe);
  +//					this.failed = true;
  +//					this.message = "could not resolve the target";
  +//				}
  +//			}
       }
   
   }
  
  
  
  1.11      +3 -3      xml-cocoon2/src/scratchpad/src/org/apache/cocoon/treeprocessor/MapStackResolver.java
  
  Index: MapStackResolver.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/src/scratchpad/src/org/apache/cocoon/treeprocessor/MapStackResolver.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- MapStackResolver.java	22 Feb 2002 06:58:03 -0000	1.10
  +++ MapStackResolver.java	22 Feb 2002 23:26:31 -0000	1.11
  @@ -60,7 +60,7 @@
    * Utility class for handling {...} pattern substitutions from a List of Maps.
    *
    * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
  - * @version CVS $Id: MapStackResolver.java,v 1.10 2002/02/22 06:58:03 cziegeler Exp $
  + * @version CVS $Id: MapStackResolver.java,v 1.11 2002/02/22 23:26:31 sylvain Exp $
    */
   
   public abstract class MapStackResolver {
  @@ -251,7 +251,7 @@
   
                   if (pos >= length || pos == -1) {
                       // no more braces
  -                    if (prev < length - 1) {
  +                    if (prev < length) {
                           stringList.add(this.unescape(expr.substring(prev)));
                           levelList.add(new Integer(-1));
                       }
  @@ -331,7 +331,7 @@
   //    public static void main(String [] args) throws Exception {
   //
   //        new CompiledResolver("&{../../blah}").dump();
  -//        new CompiledResolver("{t1}tt{t2}").dump();
  +//        new CompiledResolver("{t1}tt{t2}x").dump();
   //        new CompiledResolver("\\{t1}tt{t2}xx").dump();
   //        new CompiledResolver("{t1}tt\\{t2}xx").dump();
   //        new CompiledResolver("{t1}tt{t2}xx").dump();
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     webmaster@xml.apache.org
To unsubscribe, e-mail:          cocoon-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: cocoon-cvs-help@xml.apache.org