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 General
007: * Public License Version 2 only ("GPL") or the Common Development and Distribution
008: * License("CDDL") (collectively, the "License"). You may not use this file except in
009: * compliance with the License. You can obtain a copy of the License at
010: * http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the
011: * License for the specific language governing permissions and limitations under the
012: * License. When distributing the software, include this License Header Notice in
013: * each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun
014: * designates this particular file as subject to the "Classpath" exception as
015: * provided by Sun in the GPL Version 2 section of the License file that
016: * accompanied this code. If applicable, add the following below the License Header,
017: * with the fields enclosed by brackets [] replaced by your own identifying
018: * information: "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Contributor(s):
021: *
022: * The Original Software is NetBeans. The Initial Developer of the Original Software
023: * is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun Microsystems, Inc. All
024: * Rights Reserved.
025: *
026: * If you wish your version of this file to be governed by only the CDDL or only the
027: * GPL Version 2, indicate your decision by adding "[Contributor] elects to include
028: * this software in this distribution under the [CDDL or GPL Version 2] license." If
029: * you do not indicate a single choice of license, a recipient has the option to
030: * distribute your version of this file under either the CDDL, the GPL Version 2 or
031: * to extend the choice of license to its licensees as provided above. However, if
032: * you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then
033: * the option applies only if the new code is made subject to such option by the
034: * copyright holder.
035: */
036:
037: package org.util;
038:
039: import java.util.LinkedList;
040: import java.util.List;
041:
042: import org.MyTestCase;
043: import org.netbeans.installer.utils.exceptions.ParseException;
044: import org.netbeans.installer.utils.xml.DomExternalizable;
045: import org.netbeans.installer.utils.xml.DomUtil;
046: import org.netbeans.installer.utils.xml.visitors.DomVisitor;
047: import org.netbeans.installer.utils.xml.visitors.RecursiveDomVisitor;
048: import org.w3c.dom.Document;
049: import org.w3c.dom.Element;
050:
051: /**
052: *
053: * @author Danila_Dugurov
054: */
055:
056: public class DomVisitorTest extends MyTestCase {
057:
058: public void testSimpleVisitor() {
059: MyComponent component1 = new MyComponent("one", "leaf", 10);
060: MyComponent component2 = new MyComponent("two", "composite", 20);
061: MyComponent component3 = new MyComponent("three", "composite",
062: 30);
063: MyComponent component4 = new MyComponent("four", "leaf", 40);
064: MyComponent component5 = new MyComponent("five", "leaf", 50);
065: final List<MyComponent> deserialized = new LinkedList<MyComponent>();
066: try {
067: final Document document = DomUtil
068: .parseXmlFile("<components/>");
069: Element root = document.getDocumentElement();
070:
071: DomUtil.addChild(root, component1);
072: DomUtil.addChild(root, component2);
073: DomUtil.addChild(root, component3);
074: DomUtil.addChild(root, component4);
075: DomUtil.addChild(root, component5);
076:
077: DomVisitor visitor = new RecursiveDomVisitor() {
078: public void visit(Element element) {
079: if ("component".equals(element.getNodeName())) {
080: final MyComponent component = new MyComponent();
081: component.readXML(element);
082: deserialized.add(component);
083: } else
084: super .visit(element);
085: }
086: };
087: visitor.visit(document);
088: } catch (ParseException wontHappend) {
089: }
090: assertEquals(component1, deserialized.get(0));
091: assertEquals(component2, deserialized.get(1));
092: assertEquals(component3, deserialized.get(2));
093: assertEquals(component4, deserialized.get(3));
094: assertEquals(component5, deserialized.get(4));
095: System.out.println(component1);
096: System.out.println(component2);
097: System.out.println(component3);
098: System.out.println(component4);
099: System.out.println(component5);
100: }
101: }
102:
103: class MyComponent implements DomExternalizable {
104: String name;
105:
106: String type;
107:
108: int cost;
109:
110: public MyComponent(String name, String type, int cost) {
111: this .name = name;
112: this .type = type;
113: this .cost = cost;
114: }
115:
116: public MyComponent() {
117:
118: }
119:
120: public void readXML(Element element) {
121: final RecursiveDomVisitor visitor = new RecursiveDomVisitor() {
122: public void visit(Element element) {
123: if ("name".equals(element.getNodeName())) {
124: name = element.getTextContent();
125: } else if ("type".equals(element.getNodeName())) {
126: type = element.getTextContent();
127: } else if ("cost".equals(element.getNodeName())) {
128: cost = Integer.parseInt(element.getTextContent());
129: } else
130: super .visit(element);
131: }
132: };
133: visitor.visit(element);
134: }
135:
136: public Element writeXML(Document document) {
137: final Element root = document.createElement("component");
138: Element element = document.createElement("name");
139: element.setTextContent(name);
140: root.appendChild(element);
141: element = document.createElement("type");
142: element.setTextContent(type);
143: root.appendChild(element);
144: element = document.createElement("cost");
145: element.setTextContent(String.valueOf(cost));
146: root.appendChild(element);
147: return root;
148: }
149:
150: public String toString() {
151: return "name: " + name + " type: " + type + " cost: " + cost;
152: }
153:
154: @Override
155: public boolean equals(Object other) {
156: if (other == null)
157: return false;
158: if (other instanceof MyComponent) {
159: final MyComponent component = (MyComponent) other;
160: return component.name.equals(this .name)
161: && component.type.equals(this .type)
162: && component.cost == this .cost;
163: }//TODO: nullPointer he he.
164: return false;
165: }
166:
167: @Override
168: public int hashCode() {
169: return -1;//TODO: he-he
170: }
171:
172: }
|