001 /* 002 * JBoss DNA (http://www.jboss.org/dna) 003 * See the COPYRIGHT.txt file distributed with this work for information 004 * regarding copyright ownership. Some portions may be licensed 005 * to Red Hat, Inc. under one or more contributor license agreements. 006 * See the AUTHORS.txt file in the distribution for a full listing of 007 * individual contributors. 008 * 009 * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA 010 * is licensed to you under the terms of the GNU Lesser General Public License as 011 * published by the Free Software Foundation; either version 2.1 of 012 * the License, or (at your option) any later version. 013 * 014 * JBoss DNA is distributed in the hope that it will be useful, 015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 017 * Lesser General Public License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this software; if not, write to the Free 021 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 022 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 023 */ 024 package org.jboss.dna.jcr; 025 026 import java.io.IOException; 027 import java.io.InputStream; 028 import java.util.Arrays; 029 import net.jcip.annotations.Immutable; 030 import org.jboss.dna.cnd.CndImporter; 031 import org.jboss.dna.common.collection.ImmutableProblems; 032 import org.jboss.dna.common.collection.Problems; 033 import org.jboss.dna.common.collection.SimpleProblems; 034 import org.jboss.dna.graph.ExecutionContext; 035 import org.jboss.dna.graph.Graph; 036 import org.jboss.dna.graph.connector.inmemory.InMemoryRepositorySource; 037 import org.jboss.dna.graph.io.Destination; 038 import org.jboss.dna.graph.io.GraphBatchDestination; 039 import org.jboss.dna.graph.property.PathFactory; 040 041 /** 042 * Class to parse one or more Compact Node Definition (CND) files containing custom node type definitions into a format that can 043 * be registered with the {@link RepositoryNodeTypeManager}. 044 * <p> 045 * The class contains methods for determining whether the CND files were parsed successfully and what errors occurred. Typically, 046 * the class will be used like this: 047 * 048 * <pre> 049 * try { 050 * String[] cndFilePaths = // The URIs for the resource files on the classpath 051 * JcrNodeTypeSource nodeTypeSource = new CndNodeTypeSource(cndFilePaths); 052 * 053 * if (!nodeTypeSource.isValid()) { 054 * Problems problems = nodeTypeSource.getProblems(); 055 * // Report problems 056 * } 057 * else { 058 * repositoryNodeTypeManager.registerNodeTypes(nodeTypeSource); 059 * } 060 * } 061 * catch (IOException ioe) { 062 * System.err.println("Could not find one of the CND files."); 063 * ioe.printStackTrace(); 064 * } 065 * 066 * </pre> 067 * 068 * </p> 069 */ 070 @Immutable 071 public class CndNodeTypeSource implements JcrNodeTypeSource { 072 073 private final Graph graph; 074 private final Problems problems; 075 076 /** 077 * Creates a new {@link JcrNodeTypeSource} with based on the CND file with the given resource name. 078 * 079 * @param resourceName the name of the resource; this resource must be on the classpath 080 * @throws IOException if an error loading or reading the resource occurs 081 */ 082 public CndNodeTypeSource( String resourceName ) throws IOException { 083 this(new String[] {resourceName}); 084 } 085 086 /** 087 * Creates a new {@link JcrNodeTypeSource} based on the CND files at the given resource names. 088 * 089 * @param resourceNames the name of the resources to load; these resources must be on the classpath 090 * @throws IOException if an error loading or reading the any of the resources occurs 091 */ 092 public CndNodeTypeSource( String resourceNames[] ) throws IOException { 093 094 Problems problems = new SimpleProblems(); 095 096 // Graph creation requires a context, but there are no security implications for this and namespace mappings are 097 // specified in the CND file itself 098 ExecutionContext context = new ExecutionContext(); 099 PathFactory pathFactory = context.getValueFactories().getPathFactory(); 100 InMemoryRepositorySource source = new InMemoryRepositorySource(); 101 source.setName("CND Import Source"); 102 this.graph = Graph.create(source, context); 103 for (String resourceName : Arrays.asList(resourceNames)) { 104 Graph.Batch batch = graph.batch(); 105 Destination destination = new GraphBatchDestination(batch); 106 CndImporter importer = new CndImporter(destination, pathFactory.createRootPath()); 107 InputStream is = getClass().getResourceAsStream(resourceName); 108 109 // This submits the batch 110 importer.importFrom(is, problems, resourceName); 111 } 112 this.problems = new ImmutableProblems(problems); 113 } 114 115 /** 116 * Returns true if no errors were encountered while parsing the CND file or files 117 * 118 * @return true if no errors were encountered while parsing the CND file or files 119 */ 120 public boolean isValid() { 121 return !problems.hasErrors(); 122 } 123 124 /** 125 * Returns the problems (if any) that were encountered parsing the CND files. Node type registration errors or warnings will 126 * NOT be added to this set of problems. 127 * 128 * @return returns the problems (if any) that were encountered parsing the CND files. 129 */ 130 public Problems getProblems() { 131 return problems; 132 } 133 134 /** 135 * {@inheritDoc} 136 * 137 * @see org.jboss.dna.jcr.JcrNodeTypeSource#getNodeTypes() 138 */ 139 public final Graph getNodeTypes() { 140 return graph; 141 } 142 143 }