KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > project > DataMapProject


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.project;
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.apache.cayenne.conf.ConfigStatus;
29 import org.apache.cayenne.map.DataMap;
30 import org.apache.cayenne.map.MapLoader;
31 import org.xml.sax.InputSource JavaDoc;
32
33 /**
34  * Cayenne project that consists of a single DataMap.
35  *
36  * @author Andrus Adamchik
37  */

38 public class DataMapProject extends Project {
39
40     protected DataMap map;
41
42     /**
43      * Constructor for MapProject.
44      *
45      * @param projectFile
46      */

47     public DataMapProject(File JavaDoc projectFile) {
48         super(projectFile);
49     }
50
51     /**
52      * @since 1.1
53      */

54     public void upgrade() throws ProjectException {
55         // upgrades not supported in this type of project
56
throw new ProjectException("'DataMapProject' does not support upgrades.");
57     }
58
59     /**
60      * Does nothing.
61      */

62     public void checkForUpgrades() {
63         // do nothing
64
}
65
66     /**
67      * Initializes internal <code>map</code> object and then calls super.
68      */

69     protected void postInitialize(File JavaDoc projectFile) {
70         if (projectFile != null) {
71             try {
72                 InputStream JavaDoc in = new FileInputStream JavaDoc(projectFile.getCanonicalFile());
73                 map = new MapLoader().loadDataMap(new InputSource JavaDoc(in));
74
75                 String JavaDoc fileName = resolveSymbolicName(projectFile);
76                 String JavaDoc mapName = (fileName != null && fileName
77                         .endsWith(DataMapFile.LOCATION_SUFFIX))
78                         ? fileName.substring(0, fileName.length()
79                                 - DataMapFile.LOCATION_SUFFIX.length())
80                         : "UntitledMap";
81
82                 map.setName(mapName);
83             }
84             catch (Exception JavaDoc dme) {
85                 throw new ProjectException(
86                         "Error creating " + this.getClass().getName(),
87                         dme);
88             }
89         }
90         else {
91             map = (DataMap) NamedObjectFactory.createObject(DataMap.class, null);
92         }
93
94         super.postInitialize(projectFile);
95     }
96
97     /**
98      * Returns a list that contains project DataMap as a single object.
99      */

100     public List JavaDoc getChildren() {
101         List JavaDoc entities = new ArrayList JavaDoc();
102         entities.add(map);
103         return entities;
104     }
105
106     /**
107      * Returns appropriate ProjectFile or null if object does not require a file of its
108      * own. In case of DataMapProject, the only object that requires a file is the project
109      * itself.
110      */

111     public ProjectFile projectFileForObject(Object JavaDoc obj) {
112         if (obj == this) {
113             return new DataMapFile(this, map);
114         }
115
116         return null;
117     }
118
119     /**
120      * Always returns empty status. Map projects do not support status tracking yet.
121      */

122     public ConfigStatus getLoadStatus() {
123         return new ConfigStatus();
124     }
125 }
126
Popular Tags