KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > config > node > file > AbstractFileConfigurationDocumentFactory


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.file;
19
20 import java.io.File JavaDoc;
21
22 import org.sape.carbon.core.config.format.ConfigurationFormatService;
23 import org.sape.carbon.core.config.format.DefaultConfigurationFormatService;
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.NodeFactory;
27 import org.sape.carbon.core.exception.InvalidParameterException;
28
29
30 /**
31  * Base class for factories that construct
32  * <code>FileConfigurationDocument</code>s.
33  *
34  * Copyright 2002 Sapient
35  * @since carbon 1.0
36  * @author Douglas Voet, March 2002
37  * @version $Revision: 1.12 $($Author: dvoet $ / $Date: 2003/05/05 21:21:19 $)
38  */

39 public abstract class AbstractFileConfigurationDocumentFactory
40     implements NodeFactory {
41
42     /** Default formating service. */
43     private static final ConfigurationFormatService FORMAT_SERVICE =
44         new DefaultConfigurationFormatService();
45
46     /**
47      * Creates FileConfigurationDocument objects only as children
48      * of FileFolders.
49      *
50      * @param parent the parent node of this document factory
51      * @param name name of the file to construct a document for
52      * @return a node representing the requested document
53      * @throws InvalidParameterException if parent is not
54      * assignable from FileFolder
55      * @throws NodeCreationException indicates an error loading
56      * the node from file
57      * @see NodeFactory#getInstance
58      */

59     public Node getInstance(Node parent, String JavaDoc name)
60         throws NodeCreationException {
61
62         FileFolder parentFileFolder;
63         try {
64             parentFileFolder = (FileFolder) parent;
65         } catch (ClassCastException JavaDoc cce) {
66             throw new InvalidParameterException(
67                 this.getClass(),
68                 "parent is not assignable from FileFolder", cce);
69         }
70
71         // need to explicitly check for null, otherwise the following
72
// code will create a null.xml file
73
if (name == null) {
74             throw new InvalidParameterException(
75                 this.getClass(),
76                 "name cannot be null");
77         }
78
79         File JavaDoc file = new File JavaDoc(parentFileFolder.getInternalFileObject(),
80             name + getFileExtension());
81
82 // if (!file.exists()) {
83
// try {
84
// file.createNewFile();
85
// } catch (IOException ioe) {
86
// throw new NodeCreationException(
87
// this.getClass(),
88
// parent, name, "Could not create file", ioe);
89
// }
90
// }
91

92         return new FileConfigurationDocument(
93             parent,
94             name,
95             getConfigurationFormatService(),
96             file);
97     }
98
99     /**
100      * Gets the file extension for the node being created by the factory.
101      * The extension is used to identify the type of node contained
102      * within the document (ConfigurationDocument or LinkNode at this time).
103      *
104      * @return String the file extension starting with a '.'
105      */

106     protected abstract String JavaDoc getFileExtension();
107
108     /**
109      * Method that determines which ConfigurationFormatService will be used
110      * within the ConfigurationDocuments of the file package. The default
111      * implementation returns a static instance of
112      * org.sape.carbon.core.config.format.DefaultConfigurationFormatService.
113      * Override this method to
114      * change the ConfigurationFormatService used by the
115      * FileConfigurationDocument.
116      *
117      * @return ConfigurationFormatService
118      */

119     protected ConfigurationFormatService getConfigurationFormatService() {
120         return AbstractFileConfigurationDocumentFactory.FORMAT_SERVICE;
121     }
122 }
Popular Tags