KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > publication > DocumentTypeBuilder


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: DocumentTypeBuilder.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.cms.publication;
21
22 import java.io.File JavaDoc;
23
24 import org.apache.avalon.framework.configuration.Configuration;
25 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
26 import org.apache.lenya.cms.authoring.ParentChildCreatorInterface;
27
28
29 /**
30  * A builder for document types.
31  */

32 public final class DocumentTypeBuilder {
33     /** Creates a new instance of DocumentTypeBuilder */
34     private DocumentTypeBuilder() {
35     }
36
37     /**
38      * The default document types configuration directory, relative to the publication directory.
39      */

40     public static final String JavaDoc DOCTYPE_DIRECTORY = "config/doctypes".replace('/', File.separatorChar);
41
42     /*
43      * The default document types configuration file, relative to the publication directory.
44      */

45     public static final String JavaDoc CONFIG_FILE = "doctypes.xconf".replace('/', File.separatorChar);
46     public static final String JavaDoc DOCTYPES_ELEMENT = "doctypes";
47     public static final String JavaDoc DOCTYPE_ELEMENT = "doc";
48     public static final String JavaDoc TYPE_ATTRIBUTE = "type";
49     public static final String JavaDoc CREATOR_ELEMENT = "creator";
50     public static final String JavaDoc SRC_ATTRIBUTE = "src";
51     public static final String JavaDoc WORKFLOW_ELEMENT = "workflow";
52
53     /**
54      * Builds a document type for a given name.
55      *
56      * @param name A string value.
57      * @param publication The publication the document type belongs to.
58      * @return A document type object.
59      * @throws DocumentTypeBuildException When something went wrong.
60      */

61     public static DocumentType buildDocumentType(String JavaDoc name, Publication publication)
62         throws DocumentTypeBuildException {
63         DocumentType type = new DocumentType(name);
64
65         File JavaDoc configDirectory = new File JavaDoc(publication.getDirectory(), DOCTYPE_DIRECTORY);
66         File JavaDoc configFile = new File JavaDoc(configDirectory, CONFIG_FILE);
67
68         try {
69             Configuration configuration = new DefaultConfigurationBuilder().buildFromFile(configFile);
70
71             Configuration[] doctypeConfigurations = configuration.getChildren(DOCTYPE_ELEMENT);
72             Configuration doctypeConf = null;
73
74             for (int i = 0; i < doctypeConfigurations.length; i++) {
75                 if (doctypeConfigurations[i].getAttribute(TYPE_ATTRIBUTE).equals(name)) {
76                     doctypeConf = doctypeConfigurations[i];
77                 }
78             }
79
80             if (doctypeConf == null) {
81                 throw new DocumentTypeBuildException("No definition found for doctype '" + name +
82                     "'!");
83             }
84
85             ParentChildCreatorInterface creator;
86             Configuration creatorConf = doctypeConf.getChild(CREATOR_ELEMENT, false);
87
88             if (creatorConf != null) {
89                 String JavaDoc creatorClassName = creatorConf.getAttribute(SRC_ATTRIBUTE);
90                 Class JavaDoc creatorClass = Class.forName(creatorClassName);
91                 creator = (ParentChildCreatorInterface) creatorClass.newInstance();
92                 creator.init(creatorConf);
93             } else {
94                 creator = new org.apache.lenya.cms.authoring.DefaultBranchCreator();
95             }
96
97             type.setCreator(creator);
98
99             Configuration workflowConf = doctypeConf.getChild(WORKFLOW_ELEMENT, false);
100
101             if (workflowConf != null) {
102                 String JavaDoc workflowFileName = workflowConf.getAttribute(SRC_ATTRIBUTE);
103                 type.setWorkflowFileName(workflowFileName);
104             }
105         } catch (Exception JavaDoc e) {
106             throw new DocumentTypeBuildException(e);
107         }
108
109         return type;
110     }
111 }
112
Popular Tags