001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.cocoon.components.source;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.io.OutputStream;
023: import java.util.ArrayList;
024: import java.util.Collection;
025: import java.util.Iterator;
026:
027: import org.apache.cocoon.components.source.helpers.SourceLock;
028: import org.apache.cocoon.components.source.helpers.SourceProperty;
029: import org.apache.excalibur.source.ModifiableTraversableSource;
030: import org.apache.excalibur.source.MoveableSource;
031: import org.apache.excalibur.source.Source;
032: import org.apache.excalibur.source.SourceException;
033: import org.apache.excalibur.source.SourceValidity;
034: import org.apache.excalibur.source.TraversableSource;
035:
036: /**
037: * Data transfer object for a Source object.
038: *
039: * @version CVS $Id: SourceDTO.java 433543 2006-08-22 06:22:54Z crossley $
040: */
041: public class SourceDTO implements Source, ModifiableTraversableSource,
042: MoveableSource, LockableSource, InspectableSource,
043: VersionableSource {
044:
045: private String uri;
046: private String scheme;
047: private SourceValidity validity;
048: private String mimetype;
049: private boolean exists;
050: private long contentlength;
051: private long lastmodified;
052: private ArrayList children = new ArrayList();
053: private String name;
054: private SourceDTO parent;
055: private boolean iscollection;
056: private SourceProperty[] properties;
057: private boolean isversioned;
058: private String revision;
059: private String revisionbranch;
060: private String lastrevision;
061:
062: /**
063: * Create a data transfer object for a Source.
064: *
065: * @param source Source
066: */
067: public SourceDTO(Source source) {
068: this (source, true);
069: }
070:
071: /**
072: * Create a data transfer object for a Source.
073: *
074: * @param source Source
075: */
076: public SourceDTO(Source source, boolean deep) {
077: uri = source.getURI();
078: scheme = source.getScheme();
079: exists = source.exists();
080: if (exists) {
081: validity = source.getValidity();
082: mimetype = source.getMimeType();
083: contentlength = source.getContentLength();
084: lastmodified = source.getLastModified();
085:
086: if (source instanceof TraversableSource) {
087: TraversableSource traversablesource = (TraversableSource) source;
088:
089: iscollection = traversablesource.isCollection();
090:
091: name = traversablesource.getName();
092:
093: try {
094: if (iscollection && deep)
095: for (Iterator i = traversablesource
096: .getChildren().iterator(); i.hasNext();)
097: children.add(new SourceDTO((Source) i
098: .next(), false));
099: } catch (SourceException se) {
100: }
101:
102: try {
103: if (deep && (traversablesource.getParent() != null))
104: parent = new SourceDTO(traversablesource
105: .getParent(), false);
106: } catch (SourceException se) {
107: }
108: }
109:
110: if (source instanceof InspectableSource) {
111: InspectableSource inspectablesource = (InspectableSource) source;
112:
113: try {
114: properties = inspectablesource
115: .getSourceProperties();
116: } catch (SourceException se) {
117: }
118: }
119:
120: if (source instanceof VersionableSource) {
121: VersionableSource versionablesource = (VersionableSource) source;
122:
123: try {
124: isversioned = versionablesource.isVersioned();
125:
126: if (isversioned) {
127: revision = versionablesource
128: .getSourceRevision();
129: revisionbranch = versionablesource
130: .getSourceRevisionBranch();
131: lastrevision = versionablesource
132: .getLatestSourceRevision();
133: }
134: } catch (SourceException se) {
135: }
136: }
137: }
138: }
139:
140: /**
141: * Return an <code>InputStream</code> object to read from the source.
142: * This is the data at the point of invocation of this method,
143: * so if this is Modifiable, you might get different content
144: * from two different invocations.
145: *
146: * @return Input stream for the source.
147: *
148: * @throws IOException If an IO excepetion occurs.
149: * @throws SourceException If an exception occurs.
150: */
151: public InputStream getInputStream() throws IOException,
152: SourceException {
153: throw new IllegalStateException(
154: "Data transfer object does not support this operation");
155: }
156:
157: /**
158: * Return the unique identifer for this source
159: *
160: * @return System identifier for the source.
161: */
162: public String getURI() {
163: return uri;
164: }
165:
166: /**
167: * @see org.apache.excalibur.source.Source#getScheme()
168: *
169: * @return Scheme of the source.
170: */
171: public String getScheme() {
172: return scheme;
173: }
174:
175: /**
176: * Return the authority of a URI. This authority is
177: * typically defined by an Internet-based server or a scheme-specific
178: * registry of naming authorities
179: * (see <a href="http://www.ietf.org/rfc/rfc2396.txt">RFC 2396</a>).
180: *
181: * @return Scheme of the URI.
182: */
183: public String getAuthority() {
184: return SourceUtil.getAuthority(uri);
185: }
186:
187: /**
188: * Return the path of a URI. The path contains data, specific to the
189: * authority (or the scheme if there is no authority component),
190: * identifying the resource within the scope of that scheme and authority
191: * (see <a href="http://www.ietf.org/rfc/rfc2396.txt">RFC 2396</a>).
192: *
193: * @return Path of the URI.
194: */
195: public String getPath() {
196: return SourceUtil.getPath(uri);
197: }
198:
199: /**
200: * Return the query of a URI. The query is a string of information to
201: * be interpreted by the resource
202: * (see <a href="http://www.ietf.org/rfc/rfc2396.txt">RFC 2396</a>).
203: *
204: * @return Query of the URI.
205: */
206: public String getQuery() {
207: return SourceUtil.getQuery(uri);
208: }
209:
210: /**
211: * Return the fragment of a URI. When a URI reference is used to perform
212: * a retrieval action on the identified resource, the optional fragment
213: * identifier, consists of additional reference information to be
214: * interpreted by the user agent after the retrieval action has been
215: * successfully completed
216: * (see <a href="http://www.ietf.org/rfc/rfc2396.txt">RFC 2396</a>).
217: *
218: * @return Fragment of the URI.
219: */
220: public String getFragment() {
221: return SourceUtil.getFragment(uri);
222: }
223:
224: /**
225: * Get the Validity object. This can either wrap the last modification
226: * date or the expires information or...
227: * If it is currently not possible to calculate such an information
228: * <code>null</code> is returned.
229: *
230: * @return Validity for the source.
231: */
232: public SourceValidity getValidity() {
233: return validity;
234: }
235:
236: /**
237: * Refresh the content of this object after the underlying data
238: * content has changed.
239: */
240: public void refresh() {
241: throw new IllegalStateException(
242: "Data transfer object does not support this operation");
243: }
244:
245: /**
246: * The mime-type of the content described by this object.
247: * If the source is not able to determine the mime-type by itself
248: * this can be null.
249: *
250: * @return Mime type of the source.
251: */
252: public String getMimeType() {
253: return mimetype;
254: }
255:
256: /**
257: * Does this source actually exist ?
258: *
259: * @return true if the resource exists.
260: */
261: public boolean exists() {
262: return exists;
263: }
264:
265: /**
266: * Return the content length of the content or -1 if the length is
267: * unknown.
268: *
269: * @return Content length of the source.
270: */
271: public long getContentLength() {
272: return contentlength;
273: }
274:
275: /**
276: * Get the last modification date of the source or 0 if it
277: * is not possible to determine the date.
278: *
279: * @return Last modified date of the source.
280: */
281: public long getLastModified() {
282: return lastmodified;
283: }
284:
285: // ---------------------------------------------------- ModifiableTraversableSource
286:
287: /**
288: * Get an <code>OutputStream</code> where raw bytes can be written to.
289: * The signification of these bytes is implementation-dependent and
290: * is not restricted to a serialized XML document.
291: *
292: * @return a stream to write to
293: *
294: * @throws IOException
295: * @throws SourceException
296: */
297: public OutputStream getOutputStream() throws IOException,
298: SourceException {
299: throw new IllegalStateException(
300: "Data transfer object does not support this operation");
301: }
302:
303: /**
304: * Can the data sent to an <code>OutputStream</code> returned by
305: * {@link #getOutputStream()} be cancelled ?
306: *
307: * @param stream The ouput stream, which should be cancelled.
308: * @return true if the stream can be cancelled
309: */
310: public boolean canCancel(OutputStream stream) {
311: throw new IllegalStateException(
312: "Data transfer object does not support this operation");
313: }
314:
315: /**
316: * Cancel the data sent to an <code>OutputStream</code> returned by
317: * {@link #getOutputStream()}.
318: * <p>
319: * After cancel, the stream should no more be used.
320: *
321: * @param stream The ouput stream, which should be cancelled.
322: *
323: * @throws SourceException If the ouput stream can't be cancelled.
324: */
325: public void cancel(OutputStream stream) throws SourceException {
326: throw new IllegalStateException(
327: "Data transfer object does not support this operation");
328: }
329:
330: /**
331: * Delete the source.
332: */
333: public void delete() {
334: throw new IllegalStateException(
335: "Data transfer object does not support this operation");
336: }
337:
338: public void makeCollection() throws SourceException {
339: throw new IllegalStateException(
340: "Data transfer object does not support this operation");
341: }
342:
343: public Source getChild(String name) throws SourceException {
344: throw new IllegalStateException(
345: "Data transfer object does not support this operation");
346: }
347:
348: public Collection getChildren() throws SourceException {
349: return children;
350: }
351:
352: public String getName() {
353: return name;
354: }
355:
356: public Source getParent() throws SourceException {
357: return parent;
358:
359: }
360:
361: public boolean isCollection() {
362: //System.out.println("uri="+uri+" isCollection="+iscollection);
363: return iscollection;
364: }
365:
366: // ---------------------------------------------------- MoveableSource
367:
368: /**
369: * Move the current source to a specified destination.
370: *
371: * @param source
372: *
373: * @throws SourceException If an exception occurs during the move.
374: */
375: public void moveTo(Source source) throws SourceException {
376: throw new IllegalStateException(
377: "Data transfer object does not support this operation");
378: }
379:
380: /**
381: * Copy the current source to a specified destination.
382: *
383: * @param source
384: *
385: * @throws SourceException If an exception occurs during the copy.
386: */
387: public void copyTo(Source source) throws SourceException {
388: throw new IllegalStateException(
389: "Data transfer object does not support this operation");
390: }
391:
392: // ---------------------------------------------------- InspectableSource
393:
394: /**
395: * Returns a property from a source.
396: *
397: * @param namespace Namespace of the property
398: * @param name Name of the property
399: *
400: * @return Property of the source.
401: *
402: * @throws SourceException If an exception occurs.
403: */
404: public SourceProperty getSourceProperty(String namespace,
405: String name) throws SourceException {
406: for (int i = 0; i < properties.length; i++)
407: if (properties[i].getNamespace().equals(namespace)
408: && properties[i].getName().equals(name))
409: return properties[i];
410: return null;
411: }
412:
413: /**
414: * Sets a property for a source.
415: *
416: * @param property Property of the source
417: *
418: * @throws SourceException If an exception occurs during this operation
419: */
420: public void setSourceProperty(SourceProperty property)
421: throws SourceException {
422: throw new IllegalStateException(
423: "Data transfer object does not support this operation");
424: }
425:
426: /**
427: * Returns a enumeration of the properties
428: *
429: * @return Enumeration of SourceProperty
430: *
431: * @throws SourceException If an exception occurs.
432: */
433: public SourceProperty[] getSourceProperties()
434: throws SourceException {
435: //System.out.println("getProperties()");
436: //for(int i=0; i<properties.length; i++)
437: // System.out.println(i+". namespace="+properties[i].getNamespace()+" name="+properties[i].getName()+" value="+properties[i].getValue());
438: return properties;
439: }
440:
441: /**
442: * Remove a specified source property.
443: *
444: * @param namespace Namespace of the property.
445: * @param name Name of the property.
446: *
447: * @throws SourceException If an exception occurs.
448: */
449: public void removeSourceProperty(String namespace, String name)
450: throws SourceException {
451: throw new IllegalStateException(
452: "Data transfer object does not support this operation");
453: }
454:
455: // ---------------------------------------------------- LockableSource
456:
457: /**
458: * Add a lock to this source
459: *
460: * @param sourcelock Lock, which should be added
461: *
462: * @throws SourceException If an exception occurs during this operation
463: */
464: public void addSourceLocks(SourceLock sourcelock)
465: throws SourceException {
466: throw new IllegalStateException(
467: "Data transfer object does not support this operation");
468: }
469:
470: /**
471: * Returns a enumeration of the existing locks
472: *
473: * @return Enumeration of SourceLock
474: *
475: * @throws SourceException If an exception occurs.
476: */
477: public SourceLock[] getSourceLocks() throws SourceException {
478: throw new IllegalStateException(
479: "Data transfer object does not support this operation");
480: }
481:
482: // ---------------------------------------------------- VersionableSource
483:
484: /**
485: * If this source versioned
486: *
487: * @return True if the current source is versioned.
488: *
489: * @throws SourceException If an exception occurs.
490: */
491: public boolean isVersioned() throws SourceException {
492: return isversioned;
493: }
494:
495: /**
496: * Get the current revision of the source
497: *
498: * @return The current revision of the source
499: *
500: */
501: public String getSourceRevision() {
502: return revision;
503: }
504:
505: /**
506: * Not implemented.
507: *
508: * @param revision The revision, which should be used.
509: *
510: * @throws SourceException If an exception occurs.
511: */
512: public void setSourceRevision(String revision)
513: throws SourceException {
514: throw new IllegalStateException(
515: "Data transfer object does not support this operation");
516: }
517:
518: /**
519: * Get the current branch of the revision from the source
520: *
521: * @return The branch of the revision
522: *
523: * @throws SourceException If an exception occurs.
524: */
525: public String getSourceRevisionBranch() throws SourceException {
526: return revisionbranch;
527: }
528:
529: /**
530: * Not implemented.
531: *
532: * @param branch The branch, which should be used.
533: *
534: * @throws SourceException If an exception occurs.
535: */
536: public void setSourceRevisionBranch(String branch)
537: throws SourceException {
538: throw new IllegalStateException(
539: "Data transfer object does not support this operation");
540: }
541:
542: /**
543: * Get the latest revision
544: *
545: * @return Last revision of the source.
546: *
547: * @throws SourceException If an exception occurs.
548: */
549: public String getLatestSourceRevision() throws SourceException {
550: return lastrevision;
551: }
552:
553: }
|