KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > resources > WorkspaceDescriptionReader


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.resources;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.List JavaDoc;
17 import javax.xml.parsers.*;
18 import org.eclipse.core.internal.localstore.SafeFileInputStream;
19 import org.eclipse.core.internal.utils.Messages;
20 import org.eclipse.core.internal.utils.Policy;
21 import org.eclipse.core.resources.IResourceStatus;
22 import org.eclipse.core.runtime.IPath;
23 import org.eclipse.osgi.util.NLS;
24 import org.w3c.dom.*;
25 import org.xml.sax.SAXException JavaDoc;
26
27 /**
28  * This class contains legacy code only. It is being used to read workspace
29  * descriptions which are obsolete.
30  */

31 public class WorkspaceDescriptionReader implements IModelObjectConstants {
32     /** constants */
33     protected static final String JavaDoc[] EMPTY_STRING_ARRAY = new String JavaDoc[0];
34
35     public WorkspaceDescriptionReader() {
36         super();
37     }
38
39     protected String JavaDoc getString(Node target, String JavaDoc tagName) {
40         Node node = searchNode(target, tagName);
41         return node != null ? (node.getFirstChild() == null ? null : node.getFirstChild().getNodeValue()) : null;
42     }
43
44     protected String JavaDoc[] getStrings(Node target) {
45         if (target == null)
46             return null;
47         NodeList list = target.getChildNodes();
48         if (list.getLength() == 0)
49             return EMPTY_STRING_ARRAY;
50         List JavaDoc result = new ArrayList JavaDoc(list.getLength());
51         for (int i = 0; i < list.getLength(); i++) {
52             Node node = list.item(i);
53             if (node.getNodeType() == Node.ELEMENT_NODE)
54                 result.add(read(node.getChildNodes().item(0)));
55         }
56         return (String JavaDoc[]) result.toArray(new String JavaDoc[result.size()]);
57     }
58
59     /**
60      * A value was discovered in the workspace description file that was not a number.
61      * Log the exception.
62      */

63     private void logNumberFormatException(String JavaDoc value, NumberFormatException JavaDoc e) {
64         String JavaDoc msg = NLS.bind(Messages.resources_readWorkspaceMetaValue, value);
65         Policy.log(new ResourceStatus(IResourceStatus.FAILED_READ_METADATA, null, msg, e));
66     }
67
68     public Object JavaDoc read(InputStream JavaDoc input) {
69         try {
70             DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
71             Document document = parser.parse(input);
72             return read(document.getFirstChild());
73         } catch (IOException JavaDoc e) {
74             // ignore
75
} catch (SAXException JavaDoc e) {
76             // ignore
77
} catch (ParserConfigurationException e) {
78             // ignore
79
}
80         return null;
81     }
82
83     public Object JavaDoc read(IPath location, IPath tempLocation) throws IOException JavaDoc {
84         SafeFileInputStream file = new SafeFileInputStream(location.toOSString(), tempLocation.toOSString());
85         try {
86             return read(file);
87         } finally {
88             file.close();
89         }
90     }
91
92     protected Object JavaDoc read(Node node) {
93         if (node == null)
94             return null;
95         switch (node.getNodeType()) {
96             case Node.ELEMENT_NODE :
97                 if (node.getNodeName().equals(WORKSPACE_DESCRIPTION))
98                     return readWorkspaceDescription(node);
99             case Node.TEXT_NODE :
100                 String JavaDoc value = node.getNodeValue();
101                 return value == null ? null : value.trim();
102             default :
103                 return node.toString();
104         }
105     }
106
107     /**
108      * read (String, String) hashtables
109      */

110     protected WorkspaceDescription readWorkspaceDescription(Node node) {
111         // get values
112
String JavaDoc name = getString(node, NAME);
113         String JavaDoc autobuild = getString(node, AUTOBUILD);
114         String JavaDoc snapshotInterval = getString(node, SNAPSHOT_INTERVAL);
115         String JavaDoc fileStateLongevity = getString(node, FILE_STATE_LONGEVITY);
116         String JavaDoc maxFileStateSize = getString(node, MAX_FILE_STATE_SIZE);
117         String JavaDoc maxFileStates = getString(node, MAX_FILE_STATES);
118         String JavaDoc[] buildOrder = getStrings(searchNode(node, BUILD_ORDER));
119
120         // build instance
121
//invalid values are skipped and defaults are used instead
122
WorkspaceDescription description = new WorkspaceDescription(name);
123         if (autobuild != null)
124             //if in doubt (value is corrupt) we want autobuild on
125
description.setAutoBuilding(!autobuild.equals(Integer.toString(0)));
126         try {
127             if (fileStateLongevity != null)
128                 description.setFileStateLongevity(Long.parseLong(fileStateLongevity));
129         } catch (NumberFormatException JavaDoc e) {
130             logNumberFormatException(fileStateLongevity, e);
131         }
132         try {
133             if (maxFileStateSize != null)
134                 description.setMaxFileStateSize(Long.parseLong(maxFileStateSize));
135         } catch (NumberFormatException JavaDoc e) {
136             logNumberFormatException(maxFileStateSize, e);
137         }
138         try {
139             if (maxFileStates != null)
140                 description.setMaxFileStates(Integer.parseInt(maxFileStates));
141         } catch (NumberFormatException JavaDoc e) {
142             logNumberFormatException(maxFileStates, e);
143         }
144         if (buildOrder != null)
145             description.internalSetBuildOrder(buildOrder);
146         try {
147             if (snapshotInterval != null)
148                 description.setSnapshotInterval(Long.parseLong(snapshotInterval));
149         } catch (NumberFormatException JavaDoc e) {
150             logNumberFormatException(snapshotInterval, e);
151         }
152         return description;
153     }
154
155     protected Node searchNode(Node target, String JavaDoc tagName) {
156         NodeList list = target.getChildNodes();
157         for (int i = 0; i < list.getLength(); i++) {
158             if (list.item(i).getNodeName().equals(tagName))
159                 return list.item(i);
160         }
161         return null;
162     }
163 }
164
Popular Tags