KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ExtendedAttribute.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 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 /**
29 * Represents a WfMC DTD element that has the similar name.
30 *
31 * @see XML
32 */

33 public class ExtendedAttribute extends XMLComplexElement {
34    private XMLAttribute attrName=new XMLAttribute("Name") { // required
35
public XMLPanel getPanel () {
36          Set eas=XMLUtil.getAllExtendedAttributeNames(myOwner.getOwner());
37          // fill with just defined ext. attrib. names (if any)
38
Iterator allAttribs=myOwner.toCollection().iterator();
39          while (allAttribs.hasNext()) {
40             ExtendedAttribute ea=(ExtendedAttribute)allAttribs.next();
41             String JavaDoc eaN=ea.get("Name").toString();
42             if (!eaN.equals("")) {
43                eas.add(eaN);
44             }
45          }
46          String JavaDoc[] choices=new String JavaDoc[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    /**
65    * Creates a new instance of the class.
66    */

67    public ExtendedAttribute (ExtendedAttributes owner) {
68       super();
69       this.myOwner=owner;
70       refComplexContent=new ComplexContent(getPackage());
71       fillStructure ();
72    }
73
74    /**
75     * Returns the package where ext. attrib. belongs to.
76     */

77    public Package JavaDoc getPackage () {
78       XMLElement own=myOwner.getOwner();
79       if (own instanceof Package JavaDoc) {
80          return (Package JavaDoc)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 JavaDoc) {
86             return (Package JavaDoc)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    /**
104    * Defines the super-class method. Read the explanation for
105    * this method within XMLComplexElement class.
106    */

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    /**
118    * Overrides super-class method to realize this class specific
119    * writting to XML file.
120    *
121    * @return The string for a WfMC DTD ExtendedAttribute element tag.
122    */

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       // adding complex attribute context parsed from a string
135
try {
136          String JavaDoc 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 JavaDoc 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 JavaDoc txt=XMLUtil.getChildNodesContent(node);
153          refComplexContent.setValue(txt);
154       }
155    }
156
157    /**
158    * Prepares the panel for showing extended attribute of
159    * some XML element.
160    *
161    * @return XMLPanel to be shown.
162    */

163    public XMLPanel getPanel () {
164       return new XMLGroupPanel (this,new XMLElement[] {attrName,attrValue,refComplexContent},toLabel());
165    }
166
167    public String JavaDoc toString () {
168       String JavaDoc 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    /**
178    * Used to create exact copy of instance of this class.
179    * The newly created instance will have all the properties
180    * same as the copied one.
181    *
182    * @return The newly created instance of this class.
183    */

184    public Object JavaDoc 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    /**
195     * Parses the String entered within content box, and produces node from it.
196     */

197    private Node parseContent (String JavaDoc 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          // adding helper tag, so after parsing, all its children
209
// will be taken into account
210
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 JavaDoc 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    /**
230     * This method checks if the name of the attribute is valid, and if complex
231     * part of extended attribute is valid. It is
232     * called only if user presses OK button within the editing dialog.
233     * @param p The panel for editing extended attribute.
234     * @return If complex content is parsed properly, returns <tt>true</tt>,
235     * otherwise returns <tt>false</tt>.
236     */

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 JavaDoc nameToCheck=tp.getText();
243          if (!XMLCollection.isIdValid(nameToCheck)) {
244             String JavaDoc message=XMLUtil.getLanguageDependentString("ErrorValueIsNotValid");
245             String JavaDoc 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 JavaDoc ex){}
251
252       String JavaDoc cmplxCnt=ccp.getText().trim();
253       if (cmplxCnt.length()>0) {
254          Object JavaDoc 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