1 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 ; 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 ; 28 29 33 public class SchemaType extends XMLComplexElement { 34 37 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 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 67 complexStructure.remove(0); 69 complexStructure.remove(0); 71 72 if (parent!=null) { 74 Node node = (parent.getOwnerDocument()).createElement(name); 75 String 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 newVal=null; 97 if (node.hasChildNodes()) { 98 NodeList nl=node.getChildNodes(); 100 for (int i=0; i<nl.getLength(); i++) { 101 Node sn=nl.item(i); 102 if (sn instanceof Element) { 104 newVal=XMLUtil.getContent(sn,true); 105 schemaNode=sn; 106 break; 107 } 108 } 109 } 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 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 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 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 refToFile) { 157 Document document=null; 158 159 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 160 factory.setValidating(false); 161 162 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(new InputSource (new FileInputStream(f))); 176 Set errorMessages = pErrors.getErrorMessages(); 177 if (errorMessages.size()>0) { 178 System.err.println("Errors in schema type"); 179 } 180 } catch (Exception 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 |