KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > xml > querying > impl > xtas > resource > plugins > LocalFile


1 package org.exoplatform.services.xml.querying.impl.xtas.resource.plugins;
2
3 import java.io.FileOutputStream JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.io.File JavaDoc;
6 import java.io.IOException JavaDoc;
7 import org.exoplatform.services.xml.querying.UniFormTransformationException;
8 import org.exoplatform.services.xml.querying.XMLWellFormedData;
9 import org.exoplatform.services.xml.querying.impl.xtas.WellFormedUniFormTree;
10 import org.exoplatform.services.xml.querying.impl.xtas.resource.Resource;
11 import org.xml.sax.InputSource JavaDoc;
12
13 /**
14  * Represents XML local file as XTAS resource
15  * @version $Id: LocalFile.java 566 2005-01-25 12:50:49Z kravchuk $
16  */

17 public class LocalFile extends Resource {
18     /**
19      * Creates resource as local file named <code> resourceId </code>
20      */

21     public LocalFile(String JavaDoc resourceId)
22     {
23        this.resourceId = resourceId;
24     }
25
26     public LocalFile()
27     {
28     }
29
30     public void setContext(Object JavaDoc context)
31     {
32     }
33
34     /**
35      * Loads XML from the local file
36      * Will not validate...
37      */

38     public XMLWellFormedData load() throws UniFormTransformationException, IOException JavaDoc
39     {
40
41         File JavaDoc file = new File JavaDoc(resourceId);
42
43         try {
44
45             WellFormedUniFormTree tree = new WellFormedUniFormTree ();
46             tree.init ( new InputSource JavaDoc (file.getCanonicalPath()) );
47
48             return tree;
49
50         } catch (Exception JavaDoc e) {
51
52             throw new UniFormTransformationException("Can not create WellFormedUniFormTree (XML) Reason: " + e);
53         }
54     }
55
56     public void save(XMLWellFormedData tree) throws IOException JavaDoc
57     {
58
59
60         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc( resourceId );
61
62         byte[] b = tree.getAsByteArray();
63
64         fos.write( b, 0, b.length );
65
66         fos.flush();
67         fos.close();
68
69     }
70
71     /**
72      * Creates resource by Id
73      */

74     public void create(XMLWellFormedData initTree) throws IOException JavaDoc
75     {
76
77         File JavaDoc res = new File JavaDoc (resourceId);
78         if( !res.createNewFile() )
79             throw new IOException JavaDoc (" File '"+res.getAbsolutePath()+"' already exists.");
80
81 // save( initTree );
82

83     }
84
85     /**
86      * drops resource
87      */

88     public void drop() throws IOException JavaDoc
89     {
90         File JavaDoc res = new File JavaDoc (resourceId);
91         if( !res.delete() )
92             throw new IOException JavaDoc (" File '"+res.getAbsolutePath()+"' can not be deleted.");
93     }
94
95
96 }
97
Popular Tags