KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > config > node > NestedConfigurationDocumentFactory


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.core.config.node;
19
20 import org.sape.carbon.core.config.Config;
21 import org.sape.carbon.core.config.Configuration;
22 import org.sape.carbon.core.config.format.ConfigurationFormatException;
23
24 /**
25  * Factory for creating NestedConfigurationDocument nodes
26  * <p>
27  * Note that this implementation of nested nodes can only be used in
28  * conjunction with nodes that extend the abstract classes found in this
29  * package. If you wish to provide an alternate set of Node implementations,
30  * you must provide an alternate nested node implementation as well (that is,
31  * if you wish to support nested nodes). This is mainly because of the
32  * synchronization mechanism used: nested nodes use their parent's lock objects
33  * to ensure the thread-safety of the overall document (i.e. the containing
34  * document plus all of its nested documents).
35  *
36  * Copyright 2002 Sapient
37  * @since carbon 1.1
38  * @author Douglas Voet, Sep 13, 2002
39  * @version $Revision: 1.10 $($Author: dvoet $ / $Date: 2003/10/17 14:40:55 $)
40  */

41 public class NestedConfigurationDocumentFactory implements NodeFactory {
42
43     /**
44      * @see org.sape.carbon.core.config.node.NodeFactory#getInstance(Node, String)
45      */

46     public Node getInstance(Node parent, String JavaDoc name)
47         throws NodeCreationException {
48
49         try {
50
51             AbstractConfigurationDocument parentDoc =
52                 (AbstractConfigurationDocument) parent;
53             Configuration parentConfig = parentDoc.readConfiguration();
54             
55             Configuration childConfig =
56                 parentDoc.getFormatService().getChildConfiguration(
57                     parentConfig,
58                     name);
59                 
60             ConfigurationDocument nestedDoc;
61             
62             if (childConfig != null) {
63                 String JavaDoc childConfigName = childConfig.getConfigurationName();
64                 String JavaDoc parentConfigName = parentConfig.getConfigurationName();
65             
66                 if (!childConfigName.startsWith(parentConfigName + Node.DELIMITER)) {
67                     nestedDoc =
68                         (ConfigurationDocument) Config.getInstance().fetchNode(
69                             childConfigName);
70                 } else {
71                     nestedDoc = new NestedConfigurationDocument(parentDoc, name);
72                 }
73             } else {
74                 nestedDoc = new NestedConfigurationDocument(parentDoc, name);
75             }
76
77             return nestedDoc;
78
79         } catch (ClassCastException JavaDoc cce) {
80             throw new NodeCreationException(
81                 this.getClass(),
82                 parent,
83                 name,
84                 "parent must extend AbsractConfigurationDocument, it's class was ["
85                     + parent.getClass().getName()
86                     + "]",
87                 cce);
88
89         } catch (NodeIOException nioe) {
90             throw new NodeCreationException(
91                 this.getClass(),
92                 parent,
93                 name,
94                 "could not read configuration",
95                 nioe);
96
97         } catch (NodeNotFoundException nnfe) {
98             throw new NodeCreationException(
99                 this.getClass(),
100                 parent,
101                 name,
102                 "referenced node was not found",
103                 nnfe);
104
105         } catch (ConfigurationFormatException cfe) {
106             throw new NodeCreationException(
107                 this.getClass(),
108                 parent,
109                 name,
110                 "could not read configuration",
111                 cfe);
112         }
113     }
114
115     /** @link dependency */
116     /*#NestedConfigurationDocument lnkNestedConfigurationDocument;*/
117 }
Popular Tags