KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > jawe > xml > elements > SchemaType


1 /* SchemaType.java
2  *
3  * Authors:
4  * Stefanovic Nenad chupo@iis.ns.ac.yu
5  * Bojanic Sasa sasaboy@neobee.net
6  * Puskas Vladimir vpuskas@eunet.yu
7  * Pilipovic Goran zboniek@uns.ac.yu
8  *
9  */

10
11 package org.enhydra.jawe.xml.elements;
12
13 import org.enhydra.jawe.xml.*;
14 import org.enhydra.jawe.xml.panels.*;
15
16 import java.util.*;
17 import java.io.*;
18 import java.net.URL JavaDoc;
19 import javax.swing.*;
20 import javax.swing.event.*;
21
22 import javax.xml.parsers.*;
23 import javax.xml.transform.*;
24 import javax.xml.transform.dom.*;
25 import javax.xml.transform.stream.*;
26 import org.w3c.dom.*;
27 import org.xml.sax.InputSource JavaDoc;
28
29 /**
30  * Represents a WfMC DTD element that has the similar name.
31  * NOTE: This element is not implemented.
32  */

33 public class SchemaType extends XMLComplexElement {
34    // min=0, max=unbounded
35
//private Set refAny=new HashSet();
36

37    // helper attributes
38
private XMLAttribute attrHref;
39    private Content refContent=new Content();
40
41    private Node schemaNode;
42
43    public SchemaType () {
44       super();
45
46       attrHref=new XMLAttribute("href",1);
47
48       fillStructure();
49    }
50
51    /**
52     * Defines the super-class method. Read the explanation for
53     * this method within XMLComplexElement class.
54     */

55    protected void fillStructure () {
56       attrHref.setRequired(true);
57       complexStructure.add(attrHref);
58       refContent.setReadOnly(true);
59       complexStructure.add(refContent);
60    }
61
62    public void toXML (Node parent) throws DOMException {
63       // When saving to file, do not save the attribute that contains
64
// the location of schema, and it's description directly,
65
// but the schema content
66

67       // removes attrHref
68
complexStructure.remove(0);
69       // removes refDescription
70
complexStructure.remove(0);
71
72       // save to xml
73
if (parent!=null) {
74          Node node = (parent.getOwnerDocument()).createElement(name);
75          //node.appendChild(parent.getOwnerDocument().createTextNode(refContent.toString().trim()));
76
String JavaDoc fileLoc=attrHref.toString().trim();
77          Node schema=null;
78          if (fileLoc.length()>0) {
79             schema=parseDocument(attrHref.toString().trim());
80          } else if (refContent.toValue()!=null &&
81                     refContent.toValue().toString().length()>0) {
82             schema=schemaNode;
83          }
84          if (schema!=null) {
85             node.appendChild(parent.getOwnerDocument().importNode(schema,true));
86          }
87          parent.appendChild(node);
88       }
89
90       complexStructure.add(0,attrHref);
91       complexStructure.add(1,refContent);
92    }
93
94    public void fromXML(Node node) {
95       if (node!=null) {
96          Object JavaDoc newVal=null;
97          if (node.hasChildNodes()) {
98             //newVal=node.getChildNodes().item(0).getNodeValue();
99
NodeList nl=node.getChildNodes();
100             for (int i=0; i<nl.getLength(); i++) {
101                Node sn=nl.item(i);
102                //System.out.println("Class = "+nl.item(i).getClass().getName());
103
if (sn instanceof Element) {
104                   newVal=XMLUtil.getContent(sn,true);
105                   schemaNode=sn;
106                   break;
107                }
108             }
109             // should never happen
110
} else {
111             newVal=node.getNodeValue();
112          }
113          if (newVal!=null) {
114             value=newVal;
115          }
116       }
117       refContent.setValue(value);
118    }
119
120    public XMLPanel getPanel () {
121       XMLGroupPanel currentPanel=(XMLGroupPanel)super.getPanel();
122       XMLPanel cp=currentPanel.getPanel(1);
123       JScrollPane jsp=(JScrollPane)cp.getComponent(2);
124       JViewport jvp=(JViewport)jsp.getComponent(0);
125       final JTextArea currentContent=(JTextArea)jvp.getComponent(0);
126
127       XMLLocationPanel lp=(XMLLocationPanel)currentPanel.getPanel(0);
128       final JTextField jtf=(JTextField)lp.getComponent(2);
129       jtf.getDocument().addDocumentListener(new DocumentListener(){
130                public void changedUpdate(DocumentEvent ae){
131                   String JavaDoc fstr=XMLUtil.fileToString(jtf.getText());
132                   if (fstr==null) {
133                      fstr="";
134                   }
135                   currentContent.setText(fstr);
136                }
137                public void insertUpdate(DocumentEvent ae){
138                   String JavaDoc fstr=XMLUtil.fileToString(jtf.getText());
139                   if (fstr==null) {
140                      fstr="";
141                   }
142                   currentContent.setText(fstr);
143                }
144                public void removeUpdate(DocumentEvent ae){
145                   String JavaDoc fstr=XMLUtil.fileToString(jtf.getText());
146                   if (fstr==null) {
147                      fstr="";
148                   }
149                   currentContent.setText(fstr);
150                }
151             });
152
153       return currentPanel;
154    }
155
156    private Node parseDocument (String JavaDoc refToFile) {
157       Document document=null;
158
159       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
160       factory.setValidating(false);
161
162       // Parse the Document and traverse the DOM
163
try {
164          ParsingErrors pErrors=new ParsingErrors();
165
166          DocumentBuilder parser = factory.newDocumentBuilder();
167          parser.setErrorHandler(pErrors);
168
169          File f=new File(refToFile);
170          if (!f.exists()) {
171             f=new File(f.getCanonicalPath());
172          }
173          //document=parser.parse(xmlFile);
174
document=parser.parse(new InputSource JavaDoc(new FileInputStream(f))); // Fixed by Harald Meister
175

176          Set errorMessages = pErrors.getErrorMessages();
177          if (errorMessages.size()>0) {
178             System.err.println("Errors in schema type");
179          }
180       } catch (Exception JavaDoc ex) {
181          System.err.println("Fatal error while parsing xml schema document");
182          return null;
183       }
184       if (document!=null) {
185          return document.getDocumentElement();
186       } else {
187          return null;
188       }
189
190    }
191
192 }
193
194
Popular Tags