View Javadoc
1   /**
2    * This file Copyright (c) 2015-2016 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.resourceloader;
35  
36  import info.magnolia.resourceloader.util.ResourceTreeWalker;
37  
38  import java.io.BufferedReader;
39  import java.io.IOException;
40  import java.io.InputStream;
41  import java.io.InputStreamReader;
42  import java.io.Reader;
43  import java.nio.charset.Charset;
44  import java.util.List;
45  
46  /**
47   * Exposes methods that {@link AbstractResource} simply delegates to. Consequently, one only has to implement and
48   * test AbstractResourceOrigin's methods, while navigation still happens naturally through the Resource methods.
49   * AbstractResource implementations are merely vehicles for the underlying resource.
50   *
51   * @param <P> Subclasses of AbstractResourceOrigin are expected to produce {@link Resource} instances which are subclasses of {@link AbstractResource}.
52   */
53  public abstract class AbstractResourceOrigin<P extends AbstractResource> implements ResourceOrigin<P> {
54      private final String name;
55  
56      protected AbstractResourceOrigin(String name) {
57          this.name = name;
58      }
59  
60      @Override
61      public String getName() {
62          return name;
63      }
64  
65      @Override
66      public void traverseWith(ResourceVisitor visitor) {
67          new ResourceTreeWalker(visitor).traverse(getRoot());
68      }
69  
70      /**
71       * @see Resource#isFile()
72       */
73      protected abstract boolean isFile(P resource);
74  
75      /**
76       * @see Resource#isDirectory()
77       */
78      protected abstract boolean isDirectory(P resource);
79  
80      /**
81       * @see Resource#isEditable()
82       */
83      protected boolean isEditable(P resource) {
84          return false;
85      }
86  
87      /**
88       * @see Resource#getPath()
89       */
90      protected abstract String getPath(P resource);
91  
92      /**
93       * @see Resource#getName()
94       */
95      protected abstract String getName(P resource);
96  
97      /**
98       * @see Resource#getLastModified()
99       */
100     protected abstract long getLastModified(P resource);
101 
102     /**
103      * @see Resource#listChildren()
104      */
105     protected abstract List<P> listChildren(P resource);
106 
107     /**
108      * @see Resource#getParent()
109      */
110     protected abstract P getParent(P resource);
111 
112     /**
113      * @see Resource#openStream()
114      */
115     protected InputStream openStream(P resource) throws IOException {
116         if (resource.isDirectory()) {
117             throw new IllegalStateException("Can not open stream on directory " + resource);
118         }
119         final InputStream inputStream = doOpenStream(resource);
120         if (inputStream == null) {
121             throw new IllegalStateException("InputStream is null for " + resource);
122         }
123         return inputStream;
124     }
125 
126     protected abstract InputStream doOpenStream(P resource) throws IOException;
127 
128     /**
129      * @see Resource#openReader()
130      */
131     protected Reader openReader(P resource) throws IOException {
132         final InputStream in = openStream(resource);
133         final Charset charset = getCharsetFor(resource);
134         final Reader reader = new InputStreamReader(in, charset);
135         return new BufferedReader(reader);
136     }
137 
138     /**
139      * Returns the preferred {@link Charset} to read the given {@link Resource}.
140      */
141     protected abstract Charset getCharsetFor(P resource);
142 
143 }