View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors.
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   * 
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.jcr;
25  
26  import java.io.File;
27  import java.io.IOException;
28  import java.io.InputStream;
29  import java.net.URL;
30  import java.util.Iterator;
31  import javax.jcr.Session;
32  import javax.jcr.nodetype.NodeTypeDefinition;
33  import org.modeshape.cnd.CndImporter;
34  import org.modeshape.common.collection.Problems;
35  import org.modeshape.graph.ExecutionContext;
36  import org.modeshape.graph.Graph;
37  import org.modeshape.graph.Location;
38  import org.modeshape.graph.Subgraph;
39  import org.modeshape.graph.io.Destination;
40  import org.modeshape.graph.property.Path;
41  
42  /**
43   * A class that reads files in the standard CND format defined by the JCR 2.0 specification.
44   * <p>
45   * Typically, the class will be used like this:
46   * 
47   * <pre>
48   * Session session = ...
49   * CndNodeTypeReader reader = new CndNodeTypeReader(session);
50   * reader.read(file); // or stream or resource file
51   * 
52   * if (!reader.getProblems().isEmpty()) {
53   *     // Report problems
54   * } else {
55   *     boolean allowUpdate = false;
56   *     session.getWorkspace().getNodeTypeManager().registerNodeTypes(reader.getNodeTypeDefinitions(), allowUpdate);
57   * }
58   * </pre>
59   * 
60   * </p>
61   */
62  public class CndNodeTypeReader extends GraphNodeTypeReader {
63  
64      /**
65       * Create a new node type factory that reads CND files.
66       * 
67       * @param session the session that will be used to register the node types; may not be null
68       */
69      public CndNodeTypeReader( Session session ) {
70          super(session);
71      }
72  
73      /**
74       * Create a new node type factory that reads CND files.
75       * 
76       * @param context the context that will be used to load the node types; may not be null
77       */
78      public CndNodeTypeReader( ExecutionContext context ) {
79          super(context);
80      }
81  
82      /**
83       * Import the node types from the supplied stream and add all of the node type definitions to this factory's list.
84       * 
85       * @param stream the stream containing the node types
86       * @param resourceName a logical name for the resource name to be used when reporting problems; may be null if there is no
87       *        useful name
88       * @throws IOException if there is a problem reading from the supplied stream
89       */
90      @Override
91      public void read( InputStream stream,
92                        String resourceName ) throws IOException {
93          super.read(stream, resourceName);
94      }
95  
96      /**
97       * Import the node types from the supplied file and add all of the node type definitions to this factory's list.
98       * 
99       * @param file the file containing the node types
100      * @throws IOException if there is a problem reading from the supplied stream
101      */
102     @Override
103     public void read( File file ) throws IOException {
104         super.read(file);
105     }
106 
107     /**
108      * Import the node types from the file at the supplied URL and add all of the node type definitions to this factory's list.
109      * 
110      * @param url the URL to the file containing the node types
111      * @throws IOException if there is a problem reading from the supplied stream
112      */
113     @Override
114     public void read( URL url ) throws IOException {
115         super.read(url);
116     }
117 
118     /**
119      * Import the node types from the supplied file and add all of the node type definitions to this factory's list.
120      * 
121      * @param resourceFile the name of the resource file on the classpath containing the node types
122      * @throws IOException if there is a problem reading from the supplied resource
123      */
124     @Override
125     public void read( String resourceFile ) throws IOException {
126         super.read(resourceFile);
127     }
128 
129     /**
130      * Import the node types from the supplied string and add all of the node type definitions to this factory's list.
131      * 
132      * @param content the string containing the node types
133      * @param resourceName a logical name for the resource name to be used when reporting problems; may be null if there is no
134      *        useful name
135      */
136     @Override
137     public void read( String content,
138                       String resourceName ) {
139         super.read(content, resourceName);
140     }
141 
142     /**
143      * Import the node types from the supplied location in the specified graph.
144      * 
145      * @param graph the graph containing the standard ModeShape CND content
146      * @param parentOfTypes the path to the parent of the node type definition nodes
147      * @param resourceName a logical name for the resource name to be used when reporting problems; may be null if there is no
148      *        useful name
149      */
150     @Override
151     public void read( Graph graph,
152                       Path parentOfTypes,
153                       String resourceName ) {
154         super.read(graph, parentOfTypes, resourceName);
155     }
156 
157     /**
158      * Import the node types from the supplied location in the specified graph.
159      * 
160      * @param subgraph the subgraph containing the standard ModeShape CND content
161      * @param locationOfParent the location to the parent of the node type definition nodes
162      * @param resourceName a logical name for the resource name to be used when reporting problems; may be null if there is no
163      *        useful name
164      */
165     @Override
166     public void read( Subgraph subgraph,
167                       Location locationOfParent,
168                       String resourceName ) {
169         super.read(subgraph, locationOfParent, resourceName);
170     }
171 
172     /**
173      * Get the problems where warnings and error messages were recorded by this factory.
174      * 
175      * @return the problems; never null
176      */
177     @Override
178     public Problems getProblems() {
179         return super.getProblems();
180     }
181 
182     /**
183      * Returns the node type definitions created by this factory.
184      * 
185      * @return the {@link NodeTypeDefinition}s
186      */
187     @Override
188     public NodeTypeDefinition[] getNodeTypeDefinitions() {
189         return super.getNodeTypeDefinitions();
190     }
191 
192     /**
193      * {@inheritDoc}
194      * 
195      * @see java.lang.Iterable#iterator()
196      */
197     @Override
198     public Iterator<NodeTypeDefinition> iterator() {
199         return super.iterator();
200     }
201 
202     protected void readBuiltInTypes() throws IOException {
203         read("/org/modeshape/jcr/jsr_283_builtins.cnd");
204         read("/org/modeshape/jcr/modeshape_builtins.cnd");
205     }
206 
207     /**
208      * {@inheritDoc}
209      * 
210      * @see org.modeshape.jcr.GraphNodeTypeReader#importFrom(org.modeshape.graph.io.Destination,
211      *      org.modeshape.graph.property.Path, java.lang.String, java.lang.String)
212      */
213     @Override
214     protected void importFrom( Destination destination,
215                                Path path,
216                                String content,
217                                String resourceName ) throws Exception {
218         CndImporter importer = new CndImporter(destination, pathFactory.createRootPath());
219         importer.importFrom(content, problems, resourceName);
220     }
221 }