View Javadoc

1   /*
2    * Copyright 2000-20045 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.portals.graffito.jcr.mapper.impl;
17  
18  import java.io.FileInputStream;
19  import java.io.FileNotFoundException;
20  import java.io.InputStream;
21  import java.net.URL;
22  
23  import org.apache.commons.digester.Digester;
24  import org.apache.portals.graffito.jcr.exception.InitMapperException;
25  import org.apache.portals.graffito.jcr.mapper.model.BeanDescriptor;
26  import org.apache.portals.graffito.jcr.mapper.model.ClassDescriptor;
27  import org.apache.portals.graffito.jcr.mapper.model.CollectionDescriptor;
28  import org.apache.portals.graffito.jcr.mapper.model.FieldDescriptor;
29  import org.apache.portals.graffito.jcr.mapper.model.ImplementDescriptor;
30  import org.apache.portals.graffito.jcr.mapper.model.MappingDescriptor;
31  import org.xml.sax.SAXParseException;
32  
33  /***
34   * Helper class that reads the xml mapping file and load all class descriptors into memory (object graph)
35   * 
36   * @author <a href="mailto:christophe.lombart@sword-technologies.com">Lombart Christophe </a>
37   * @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'>Alexandru Popescu</a>
38   */
39  public class DigesterDescriptorReader
40  {
41      private boolean validating = true;
42      private URL dtdResolver;
43      
44      /***
45       * Set if the mapping should be validated.
46       * @param flag <tt>true</tt> if the mapping should be validated
47       */
48      public void setValidating(boolean flag) {
49          this.validating= flag;
50      }
51  
52      public void setResolver(URL dtdResolver) {
53          this.dtdResolver = dtdResolver;
54      }
55      
56  	/***
57  	 * Load all class descriptors found in the xml mapping file.
58  	 * 
59  	 * @param stream the xml mapping file reference
60  	 * @return a {@link MappingDescriptor}
61  	 * 
62  	 */
63  	public MappingDescriptor loadClassDescriptors(InputStream stream)
64  	{
65  		try
66  		{
67  			Digester digester = new Digester();
68  			digester.setValidating(this.validating);
69  			if (null != this.dtdResolver) {
70                  digester.register("-//The Apache Software Foundation//DTD Repository//EN",
71                                    this.dtdResolver.toString());
72              }
73  			
74  	        MappingDescriptor mappingDescriptor = new MappingDescriptor();
75  	        digester.push(mappingDescriptor);
76  			
77  	       // TODO : activater the following line wich cause some bugs when loading the xml stream  
78             //digester.addSetProperties("graffito-jcr", package, pa);
79  
80  			// --------------------------------------------------------------------------------
81  			// Rules used for the class-descriptor element
82  			// --------------------------------------------------------------------------------	                        
83  			digester.addObjectCreate("graffito-jcr/class-descriptor", ClassDescriptor.class);
84  			digester.addSetProperties("graffito-jcr/class-descriptor");
85  			digester.addSetNext("graffito-jcr/class-descriptor", "addClassDescriptor");		
86  
87  			// --------------------------------------------------------------------------------
88  			// Rules used for the field-descriptor element
89  			// --------------------------------------------------------------------------------
90  			digester.addObjectCreate("graffito-jcr/class-descriptor/implement-descriptor", ImplementDescriptor.class);
91  			digester.addSetProperties("graffito-jcr/class-descriptor/implement-descriptor");
92              digester.addSetNext("graffito-jcr/class-descriptor/implement-descriptor", "addImplementDescriptor");
93  			
94  			// --------------------------------------------------------------------------------
95  			// Rules used for the field-descriptor element
96  			// --------------------------------------------------------------------------------
97  			digester.addObjectCreate("graffito-jcr/class-descriptor/field-descriptor", FieldDescriptor.class);
98  			digester.addSetProperties("graffito-jcr/class-descriptor/field-descriptor");
99              digester.addSetNext("graffito-jcr/class-descriptor/field-descriptor", "addFieldDescriptor");
100 
101 			// --------------------------------------------------------------------------------
102 			// Rules used for the bean-descriptor element
103 			// --------------------------------------------------------------------------------
104 			digester.addObjectCreate("graffito-jcr/class-descriptor/bean-descriptor", BeanDescriptor.class);
105 			digester.addSetProperties("graffito-jcr/class-descriptor/bean-descriptor");
106             digester.addSetNext("graffito-jcr/class-descriptor/bean-descriptor", "addBeanDescriptor");
107 
108 			// --------------------------------------------------------------------------------
109 			// Rules used for the collection-descriptor element
110 			// --------------------------------------------------------------------------------
111 			digester.addObjectCreate("graffito-jcr/class-descriptor/collection-descriptor", CollectionDescriptor.class);
112 			digester.addSetProperties("graffito-jcr/class-descriptor/collection-descriptor");
113             digester.addSetNext("graffito-jcr/class-descriptor/collection-descriptor", "addCollectionDescriptor");			
114 
115             return (MappingDescriptor) digester.parse(stream);
116 		}
117 		catch (Exception e)
118 		{
119 			throw new InitMapperException("Impossible to read the xml mapping file", e);
120 		}
121 	}
122 
123 	/***
124 	 * Load all class descriptors found in the xml mapping file.
125 	 * 
126 	 * @param xmlFile the xml mapping file reference
127 	 * @return a {@link MappingDescriptor}
128 	 * 
129 	 */	
130 	public MappingDescriptor loadClassDescriptors(String xmlFile)
131 	{
132 		try
133 		{
134 			return loadClassDescriptors(new FileInputStream(xmlFile));
135 		}
136 		
137 		catch (FileNotFoundException e)
138 		{
139 			throw new InitMapperException("Mapping file not found : " + xmlFile,e);
140 		}
141 	}
142 }