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.compapp.javaee.sunresources.tool.graph;
043:
044: import java.awt.Point;
045: import java.io.File;
046: import java.io.FileOutputStream;
047: import java.io.PrintWriter;
048: import java.util.Collections;
049: import java.util.List;
050:
051: import javax.xml.bind.JAXBContext;
052: import javax.xml.bind.JAXBElement;
053: import javax.xml.bind.Marshaller;
054: import javax.xml.bind.Unmarshaller;
055:
056: import org.netbeans.modules.compapp.javaee.sunresources.generated.graph.GraphType;
057: import org.netbeans.modules.compapp.javaee.sunresources.generated.graph.ObjectFactory;
058: import org.netbeans.modules.compapp.javaee.sunresources.tool.archive.FileUtil;
059:
060: /**
061: * @author echou
062: *
063: */
064: public class JAXBHandler {
065:
066: private JAXBContext jc;
067: private JAXBElement<?> graphElement;
068: private List<GraphType.Node> nodeList;
069: private Marshaller marshaller;
070: private File xmlFile;
071:
072: public JAXBHandler(File xmlFile) throws Exception {
073: this .xmlFile = xmlFile;
074:
075: jc = JAXBContext.newInstance("com.sun.wasilla.jaxb.graph", // NOI18N
076: this .getClass().getClassLoader());
077:
078: boolean unmarshalSuccess = true;
079: // unmarshal it to memory
080: try {
081: Unmarshaller unmarshaller = jc.createUnmarshaller();
082: graphElement = (JAXBElement<?>) unmarshaller
083: .unmarshal(xmlFile);
084: nodeList = Collections
085: .synchronizedList(((GraphType) graphElement
086: .getValue()).getNode());
087: } catch (Exception e) {
088: unmarshalSuccess = false;
089: }
090:
091: // if unable to marshal xml, then create a new empty one
092: if (!unmarshalSuccess) {
093: // construct JAXB tree
094: ObjectFactory factory = new ObjectFactory();
095: GraphType graph = factory.createGraphType();
096: graphElement = factory.createGraph(graph);
097: nodeList = Collections
098: .synchronizedList(((GraphType) graphElement
099: .getValue()).getNode());
100:
101: }
102:
103: }
104:
105: public GraphType.Node findNode(String logicalname) {
106: for (int i = 0; i < nodeList.size(); i++) {
107: GraphType.Node curNode = nodeList.get(i);
108: if (logicalname.equals(curNode.getLogicalname())) {
109: return curNode;
110: }
111: }
112:
113: return null;
114: }
115:
116: public void addNode(GraphType.Node node) {
117: nodeList.add(node);
118: }
119:
120: public void updateNode(String logicalname, Point p) {
121: GraphType.Node node = findNode(logicalname);
122: if (node == null) {
123: node = new GraphType.Node();
124: node.setLogicalname(logicalname);
125: node.setLocX(String.valueOf(p.x));
126: node.setLocY(String.valueOf(p.y));
127: addNode(node);
128: } else {
129: node.setLocX(String.valueOf(p.x));
130: node.setLocY(String.valueOf(p.y));
131: }
132: }
133:
134: public void saveXML() throws Exception {
135: if (marshaller == null) {
136: marshaller = jc.createMarshaller();
137: marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
138: Boolean.TRUE);
139: marshaller.setProperty("com.sun.xml.bind.xmlDeclaration",
140: Boolean.FALSE); // NOI18N
141: }
142: // write the prolog here
143: FileOutputStream fos = new FileOutputStream(xmlFile);
144: PrintWriter out = new PrintWriter(fos);
145: try {
146: out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); // NOI18N
147: out
148: .println("<!DOCTYPE resources PUBLIC \"-//Sun Microsystems Inc.//DTD Application Server 9.0 Domain//EN\" \"http://www.sun.com/software/appserver/dtds/sun-resources_1_3.dtd\">"); // NOI18N
149: marshaller.marshal(graphElement, out);
150: } finally {
151: FileUtil.safeclose(out);
152: FileUtil.safeclose(fos);
153: }
154: }
155:
156: /*
157: * test of this class
158: */
159: public static void main(String[] args) {
160: try {
161: String xmlFileName = "C:/testcode/wasilla/graph2.xml"; // NOI18N
162: File xmlFile = new File(xmlFileName);
163: JAXBHandler h = new JAXBHandler(xmlFile);
164: h.saveXML();
165: } catch (Exception e) {
166: e.printStackTrace();
167: }
168: }
169: }
|