KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > storeconfig > StoreFactoryBase


1 /*
2  * Copyright 1999-2001,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 package org.apache.catalina.storeconfig;
18
19 import java.io.PrintWriter JavaDoc;
20
21 import org.apache.catalina.util.StringManager;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 /**
26  * StoreFactory saves spezial elements.
27  * Output was generate with StoreAppenders.
28  *
29  * @author Peter Rossbach
30  * @version 1.0
31  *
32  */

33 public class StoreFactoryBase implements IStoreFactory {
34     private static Log log = LogFactory.getLog(StoreFactoryBase.class);
35
36     private StoreRegistry registry;
37
38     private StoreAppender storeAppender = new StoreAppender();
39
40     /**
41      * The string manager for this package.
42      */

43     protected static final StringManager sm = StringManager
44             .getManager(Constants.Package);
45
46     /**
47      * The descriptive information string for this implementation.
48      */

49     private static final String JavaDoc info = "org.apache.catalina.config.StoreFactoryBase/1.0";
50
51     /**
52      * Return descriptive information about this Facotry implementation and the
53      * corresponding version number, in the format
54      * <code>&lt;description&gt;/&lt;version&gt;</code>.
55      */

56     public String JavaDoc getInfo() {
57
58         return (info);
59
60     }
61
62     /**
63      * @return Returns the storeAppender.
64      */

65     public StoreAppender getStoreAppender() {
66         return storeAppender;
67     }
68
69     /**
70      * @param storeAppender
71      * The storeAppender to set.
72      */

73     public void setStoreAppender(StoreAppender storeAppender) {
74         this.storeAppender = storeAppender;
75     }
76
77     /*
78      * set Registry
79      *
80      * @see org.apache.catalina.config.IStoreFactory#setRegistry(org.apache.catalina.config.ConfigurationRegistry)
81      */

82     public void setRegistry(StoreRegistry aRegistry) {
83         registry = aRegistry;
84
85     }
86
87     /*
88      * get Registry
89      *
90      * @see org.apache.catalina.config.IStoreFactory#getRegistry()
91      */

92     public StoreRegistry getRegistry() {
93
94         return registry;
95     }
96
97     public void storeXMLHead(PrintWriter JavaDoc aWriter) {
98         // Store the beginning of this element
99
aWriter.print("<?xml version=\"1.0\" encoding=\"");
100         aWriter.print(getRegistry().getEncoding());
101         aWriter.println("\"?>");
102     }
103
104     /*
105      * Store a server.xml element with attributes and childs
106      *
107      * @see org.apache.catalina.storeconfig.IStoreFactory#store(java.io.PrintWriter,
108      * int, java.lang.Object)
109      */

110     public void store(PrintWriter JavaDoc aWriter, int indent, Object JavaDoc aElement)
111             throws Exception JavaDoc {
112
113         StoreDescription elementDesc = getRegistry().findDescription(
114                 aElement.getClass());
115
116         if (elementDesc != null) {
117             if (log.isDebugEnabled())
118                 log.debug(sm.getString("factory.storeTag",
119                         elementDesc.getTag(), aElement));
120             getStoreAppender().printIndent(aWriter, indent + 2);
121             if (!elementDesc.isChilds()) {
122                 getStoreAppender().printTag(aWriter, indent, aElement,
123                         elementDesc);
124             } else {
125                 getStoreAppender().printOpenTag(aWriter, indent + 2, aElement,
126                         elementDesc);
127                 storeChilds(aWriter, indent + 2, aElement, elementDesc);
128                 getStoreAppender().printIndent(aWriter, indent + 2);
129                 getStoreAppender().printCloseTag(aWriter, elementDesc);
130             }
131         } else
132             log.warn(sm.getString("factory.storeNoDescriptor", aElement
133                     .getClass()));
134     }
135
136     /**
137      * Must Implement at subclass for sepzial store childs handling
138      *
139      * @param aWriter
140      * @param indent
141      * @param aElement
142      * @param elementDesc
143      */

144     public void storeChilds(PrintWriter JavaDoc aWriter, int indent, Object JavaDoc aElement,
145             StoreDescription elementDesc) throws Exception JavaDoc {
146     }
147
148     /**
149      * Store only elements from storeChilds methods that are not a transient
150      * child.
151      *
152      * @param aWriter
153      * @param indent
154      * @param aTagElement
155      * @throws Exception
156      */

157     protected void storeElement(PrintWriter JavaDoc aWriter, int indent,
158             Object JavaDoc aTagElement) throws Exception JavaDoc {
159         if (aTagElement != null) {
160             IStoreFactory elementFactory = getRegistry().findStoreFactory(
161                     aTagElement.getClass());
162
163             if (elementFactory != null) {
164                 StoreDescription desc = getRegistry().findDescription(
165                         aTagElement.getClass());
166                 if (!desc.isTransientChild(aTagElement.getClass().getName()))
167                     elementFactory.store(aWriter, indent, aTagElement);
168             } else {
169                 log.warn(sm.getString("factory.storeNoDescriptor", aTagElement
170                         .getClass()));
171             }
172         }
173     }
174
175     /*
176      * Save a array of elements @param aWriter @param indent @param elements
177      */

178     protected void storeElementArray(PrintWriter JavaDoc aWriter, int indent,
179             Object JavaDoc[] elements) throws Exception JavaDoc {
180         if (elements != null) {
181             for (int i = 0; i < elements.length; i++) {
182                 storeElement(aWriter, indent, elements[i]);
183             }
184         }
185     }
186 }
Popular Tags