001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.sun.manager.jbi.util;
043:
044: import javax.xml.xpath.XPathConstants;
045: import javax.xml.xpath.XPathFactory;
046: import org.w3c.dom.Document;
047: import org.w3c.dom.Element;
048: import org.w3c.dom.Node;
049: import org.w3c.dom.NodeList;
050: import org.xml.sax.InputSource;
051: import org.xml.sax.SAXException;
052: import org.xml.sax.EntityResolver;
053: import java.io.File;
054: import java.io.IOException;
055: import java.net.URL;
056: import java.util.ArrayList;
057: import java.util.List;
058: import javax.xml.parsers.DocumentBuilder;
059: import javax.xml.parsers.DocumentBuilderFactory;
060: import javax.xml.xpath.XPath;
061: import org.openide.filesystems.FileObject;
062: import org.openide.filesystems.Repository;
063:
064: /**
065: * Reader for NetBeans server config file.
066: *
067: * @author jqian
068: */
069: public class ServerInstanceReader {
070:
071: private static final String FILE_SEPARATOR = System
072: .getProperty("file.separator");
073: public static final String RELATIVE_FILE_PATH = FILE_SEPARATOR
074: + "config" + // NOI18N
075: FILE_SEPARATOR + "J2EE" + // NOI18N
076: FILE_SEPARATOR + "InstalledServers" + // NOI18N
077: FILE_SEPARATOR + ".nbattrs"; // NOI18N
078:
079: private static final String NB_DEFAULT_ATTRS_DTD = "xml/entities/NetBeans/DTD_DefaultAttributes_1_0"; // NOI18N
080: private static final String NB_DEFAULT_ATTRS_PUBLIC_ID = "-//NetBeans//DTD DefaultAttributes 1.0//EN"; // NOI18N
081:
082: private static final String LOCAL_NB_DEFAULT_ATTRS_DTD = "NetBeansDefaultAttrs_1_0.dtd";
083:
084: private List<ServerInstance> instances;
085: private String fileName;
086:
087: /**
088: * Creates a new ServerInstanceReader object.
089: *
090: * @param fileName
091: */
092: public ServerInstanceReader(String fileName) {
093: assert fileName != null;
094: this .fileName = fileName.replace('\\', '/'); // NOI18N
095: assert new File(fileName).exists() : "Server config file "
096: + fileName + " doesn't exist.";
097: }
098:
099: /**
100: * Gets a list of server instances in the server config file.
101: */
102: public List<ServerInstance> getServerInstances() {
103: if (instances == null) {
104: instances = new ArrayList<ServerInstance>();
105:
106: try {
107: XPath xpath = XPathFactory.newInstance().newXPath();
108: DocumentBuilderFactory factory = DocumentBuilderFactory
109: .newInstance();
110: DocumentBuilder builder = factory.newDocumentBuilder();
111:
112: builder.setEntityResolver(new EntityResolver() {
113: public InputSource resolveEntity(String publicID,
114: String systemID) throws SAXException,
115: IOException {
116: if (NB_DEFAULT_ATTRS_PUBLIC_ID.equals(publicID)) {
117: FileObject file = Repository.getDefault()
118: .getDefaultFileSystem()
119: .findResource(NB_DEFAULT_ATTRS_DTD);
120: if (file != null) {
121: return new InputSource(file
122: .getInputStream());
123: } else { // command line support for offline ATS
124: URL url = getClass().getResource(
125: LOCAL_NB_DEFAULT_ATTRS_DTD);
126: return new InputSource(url.openStream());
127: }
128: }
129:
130: // use the default behavior
131: return null;
132: }
133: });
134: Document document = builder.parse(new File(fileName));
135:
136: NodeList nodes = (NodeList) xpath.evaluate(
137: "/attributes/fileobject", document, // NOI18N
138: XPathConstants.NODESET);
139:
140: for (int i = 0; i < nodes.getLength(); i++) {
141: Node node = nodes.item(i);
142: NodeList childNodes = node.getChildNodes();
143:
144: ServerInstance instance = new ServerInstance();
145:
146: for (int j = 0; j < childNodes.getLength(); j++) {
147: Node childNode = childNodes.item(j);
148: String childNodeName = childNode.getNodeName();
149:
150: if ((childNode.getNodeType() == Node.ELEMENT_NODE)
151: && (childNodeName
152: .equalsIgnoreCase("attr"))) { // NOI18N
153: Element attrElement = (Element) childNode;
154:
155: String key = attrElement
156: .getAttribute("name"); // NOI18N
157: String value = attrElement
158: .getAttribute("stringvalue"); // NOI18N
159:
160: if (key
161: .equalsIgnoreCase(ServerInstance.DISPLAY_NAME)) {
162: instance.setDisplayName(value);
163: } else if (key
164: .equalsIgnoreCase(ServerInstance.DOMAIN)) {
165: instance.setDomain(value);
166: } else if (key
167: .equalsIgnoreCase(ServerInstance.HTTP_MONITOR_ON)) {
168: instance.setHttpMonitorOn(value);
169: } else if (key
170: .equalsIgnoreCase(ServerInstance.HTTP_PORT_NUMBER)) {
171: instance.setHttpPortNumber(value);
172: } else if (key
173: .equalsIgnoreCase(ServerInstance.LOCATION)) {
174: instance.setLocation(value);
175: } else if (key
176: .equalsIgnoreCase(ServerInstance.PASSWORD)) {
177: instance.setPassword(value);
178: } else if (key
179: .equalsIgnoreCase(ServerInstance.URL)) {
180: instance.setUrl(value);
181: } else if (key
182: .equalsIgnoreCase(ServerInstance.USER_NAME)) {
183: instance.setUserName(value);
184: }
185: }
186: }
187:
188: instances.add(instance);
189: }
190: } catch (Exception e) {
191: e.printStackTrace(System.err);
192: }
193: }
194:
195: return instances;
196: }
197:
198: /**
199: * DOCUMENT ME!
200: *
201: * @param args DOCUMENT ME!
202: */
203: public static void main(String[] args) {
204: ServerInstanceReader settings = null;
205:
206: try {
207: settings = new ServerInstanceReader(
208: "C:/Documents and Settings/Graj/.netbeans/dev" + // NOI18N
209: ServerInstanceReader.RELATIVE_FILE_PATH);
210:
211: List list = settings.getServerInstances();
212: java.util.Iterator iterator = list.iterator();
213:
214: while (iterator.hasNext() == true) {
215: ServerInstance instance = (ServerInstance) iterator
216: .next();
217: instance.printOut();
218: }
219: } catch (Exception e) {
220: e.printStackTrace();
221: }
222: }
223: }
|