KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > source > SourceDTO


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.cocoon.components.source;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 import org.apache.cocoon.components.source.helpers.SourceLock;
27 import org.apache.cocoon.components.source.helpers.SourceProperty;
28 import org.apache.excalibur.source.ModifiableTraversableSource;
29 import org.apache.excalibur.source.MoveableSource;
30 import org.apache.excalibur.source.Source;
31 import org.apache.excalibur.source.SourceException;
32 import org.apache.excalibur.source.SourceValidity;
33 import org.apache.excalibur.source.TraversableSource;
34
35 /**
36  * Data transfer object for a Source object.
37  *
38  * @version CVS $Id: SourceDTO.java 30932 2004-07-29 17:35:38Z vgritsenko $
39  */

40 public class SourceDTO implements Source, ModifiableTraversableSource,
41            MoveableSource, LockableSource, InspectableSource,
42            VersionableSource {
43
44     private String JavaDoc uri;
45     private String JavaDoc scheme;
46     private SourceValidity validity;
47     private String JavaDoc mimetype;
48     private boolean exists;
49     private long contentlength;
50     private long lastmodified;
51     private ArrayList JavaDoc children = new ArrayList JavaDoc();
52     private String JavaDoc name;
53     private SourceDTO parent;
54     private boolean iscollection;
55     private SourceProperty[] properties;
56     private boolean isversioned;
57     private String JavaDoc revision;
58     private String JavaDoc revisionbranch;
59     private String JavaDoc lastrevision;
60
61     /**
62      * Create a data transfer object for a Source.
63      *
64      * @param source Source
65      */

66     public SourceDTO(Source source) {
67         this(source, true);
68     }
69
70     /**
71      * Create a data transfer object for a Source.
72      *
73      * @param source Source
74      */

75     public SourceDTO(Source source, boolean deep) {
76         uri = source.getURI();
77         scheme = source.getScheme();
78         exists = source.exists();
79         if (exists) {
80             validity = source.getValidity();
81             mimetype = source.getMimeType();
82             contentlength = source.getContentLength();
83             lastmodified = source.getLastModified();
84
85             if (source instanceof TraversableSource) {
86                 TraversableSource traversablesource = (TraversableSource) source;
87   
88                 iscollection = traversablesource.isCollection();
89
90                 name = traversablesource.getName();
91
92                 try {
93                     if (iscollection && deep)
94                         for(Iterator JavaDoc i = traversablesource.getChildren().iterator(); i.hasNext(); )
95                             children.add(new SourceDTO((Source)i.next(), false));
96                 } catch (SourceException se) {}
97
98                 try {
99                     if (deep && (traversablesource.getParent()!=null))
100                         parent = new SourceDTO(traversablesource.getParent(), false);
101                 } catch (SourceException se) {}
102             }
103
104             if (source instanceof InspectableSource) {
105                 InspectableSource inspectablesource = (InspectableSource) source;
106
107                 try {
108                     properties = inspectablesource.getSourceProperties();
109                 } catch (SourceException se) {}
110             }
111
112             if (source instanceof VersionableSource) {
113                 VersionableSource versionablesource = (VersionableSource) source;
114
115                 try {
116                     isversioned = versionablesource.isVersioned();
117     
118                     if (isversioned) {
119                         revision = versionablesource.getSourceRevision();
120                         revisionbranch = versionablesource.getSourceRevisionBranch();
121                         lastrevision = versionablesource.getLatestSourceRevision();
122                     }
123                 } catch (SourceException se) {}
124             }
125         }
126     }
127     
128     /**
129      * Return an <code>InputStream</code> object to read from the source.
130      * This is the data at the point of invocation of this method,
131      * so if this is Modifiable, you might get different content
132      * from two different invocations.
133      *
134      * @return Input stream for the source.
135      *
136      * @throws IOException If an IO excepetion occurs.
137      * @throws SourceException If an exception occurs.
138      */

139     public InputStream JavaDoc getInputStream() throws IOException JavaDoc, SourceException {
140         throw new IllegalStateException JavaDoc("Data transfer object does not support this operation");
141     }
142
143     /**
144      * Return the unique identifer for this source
145      *
146      * @return System identifier for the source.
147      */

148     public String JavaDoc getURI() {
149         return uri;
150     }
151
152     /**
153      * @see org.apache.excalibur.source.Source#getScheme()
154      *
155      * @return Scheme of the source.
156      */

157     public String JavaDoc getScheme() {
158         return scheme;
159     }
160
161     /**
162      * Return the authority of a URI. This authority is
163      * typically defined by an Internet-based server or a scheme-specific
164      * registry of naming authorities
165      * (see <a HREF="http://www.ietf.org/rfc/rfc2396.txt">RFC 2396</a>).
166      *
167      * @return Scheme of the URI.
168      */

169     public String JavaDoc getAuthority() {
170         return SourceUtil.getAuthority(uri);
171     }
172
173     /**
174      * Return the path of a URI. The path contains data, specific to the
175      * authority (or the scheme if there is no authority component),
176      * identifying the resource within the scope of that scheme and authority
177      * (see <a HREF="http://www.ietf.org/rfc/rfc2396.txt">RFC 2396</a>).
178      *
179      * @return Path of the URI.
180      */

181     public String JavaDoc getPath() {
182         return SourceUtil.getPath(uri);
183     }
184
185     /**
186      * Return the query of a URI. The query is a string of information to
187      * be interpreted by the resource
188      * (see <a HREF="http://www.ietf.org/rfc/rfc2396.txt">RFC 2396</a>).
189      *
190      * @return Query of the URI.
191      */

192     public String JavaDoc getQuery() {
193         return SourceUtil.getQuery(uri);
194     }
195
196     /**
197      * Return the fragment of a URI. When a URI reference is used to perform
198      * a retrieval action on the identified resource, the optional fragment
199      * identifier, consists of additional reference information to be
200      * interpreted by the user agent after the retrieval action has been
201      * successfully completed
202      * (see <a HREF="http://www.ietf.org/rfc/rfc2396.txt">RFC 2396</a>).
203      *
204      * @return Fragment of the URI.
205      */

206     public String JavaDoc getFragment() {
207         return SourceUtil.getFragment(uri);
208     }
209
210     /**
211      * Get the Validity object. This can either wrap the last modification
212      * date or the expires information or...
213      * If it is currently not possible to calculate such an information
214      * <code>null</code> is returned.
215      *
216      * @return Validity for the source.
217      */

218     public SourceValidity getValidity() {
219         return validity;
220     }
221
222     /**
223      * Refresh the content of this object after the underlying data
224      * content has changed.
225      */

226     public void refresh() {
227         throw new IllegalStateException JavaDoc("Data transfer object does not support this operation");
228     }
229
230     /**
231      * The mime-type of the content described by this object.
232      * If the source is not able to determine the mime-type by itself
233      * this can be null.
234      *
235      * @return Mime type of the source.
236      */

237     public String JavaDoc getMimeType() {
238         return mimetype;
239     }
240
241     /**
242      * Does this source actually exist ?
243      *
244      * @return true if the resource exists.
245      */

246     public boolean exists() {
247         return exists;
248     }
249
250     /**
251      * Return the content length of the content or -1 if the length is
252      * unknown.
253      *
254      * @return Content length of the source.
255      */

256     public long getContentLength() {
257         return contentlength;
258     }
259
260     /**
261      * Get the last modification date of the source or 0 if it
262      * is not possible to determine the date.
263      *
264      * @return Last modified date of the source.
265      */

266     public long getLastModified() {
267         return lastmodified;
268     }
269     
270     // ---------------------------------------------------- ModifiableTraversableSource
271

272     /**
273      * Get an <code>OutputStream</code> where raw bytes can be written to.
274      * The signification of these bytes is implementation-dependent and
275      * is not restricted to a serialized XML document.
276      *
277      * @return a stream to write to
278      *
279      * @throws IOException
280      * @throws SourceException
281      */

282     public OutputStream JavaDoc getOutputStream()
283       throws IOException JavaDoc, SourceException {
284         throw new IllegalStateException JavaDoc("Data transfer object does not support this operation");
285     }
286
287     /**
288      * Can the data sent to an <code>OutputStream</code> returned by
289      * {@link #getOutputStream()} be cancelled ?
290      *
291      * @param stream The ouput stream, which should be cancelled.
292      * @return true if the stream can be cancelled
293      */

294     public boolean canCancel(OutputStream JavaDoc stream) {
295         throw new IllegalStateException JavaDoc("Data transfer object does not support this operation");
296     }
297
298     /**
299      * Cancel the data sent to an <code>OutputStream</code> returned by
300      * {@link #getOutputStream()}.
301      * <p>
302      * After cancel, the stream should no more be used.
303      *
304      * @param stream The ouput stream, which should be cancelled.
305      *
306      * @throws SourceException If the ouput stream can't be cancelled.
307      */

308     public void cancel(OutputStream JavaDoc stream) throws SourceException {
309         throw new IllegalStateException JavaDoc("Data transfer object does not support this operation");
310     }
311     
312     /**
313      * Delete the source.
314      */

315     public void delete() {
316         throw new IllegalStateException JavaDoc("Data transfer object does not support this operation");
317     }
318     
319     public void makeCollection() throws SourceException {
320         throw new IllegalStateException JavaDoc("Data transfer object does not support this operation");
321     }
322     
323     public Source getChild(String JavaDoc name) throws SourceException {
324         throw new IllegalStateException JavaDoc("Data transfer object does not support this operation");
325     }
326     
327     public Collection JavaDoc getChildren() throws SourceException {
328         return children;
329     }
330     
331     public String JavaDoc getName() {
332         return name;
333     }
334     
335     public Source getParent() throws SourceException {
336         return parent;
337
338     }
339     
340     public boolean isCollection() {
341         //System.out.println("uri="+uri+" isCollection="+iscollection);
342
return iscollection;
343     }
344     
345     // ---------------------------------------------------- MoveableSource
346

347     /**
348      * Move the current source to a specified destination.
349      *
350      * @param source
351      *
352      * @throws SourceException If an exception occurs during the move.
353      */

354     public void moveTo(Source source) throws SourceException {
355         throw new IllegalStateException JavaDoc("Data transfer object does not support this operation");
356     }
357
358     /**
359      * Copy the current source to a specified destination.
360      *
361      * @param source
362      *
363      * @throws SourceException If an exception occurs during the copy.
364      */

365     public void copyTo(Source source) throws SourceException {
366         throw new IllegalStateException JavaDoc("Data transfer object does not support this operation");
367     }
368
369     // ---------------------------------------------------- InspectableSource
370

371     /**
372      * Returns a property from a source.
373      *
374      * @param namespace Namespace of the property
375      * @param name Name of the property
376      *
377      * @return Property of the source.
378      *
379      * @throws SourceException If an exception occurs.
380      */

381     public SourceProperty getSourceProperty(String JavaDoc namespace, String JavaDoc name)
382         throws SourceException {
383         for (int i = 0; i<properties.length; i++)
384             if (properties[i].getNamespace().equals(namespace) &&
385                 properties[i].getName().equals(name))
386                 return properties[i];
387         return null;
388     }
389     
390     /**
391      * Sets a property for a source.
392      *
393      * @param property Property of the source
394      *
395      * @throws SourceException If an exception occurs during this operation
396      */

397     public void setSourceProperty(SourceProperty property)
398           throws SourceException {
399         throw new IllegalStateException JavaDoc("Data transfer object does not support this operation");
400     }
401
402     /**
403      * Returns a enumeration of the properties
404      *
405      * @return Enumeration of SourceProperty
406      *
407      * @throws SourceException If an exception occurs.
408      */

409     public SourceProperty[] getSourceProperties() throws SourceException {
410         //System.out.println("getProperties()");
411
//for(int i=0; i<properties.length; i++)
412
// System.out.println(i+". namespace="+properties[i].getNamespace()+" name="+properties[i].getName()+" value="+properties[i].getValue());
413
return properties;
414     }
415
416     /**
417      * Remove a specified source property.
418      *
419      * @param namespace Namespace of the property.
420      * @param name Name of the property.
421      *
422      * @throws SourceException If an exception occurs.
423      */

424     public void removeSourceProperty(String JavaDoc namespace, String JavaDoc name) throws SourceException {
425         throw new IllegalStateException JavaDoc("Data transfer object does not support this operation");
426     }
427     
428     // ---------------------------------------------------- LockableSource
429

430     /**
431      * Add a lock to this source
432      *
433      * @param sourcelock Lock, which should be added
434      *
435      * @throws SourceException If an exception occurs during this operation
436      */

437     public void addSourceLocks(SourceLock sourcelock) throws SourceException {
438         throw new IllegalStateException JavaDoc("Data transfer object does not support this operation");
439     }
440
441     /**
442      * Returns a enumeration of the existing locks
443      *
444      * @return Enumeration of SourceLock
445      *
446      * @throws SourceException If an exception occurs.
447      */

448     public SourceLock[] getSourceLocks() throws SourceException {
449         throw new IllegalStateException JavaDoc("Data transfer object does not support this operation");
450     }
451
452     // ---------------------------------------------------- VersionableSource
453

454     /**
455      * If this source versioned
456      *
457      * @return True if the current source is versioned.
458      *
459      * @throws SourceException If an exception occurs.
460      */

461     public boolean isVersioned() throws SourceException {
462         return isversioned;
463     }
464
465     /**
466      * Get the current revision of the source
467      *
468      * @return The current revision of the source
469      *
470      */

471     public String JavaDoc getSourceRevision() {
472         return revision;
473     }
474
475     /**
476      * Not implemented.
477      *
478      * @param revision The revision, which should be used.
479      *
480      * @throws SourceException If an exception occurs.
481      */

482     public void setSourceRevision(String JavaDoc revision) throws SourceException {
483         throw new IllegalStateException JavaDoc("Data transfer object does not support this operation");
484     }
485
486     /**
487      * Get the current branch of the revision from the source
488      *
489      * @return The branch of the revision
490      *
491      * @throws SourceException If an exception occurs.
492      */

493     public String JavaDoc getSourceRevisionBranch() throws SourceException {
494         return revisionbranch;
495     }
496
497     /**
498      * Not implemented.
499      *
500      * @param branch The branch, which should be used.
501      *
502      * @throws SourceException If an exception occurs.
503      */

504     public void setSourceRevisionBranch(String JavaDoc branch) throws SourceException {
505         throw new IllegalStateException JavaDoc("Data transfer object does not support this operation");
506     }
507
508     /**
509      * Get the latest revision
510      *
511      * @return Last revision of the source.
512      *
513      * @throws SourceException If an exception occurs.
514      */

515     public String JavaDoc getLatestSourceRevision() throws SourceException {
516         return lastrevision;
517     }
518
519 }
520
521
Popular Tags