KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dvdconvertor > DVDConvertor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.dvdconvertor;
21
22 import java.io.FileInputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import org.netbeans.api.convertor.ConvertorDescriptor;
27 import org.netbeans.spi.convertor.Convertor;
28 import org.netbeans.api.convertor.dvd.DVD;
29 import org.w3c.dom.Document JavaDoc;
30 import org.w3c.dom.Element JavaDoc;
31 import org.w3c.dom.Node JavaDoc;
32 import org.w3c.dom.NodeList JavaDoc;
33 import org.w3c.dom.Text JavaDoc;
34 import org.xml.sax.ErrorHandler JavaDoc;
35 import org.xml.sax.InputSource JavaDoc;
36 import org.xml.sax.SAXException JavaDoc;
37
38
39 /**
40  *
41  * @author David Konecny
42  */

43 public class DVDConvertor implements Convertor {
44
45     private static final String JavaDoc NAMESPACE = "http://www.netbeans.org/ns/dvd";
46     
47     public DVDConvertor() {
48     }
49
50     public Object JavaDoc read(org.w3c.dom.Element JavaDoc element) {
51         int id = 0;
52         String JavaDoc title = null;
53         String JavaDoc publisher = null;
54         int price = 0;
55         // assert element == <dvd>
56
NodeList JavaDoc nodes = element.getChildNodes();
57         for (int i = 0; i < nodes.getLength(); i++) {
58             Node JavaDoc node = nodes.item(i);
59             if (node.getNodeType() == Node.ELEMENT_NODE) {
60                 Element JavaDoc e = (Element JavaDoc)node;
61                 if (e.getNodeName().equals("id")) {
62                     id = Integer.parseInt(getTextValue(e));
63                 }
64                 if (e.getNodeName().equals("title")) {
65                     title = getTextValue(e);
66                 }
67                 if (e.getNodeName().equals("publisher")) {
68                     publisher = getTextValue(e);
69                 }
70                 if (e.getNodeName().equals("price")) {
71                     price = Integer.parseInt(getTextValue(e));
72                 }
73             }
74         }
75         return new DVD(id, title, publisher, price);
76     }
77     
78     public org.w3c.dom.Element JavaDoc write(Document JavaDoc doc, Object JavaDoc inst) {
79         DVD dvd = (DVD)inst;
80         Element JavaDoc element = doc.createElementNS(NAMESPACE, "dvd");
81         
82         Element JavaDoc e = doc.createElementNS(NAMESPACE, "id");
83         Text JavaDoc t = doc.createTextNode(Integer.toString(dvd.ID));
84         e.appendChild(t);
85         element.appendChild(e);
86         
87         e = doc.createElementNS(NAMESPACE, "title");
88         t = doc.createTextNode(dvd.title);
89         e.appendChild(t);
90         element.appendChild(e);
91         
92         e = doc.createElementNS(NAMESPACE, "publisher");
93         t = doc.createTextNode(dvd.publisher);
94         e.appendChild(t);
95         element.appendChild(e);
96         
97         e = doc.createElementNS(NAMESPACE, "price");
98         t = doc.createTextNode(Integer.toString(dvd.price));
99         e.appendChild(t);
100         element.appendChild(e);
101         
102         return element;
103     }
104
105     // THIS IMPL IS NOT ROBUST. DO NOT REUSE IT!
106
private String JavaDoc getTextValue(Element JavaDoc element) {
107         NodeList JavaDoc nodes = element.getChildNodes();
108         for (int i = 0; i < nodes.getLength(); i++) {
109             Node JavaDoc node = nodes.item(i);
110             if (node.getNodeType() == Node.TEXT_NODE) {
111                 return ((Text JavaDoc)node).getData();
112             }
113         }
114         return null;
115     }
116     
117 }
118
Popular Tags