1 10 11 package org.enhydra.jawe.xml.elements; 12 13 import org.enhydra.jawe.xml.*; 14 import org.enhydra.jawe.xml.panels.*; 15 import org.enhydra.jawe.xml.elements.specialpanels.*; 16 17 import java.util.*; 18 import javax.swing.*; 19 import java.io.*; 20 21 import javax.xml.parsers.*; 22 import javax.xml.transform.*; 23 import javax.xml.transform.dom.*; 24 import javax.xml.transform.stream.*; 25 import org.xml.sax.*; 26 import org.w3c.dom.*; 27 28 33 public class ExtendedAttribute extends XMLComplexElement { 34 private XMLAttribute attrName=new XMLAttribute("Name") { public XMLPanel getPanel () { 36 Set eas=XMLUtil.getAllExtendedAttributeNames(myOwner.getOwner()); 37 Iterator allAttribs=myOwner.toCollection().iterator(); 39 while (allAttribs.hasNext()) { 40 ExtendedAttribute ea=(ExtendedAttribute)allAttribs.next(); 41 String eaN=ea.get("Name").toString(); 42 if (!eaN.equals("")) { 43 eas.add(eaN); 44 } 45 } 46 String [] choices=new String [eas.size()]; 47 eas.toArray(choices); 48 Arrays.sort(choices); 49 return new XMLComboPanel(this,choices,XMLPanel.BOX_LAYOUT,false,true); 50 } 51 }; 52 53 private XMLAttribute attrValue=new XMLAttribute("Value") { 54 public XMLPanel getPanel () { 55 return new XMLMultiLineTextPanel(this,XMLPanel.BOX_LAYOUT,true,true,false); 56 } 57 }; 58 59 private ComplexContent refComplexContent; 60 61 private XMLCollection refAny=new XMLCollection(null,"Any"); 62 63 private ExtendedAttributes myOwner=null; 64 67 public ExtendedAttribute (ExtendedAttributes owner) { 68 super(); 69 this.myOwner=owner; 70 refComplexContent=new ComplexContent(getPackage()); 71 fillStructure (); 72 } 73 74 77 public Package getPackage () { 78 XMLElement own=myOwner.getOwner(); 79 if (own instanceof Package ) { 80 return (Package )own; 81 } else if (own instanceof Participant) { 82 return ((Participant)own).getPackage(); 83 } else if (own instanceof XMLCollectionElement) { 84 XMLComplexElement own2=((XMLCollectionElement)own).getCollection().getOwner(); 85 if (own2 instanceof Package ) { 86 return (Package )own2; 87 } else if (own2 instanceof WorkflowProcess) { 88 return ((WorkflowProcess)own2).getPackage(); 89 } else if (own2 instanceof Activity) { 90 return ((Activity)own2).getOwnerProcess().getPackage(); 91 } else if (own2 instanceof ActivitySet) { 92 return ((ActivitySet)own2).getOwnerProcess().getPackage(); 93 } else { 94 return null; 95 } 96 } else if (own instanceof ExternalPackage) { 97 return ((ExternalPackage)own).getMyPackage(); 98 } else { 99 return null; 100 } 101 } 102 103 107 protected void fillStructure () { 108 attrName.setRequired(true); 109 complexStructure.add(attrName); 110 attributes.add(attrName); 111 complexStructure.add(attrValue); 112 attributes.add(attrValue); 113 complexStructure.add(refAny); 114 complexStructure.add(refComplexContent); 115 } 116 117 123 public void toXML(Node parent) { 124 Node node = (parent.getOwnerDocument()).createElement(name); 125 attrName.toXML(node); 126 attrValue.toXML(node); 127 if (refAny.size()!=0) { 128 Iterator it=refAny.toCollection().iterator(); 129 while (it.hasNext()) { 130 XMLElement el=(XMLElement)it.next(); 131 el.toXML(node); 132 } 133 } 134 try { 136 String toParse=refComplexContent.toString().trim(); 137 if (toParse.length()>0) { 138 Node n=parseContent(toParse); 139 NodeList nl=n.getChildNodes(); 140 for (int i=0; i<nl.getLength(); i++) { 141 node.appendChild(parent.getOwnerDocument().importNode(nl.item(i),true)); 142 } 143 } 144 } catch (Exception ex) {} 145 146 parent.appendChild(node); 147 } 148 149 public void fromXML(Node node) { 150 super.fromXML(node); 151 if (node!=null && node.hasChildNodes()) { 152 String txt=XMLUtil.getChildNodesContent(node); 153 refComplexContent.setValue(txt); 154 } 155 } 156 157 163 public XMLPanel getPanel () { 164 return new XMLGroupPanel (this,new XMLElement[] {attrName,attrValue,refComplexContent},toLabel()); 165 } 166 167 public String toString () { 168 String toRet=XMLUtil.getLanguageDependentString("NameKey")+"="+attrName.toString()+", "+ 169 XMLUtil.getLanguageDependentString("ValueKey")+"="+attrValue.toString(); 170 if (toRet==null) { 171 return ""; 172 } else { 173 return toRet; 174 } 175 } 176 177 184 public Object clone () { 185 ExtendedAttribute ea=(ExtendedAttribute)super.clone(); 186 ea.attrName=(XMLAttribute)this.attrName.clone(); 187 ea.attrValue=(XMLAttribute)this.attrValue.clone(); 188 ea.refComplexContent=(ComplexContent)this.refComplexContent.clone(); 189 ea.fillStructure(); 190 return ea; 191 } 192 193 194 197 private Node parseContent (String toParse) { 198 Document document=null; 199 200 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 201 factory.setValidating(false); 202 203 try { 204 ParsingErrors pErrors=new ParsingErrors(); 205 206 DocumentBuilder parser = factory.newDocumentBuilder(); 207 parser.setErrorHandler(pErrors); 208 toParse="<ExtAttribsAddition>"+toParse+"</ExtAttribsAddition>"; 211 document=parser.parse(new InputSource(new StringReader(toParse))); 212 Set errorMessages = pErrors.getErrorMessages(); 213 if (errorMessages.size()>0) { 214 System.err.println("Errors in schema type"); 215 } 216 } catch (Exception ex) { 217 ex.printStackTrace(); 218 System.err.println("Fatal error while parsing ext. attributes complex content "+toParse); 219 return null; 220 } 221 if (document!=null) { 222 return document.getDocumentElement(); 223 } else { 224 return null; 225 } 226 227 } 228 229 237 public boolean isValidEnter (XMLPanel p) { 238 XMLGroupPanel gp=(XMLGroupPanel)p; 239 XMLComplexContentPanel ccp=(XMLComplexContentPanel)gp.getPanel(2); 240 try { 241 XMLTextPanel tp=(XMLTextPanel)gp.getPanel(0); 242 String nameToCheck=tp.getText(); 243 if (!XMLCollection.isIdValid(nameToCheck)) { 244 String message=XMLUtil.getLanguageDependentString("ErrorValueIsNotValid"); 245 String dialogTitle=XMLUtil.getLanguageDependentString("Title"); 246 XMLPanel.errorMessage(p.getDialog(),dialogTitle,attrName.toLabel()+": ",message); 247 ((JTextField)tp.getComponent(2)).requestFocus(); 248 return false; 249 } 250 } catch (Exception ex){} 251 252 String cmplxCnt=ccp.getText().trim(); 253 if (cmplxCnt.length()>0) { 254 Object ret=parseContent(cmplxCnt); 255 if (ret==null) { 256 XMLUtil.message( 257 XMLUtil.getLanguageDependentString("ErrorComplexContentOfExtendedAttributeIsNotValid"), 258 javax.swing.JOptionPane.ERROR_MESSAGE); 259 return false; 260 } 261 } 262 263 return true; 264 } 265 266 } 267 | Popular Tags |