KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > config > node > link > GenericLinkNodeFactory


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.link;
19
20 import org.sape.carbon.core.config.Config;
21 import org.sape.carbon.core.config.format.ConfigurationFormatException;
22 import org.sape.carbon.core.config.node.ConfigurationDocument;
23 import org.sape.carbon.core.config.node.Folder;
24 import org.sape.carbon.core.config.node.Node;
25 import org.sape.carbon.core.config.node.NodeCreationException;
26 import org.sape.carbon.core.config.node.NodeIOException;
27 import org.sape.carbon.core.config.node.NodeNotFoundException;
28
29 /**
30  * Factory for creating links that point to other
31  * <code>Nodes</code> within the existing configuration <code>Node</code>
32  * hierarchy. These links are not specific to a backing
33  * data store.
34  *
35  * Copyright 2002 Sapient
36  * @since carbon 1.0
37  * @author Douglas Voet, April 2002
38  * @version $Revision: 1.5 $($Author: dvoet $ / $Date: 2003/05/05 21:21:19 $)
39  */

40 public class GenericLinkNodeFactory implements LinkNodeFactory {
41
42     /**
43      * This implementation will return either a <code>FolderLink</code>
44      * or a <code>ConfigurationDocumentLink</code> depending on the type of the
45      * target <code>Node</code>.
46      *
47      * @param parent the parent of the link node
48      * @param name the name of the link node
49      * @param linkConfigurationDoc must be an instanct of
50      * <code>GenericLinkNodeConfiguration</code>
51      * @return the instance of the link node
52      * @throws NodeCreationException indicates an error creating the link node
53      * @see LinkNodeFactory#getInstance
54      */

55     public LinkNode getInstance(
56         Node parent,
57         String JavaDoc name,
58         ConfigurationDocument linkConfigurationDoc)
59         throws NodeCreationException {
60
61         try {
62
63             GenericLinkNodeConfiguration linkConfiguration =
64                 (GenericLinkNodeConfiguration)
65                 linkConfigurationDoc.readConfiguration();
66
67             String JavaDoc targetNodeName = linkConfiguration.getTargetNodeName();
68
69             if (!targetNodeName.startsWith(String.valueOf(Node.DELIMITER))) {
70                 // relative link
71
targetNodeName = parent.getAbsoluteName()
72                                  + Node.DELIMITER
73                                  + targetNodeName;
74             }
75
76             Node targetNode = Config.getInstance().fetchNode(targetNodeName);
77
78             if (targetNode instanceof Folder) {
79                 return new FolderLinkImpl(linkConfigurationDoc,
80                     (Folder) targetNode);
81
82             } else if (targetNode instanceof ConfigurationDocument) {
83                 return new ConfigurationDocumentLinkImpl(linkConfigurationDoc,
84                     (ConfigurationDocument) targetNode);
85
86             } else {
87                 throw new NodeCreationException(
88                     this.getClass(),
89                     parent, name,
90                     "target node was of an unknown type");
91             }
92
93         } catch (ClassCastException JavaDoc cce) {
94             throw new NodeCreationException(
95                 this.getClass(),
96                 parent, name,
97                 "linkConfigurationDoc did not contain a "
98                     + "GenericLinkNodeConfiguration", cce);
99
100         } catch (NodeIOException nioe) {
101             throw new NodeCreationException(
102                 this.getClass(),
103                 parent, name,
104                 "problem reading GenericLinkNodeConfiguration", nioe);
105
106         } catch (ConfigurationFormatException cfe) {
107             throw new NodeCreationException(
108                 this.getClass(),
109                 parent, name,
110                 "problem reading GenericLinkNodeConfiguration", cfe);
111
112         } catch (NodeNotFoundException nnfe) {
113             throw new NodeCreationException(
114                 this.getClass(),
115                 parent, name,
116                 "target node was not found", nnfe);
117         }
118     }
119
120 }
121
Popular Tags