View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   */
19  package org.apache.rat.api;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.Reader;
24  import java.nio.file.Path;
25  import java.nio.file.Paths;
26  import java.util.SortedSet;
27  
28  /**
29   * The representation of a document being scanned.
30   */
31  public abstract class Document implements Comparable<Document> {
32  
33      /**
34       * An enumeraton of document types.
35       */
36      public enum Type {
37          /** A generated document. */
38          GENERATED,
39          /** An unknown document type. */
40          UNKNOWN,
41          /** An archive type document. */
42          ARCHIVE,
43          /** A notice document (e.g. LICENSE file) */
44          NOTICE,
45          /** A binary file */
46          BINARY,
47          /** A standard document */
48          STANDARD;;
49      }
50  
51      private final MetaData metaData;
52      private final String name;
53  
54      /**
55       * Creates an instance.
56       * @param name the name of the resource.
57       */
58      protected Document(String name) {
59          this.name = name;
60          this.metaData = new MetaData();
61      }
62  
63      /**
64       * @return the name of the current document.
65       */
66      public final String getName() {
67          return name;
68      }
69  
70      @Override
71      public int compareTo(Document doc) {
72          return getPath().compareTo(doc.getPath());
73      }
74  
75      @Override
76      public int hashCode() {
77          return getPath().hashCode();
78      }
79  
80      @Override
81      public boolean equals(final Object obj) {
82          if (!(obj instanceof Document)) {
83              return false;
84          }
85          return getPath().equals(((Document) obj).getPath());
86      }
87  
88      /**
89       * Get the path that identifies the document.
90       * @return the path for the document.
91       */
92      public Path getPath() {
93          return Paths.get(getName());
94      }
95  
96      /**
97       * Reads the contents of this document.
98       * 
99       * @return <code>Reader</code> not null
100      * @throws IOException if this document cannot be read
101      * composite archive
102      */
103     public abstract Reader reader() throws IOException;
104 
105     /**
106      * Streams the document's contents.
107      * 
108      * @return a non null input stream of the document.
109      * @throws IOException when stream could not be opened
110      */
111     public abstract InputStream inputStream() throws IOException;
112 
113     /**
114      * Gets data describing this resource.
115      * 
116      * @return a non null MetaData object.
117      */
118     public final MetaData getMetaData() {
119         return metaData;
120     }
121 
122     /**
123      * Representations suitable for logging.
124      * @return a <code>String</code> representation
125      * of this object.
126      */
127     @Override
128     public String toString() {
129         return String.format("%s( name = %s metaData = %s )", this.getClass().getSimpleName(), getName(), getMetaData());
130     }
131 
132     /**
133      * Determines if this Document is a directory type.
134      * @return {@code true} if this is a directory.
135      */
136     public abstract boolean isDirectory();
137 
138     /**
139      * Gets a sorted set of Documents that are children of this document.
140      * @return A sorted set of child Documents.  May  be empty
141      */
142     public abstract SortedSet<Document> listChildren();
143 
144 }