KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > authoring > DefaultCreator


1 /*
2  * Copyright 1999-2004 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  */

17
18 /* $Id: DefaultCreator.java 201620 2005-06-24 14:02:30Z michi $ */
19
20 package org.apache.lenya.cms.authoring;
21
22 import java.io.File JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.avalon.framework.configuration.Configuration;
27 import org.apache.lenya.xml.DocumentHelper;
28 import org.apache.log4j.Category;
29 import org.w3c.dom.Document JavaDoc;
30
31 public class DefaultCreator implements ParentChildCreatorInterface {
32     private static Category log = Category.getInstance(DefaultCreator.class);
33     public static final String JavaDoc RESOURCE_NAME = "resource-name";
34     public static final String JavaDoc RESOURCE_META_NAME = "resource-meta-name";
35     public static final String JavaDoc SAMPLE_NAME = "sample-name";
36     public static final String JavaDoc SAMPLE_META_NAME = "sample-meta-name";
37
38     private String JavaDoc resourceName = null;
39     private String JavaDoc resourceMetaName = null;
40     private String JavaDoc sampleResourceName = null;
41     private String JavaDoc sampleMetaName = null;
42
43     /**
44      * @see org.apache.lenya.cms.authoring.ParentChildCreatorInterface#init(Configuration)
45      *
46      * @param conf DOCUMENT ME!
47      */

48     public void init(Configuration conf) {
49         if (conf == null) {
50             return;
51         }
52
53         if (conf.getChild(RESOURCE_NAME, false) != null) {
54             resourceName = conf.getChild(RESOURCE_NAME).getValue("index.xml");
55         }
56
57         if (conf.getChild(RESOURCE_META_NAME, false) != null) {
58             resourceMetaName =
59                 conf.getChild(RESOURCE_META_NAME).getValue("index-meta.xml");
60         }
61
62         if (conf.getChild(SAMPLE_NAME, false) != null) {
63             sampleResourceName =
64                 conf.getChild(SAMPLE_NAME).getValue("sampleindex.xml");
65         }
66
67         if (conf.getChild(SAMPLE_META_NAME, false) != null) {
68             sampleMetaName =
69                 conf.getChild(SAMPLE_META_NAME).getValue("samplemeta.xml");
70         }
71     }
72
73     /**
74      * Generate a tree id by returning the child ID.
75      *
76      * @param childId a <code>String</code> value
77      * @param childType a <code>short</code> value
78      *
79      * @return a <code>String</code> value
80      *
81      * @exception Exception if an error occurs
82      */

83     public String JavaDoc generateTreeId(String JavaDoc childId, short childType)
84         throws Exception JavaDoc {
85         return childId;
86     }
87
88     /**
89      * Return the child type by simply returning the child type.
90      *
91      * @param childType a <code>short</code> value
92      *
93      * @return a <code>short</code> value
94      *
95      * @exception Exception if an error occurs
96      */

97     public short getChildType(short childType) throws Exception JavaDoc {
98         return childType;
99     }
100
101     /**
102      * Create Child Name for tree entry
103      *
104      * @param childname a <code>String</code> value
105      *
106      * @return a <code>String</code> for Child Name for tree entry
107      *
108      * @exception Exception if an error occurs
109      */

110     public String JavaDoc getChildName(String JavaDoc childname) throws Exception JavaDoc {
111         if (childname.length() != 0) {
112             return childname;
113         } else {
114             return "abstract_default";
115         }
116     }
117
118     /**
119       * DOCUMENT ME!
120       *
121       * @param samplesDir DOCUMENT ME!
122       * @param parentDir DOCUMENT ME!
123       * @param childId DOCUMENT ME!
124       * @param childType DOCUMENT ME!
125       * @param childName the name of the child
126       * @param language for which the document is created
127       * @param parameters additional parameters that can be considered when
128       * creating the child
129       *
130       * @throws Exception DOCUMENT ME!
131       */

132     public void create(
133         File JavaDoc samplesDir,
134         File JavaDoc parentDir,
135         String JavaDoc childId,
136         short childType,
137         String JavaDoc childName,
138         String JavaDoc language,
139         Map JavaDoc parameters)
140         throws Exception JavaDoc {
141         // Set filenames
142
String JavaDoc id = generateTreeId(childId, childType);
143         String JavaDoc filename = getChildFileName(parentDir, id, language);
144         String JavaDoc filenameMeta = getChildMetaFileName(parentDir, id, language);
145
146         String JavaDoc doctypeSample = samplesDir + File.separator + sampleResourceName;
147         String JavaDoc doctypeMeta = samplesDir + File.separator + sampleMetaName;
148
149         File JavaDoc sampleFile = new File JavaDoc(doctypeSample);
150         if (!sampleFile.exists()) {
151             log.error("No such sample file: " + sampleFile + " Have you configured the sample within doctypes.xconf?");
152             throw new FileNotFoundException JavaDoc("" + sampleFile);
153         }
154
155         // Read sample file
156
log.debug("Read sample file: " + doctypeSample);
157
158         Document JavaDoc doc = DocumentHelper.readDocument(new File JavaDoc(doctypeSample));
159
160         log.debug("sample document: " + doc);
161
162         // transform the xml if needed
163
log.debug("transform sample file: ");
164         transformXML(doc, id, childType, childName, parameters);
165
166         // write the document (create the path, i.e. the parent
167
// directory first if needed)
168
File JavaDoc parent = new File JavaDoc(new File JavaDoc(filename).getParent());
169
170         if (!parent.exists()) {
171             parent.mkdirs();
172         }
173
174         // Write file
175
log.debug("write file: " + filename);
176         DocumentHelper.writeDocument(doc, new File JavaDoc(filename));
177
178         // now do the same thing for the meta document if the
179
// sampleMetaName is specified
180
if (sampleMetaName != null) {
181             doc = DocumentHelper.readDocument(new File JavaDoc(doctypeMeta));
182
183             transformMetaXML(doc, id, childType, childName, parameters);
184
185             parent = new File JavaDoc(new File JavaDoc(filenameMeta).getParent());
186
187             if (!parent.exists()) {
188                 parent.mkdirs();
189             }
190
191             DocumentHelper.writeDocument(doc, new File JavaDoc(filenameMeta));
192         }
193     }
194
195     /**
196      * Apply some transformation on the newly created child.
197      *
198      * @param doc the xml document
199      * @param childId the id of the child
200      * @param childType the type of child
201      * @param childName the name of the child
202      * @param parameters additional parameters that can be used in the transformation
203      *
204      * @throws Exception if the transformation fails
205      */

206     protected void transformXML(
207         Document JavaDoc doc,
208         String JavaDoc childId,
209         short childType,
210         String JavaDoc childName,
211         Map JavaDoc parameters)
212         throws Exception JavaDoc {}
213
214     /**
215      * Apply some transformation on the meta file of newly created child.
216      *
217      * @param doc the xml document
218      * @param childId the id of the child
219      * @param childType the type of child
220      * @param childName the name of the child
221      * @param parameters additional parameters that can be used in the transformation
222      *
223      * @throws Exception if the transformation fails
224      */

225     protected void transformMetaXML(
226         Document JavaDoc doc,
227         String JavaDoc childId,
228         short childType,
229         String JavaDoc childName,
230         Map JavaDoc parameters)
231         throws Exception JavaDoc {}
232
233     /**
234      * Get the file name of the child
235      *
236      * @param parentDir the parent directory
237      * @param childId the id of the child
238      * @param language for which the document is created
239      *
240      * @return the file name of the child
241      */

242     protected String JavaDoc getChildFileName(
243         File JavaDoc parentDir,
244         String JavaDoc childId,
245         String JavaDoc language) {
246         return null;
247     }
248
249     /**
250      * Get the file name of the meta file
251      *
252      * @param parentDir the parent directory
253      * @param childId the id of the child
254      * @param language for which the document is created
255      *
256      * @return the name of the meta file
257      */

258     protected String JavaDoc getChildMetaFileName(
259         File JavaDoc parentDir,
260         String JavaDoc childId,
261         String JavaDoc language) {
262         return null;
263     }
264
265     /**
266      * Create the language suffix for a file name given a language string
267      *
268      * @param language the language
269      *
270      * @return the suffix for the language dependant file name
271      */

272     protected String JavaDoc getLanguageSuffix(String JavaDoc language) {
273         return (language != null) ? "_" + language : "";
274     }
275
276     /**
277      * Get filename of template/sample
278      */

279     public String JavaDoc getSampleResourceName() {
280         return sampleResourceName;
281     }
282 }
283
Popular Tags