View Javadoc

1   /*
2   Wotonomy: OpenStep design patterns for pure Java applications.
3   Copyright (C) 2000 Michael Powers
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, see http://www.gnu.org
17  */
18  
19  package net.wotonomy.datastore;
20  
21  import java.io.File;
22  import java.io.FileInputStream;
23  import java.io.FileOutputStream;
24  import java.io.IOException;
25  import java.net.MalformedURLException;
26  
27  import net.wotonomy.web.xml.XMLRPCDecoder;
28  import net.wotonomy.web.xml.XMLRPCEncoder;
29  
30  public class XMLFileSoup extends FileSoup
31  {
32      public XMLFileSoup( String aPath )
33      {
34          super( aPath );
35      }
36  
37      // file access methods    
38      
39      protected boolean writeFile( String name, Object anObject )
40      {
41  //System.out.print( "writeFile: " + name + "..." );        
42          try
43          {
44              File f = new File( getHomeDirectory(), name );
45              FileOutputStream fos = new FileOutputStream( f );
46              XMLRPCEncoder encoder = new XMLRPCEncoder();
47              encoder.encode( anObject, fos );
48              fos.flush();
49              fos.close();
50          } 
51          catch ( Exception exc )
52          {
53              System.err.println( "XMLFileSoup.writeFile: " + exc );
54              return false;
55          }
56  //System.out.println( "done." );        
57          return true;
58      }
59      
60      protected Object readFile( String name )
61      {
62  //System.out.print( "readFile: " + name + "..." );        
63      	Object result = null;
64  
65          try
66          {
67              File f = new File( getHomeDirectory(), name );
68              FileInputStream fis = new FileInputStream( f );
69              XMLRPCDecoder decoder = new XMLRPCDecoder();
70              result = decoder.decode( fis, f.getAbsolutePath(), f.toURL() );
71              fis.close();
72          }
73  		catch ( MalformedURLException exc )
74  		{
75  			result = null;
76  		}
77  		catch ( IOException exc )
78  		{
79  			result = null;
80  		}
81  //System.out.println( "done." );        
82          return result;
83      }
84      
85      protected boolean deleteFile( String name )
86      {
87      	try
88          {
89              File f = new File( getHomeDirectory(), name );
90              if ( f.exists() )
91              {
92                  f.delete();
93  				return true;
94              }
95          }
96          catch ( Exception exc )
97          {
98              System.err.println( "XMLFileSoup.deleteFile: " + exc );
99          }
100 
101         return false;
102     }
103     
104 }
105 
106 /*
107  * $Log$
108  * Revision 1.2  2006/02/19 16:26:19  cgruber
109  * Move non-unit-test code to tests project
110  * Fix up code to work with proper imports
111  * Fix maven dependencies.
112  *
113  * Revision 1.1  2006/02/16 13:18:56  cgruber
114  * Check in all sources in eclipse-friendly maven-enabled packages.
115  *
116  * Revision 1.3  2003/08/14 19:29:38  chochos
117  * minor cleanup (imports, static method calls, etc)
118  *
119  * Revision 1.2  2001/02/07 19:26:28  mpowers
120  * XML classes are in new package.
121  *
122  * Revision 1.1.1.1  2000/12/21 15:47:23  mpowers
123  * Contributing wotonomy.
124  *
125  * Revision 1.6  2000/12/20 16:25:37  michael
126  * Added log to all files.
127  *
128  *
129  */
130