1 10 11 12 package org.enhydra.jawe.xml; 13 14 import org.enhydra.jawe.xml.panels.*; 15 import org.enhydra.jawe.xml.ToNameMutableTreeNode; 16 17 import javax.swing.tree.*; 18 import javax.swing.text.*; 19 import java.util.*; 20 import org.w3c.dom.*; 21 22 72 public class XMLComplexElement 73 extends XMLElement { 74 75 79 protected java.util.List complexStructure = new ArrayList(); 80 81 protected java.util.List attributes = new ArrayList(); 82 83 public XMLComplexElement() { 84 super(); 85 } 86 87 90 public Collection toComplexType() { 91 return complexStructure; 92 } 93 94 98 public Collection toComplexTypeValues() { 99 java.util.List l = new ArrayList(); 100 Iterator it = complexStructure.iterator(); 101 while ( it.hasNext() ) { 102 XMLElement el = ( XMLElement ) it.next(); 103 if ( el instanceof XMLAttribute ) { 104 l.add( el.toString() ); 105 } 106 else { 107 l.add( el.toValue() ); 108 } 109 } 110 return l; 111 } 112 113 117 public void setReadOnly( boolean ro ) { 118 super.setReadOnly( ro ); 119 Iterator it = complexStructure.iterator(); 120 while ( it.hasNext() ) { 121 XMLElement el = ( XMLElement ) it.next(); 122 el.setReadOnly( ro ); 123 } 124 } 125 126 public boolean isEmpty() { 127 boolean isEmpty = true; 128 Iterator it = complexStructure.iterator(); 129 while ( it.hasNext() ) { 130 XMLElement el = ( XMLElement ) it.next(); 131 isEmpty = isEmpty && el.isEmpty(); 132 } 133 return isEmpty; 134 } 135 136 public boolean isValid() { 137 boolean isValid = true; 138 Iterator it = complexStructure.iterator(); 139 while ( it.hasNext() ) { 140 XMLElement el = ( XMLElement ) it.next(); 141 isValid = isValid && el.isValid(); 142 } 143 return isValid; 144 } 145 146 public void toXML( Node parent ) throws DOMException { 147 if ( isEmpty() && !isRequired() ) 148 return; 149 if ( parent != null ) { 150 Node node = ( parent.getOwnerDocument() ).createElement( name ); 151 for ( Iterator it = complexStructure.iterator(); it.hasNext(); ) { 152 XMLElement el = ( XMLElement ) it.next(); 153 el.toXML( node ); 154 } 155 parent.appendChild( node ); 156 } 157 } 158 159 public void fromXML( Node node ) { 160 processAttributes( node ); 161 processElements( node ); 162 } 163 164 protected void processAttributes( Node node ) { 165 if ( node != null ) { 166 if ( node.hasAttributes() ) { 167 NamedNodeMap attribs = node.getAttributes(); 168 for ( int i = 0; i < attribs.getLength(); ++i ) { 169 Node attrib = ( Node ) attribs.item( i ); 170 try { 171 get( attrib.getNodeName() ).fromXML( attrib ); 173 } 174 catch ( NullPointerException npe ) { 175 181 } 182 } 183 } 184 } 185 } 186 187 protected void processElements( Node node ) { 188 if ( node != null ) { 189 if ( node.hasChildNodes() ) { 190 XMLUtil.parseElements( node, complexStructure ); 191 } 192 } 193 } 194 195 public XMLPanel getPanel() { 196 XMLElement[] c = new XMLElement[complexStructure.size()]; 197 complexStructure.toArray( c ); 198 return new XMLGroupPanel( this, c, toLabel() ); 199 } 200 201 202 public XMLElement get( int no ) { 203 try { 204 return ( XMLElement ) complexStructure.get( no ); 205 } 206 catch ( Exception ex ) { 207 return null; 208 } 209 } 210 211 215 public void set( int no, Object value ) { 216 XMLElement el; 217 try { 218 el = get( no ); 219 } 220 catch ( Exception ex ) { 221 el = null; 222 } 223 if ( el != null ) { 224 el.setValue( value ); 225 } 226 } 227 228 229 public XMLElement get( String name ) { 230 Iterator it = complexStructure.iterator(); 231 while ( it.hasNext() ) { 232 XMLElement el = ( XMLElement ) it.next(); 233 if ( el.name.equals( name ) ) { 234 return el; 235 } 236 } 237 return null; 238 } 239 240 244 public void set( String name, Object value ) { 245 XMLElement el = get( name ); 246 if ( el != null ) { 247 el.setValue( value ); 248 } 249 } 250 251 public String toString() { 252 if ( labelName != null ) { 253 return labelName; 254 } 255 else { 256 return ""; 257 } 258 } 259 260 282 protected void fillStructure() { 283 return; 284 } 285 286 public Object clone() { 288 XMLComplexElement d = ( XMLComplexElement )super.clone(); 289 d.complexStructure = new ArrayList(); 290 d.attributes = new ArrayList(); 291 292 return d; 293 } 294 295 public void refreshLabelName() { 296 super.refreshLabelName(); 297 Iterator itCs = complexStructure.iterator(); 298 while ( itCs.hasNext() ) { 299 XMLElement el = ( XMLElement ) itCs.next(); 300 el.refreshLabelName(); 301 } 302 } 303 304 308 public DefaultMutableTreeNode getNode() { 309 DefaultMutableTreeNode node = new ToNameMutableTreeNode( this ); 310 for ( int i = 0; i < this.complexStructure.size(); i++ ) { 311 if ( !this.attributes.contains( this.complexStructure.get( i ) ) ) { 312 node.add( 313 ( ( XMLElement )this.complexStructure.get( i ) ).getNode() 314 ); 315 } 316 } 317 return node; 318 } 319 320 324 public int getElementsCount() { 325 int elementsCount = 0; 326 for ( int i = 0; i < this.complexStructure.size(); i++ ) { 327 if ( !this.attributes.contains( this.complexStructure.get( i ) ) ) { 328 elementsCount++; 329 } 330 } 331 return elementsCount; 332 } 333 334 338 public List getChildElements() { 339 List list = new ArrayList(); 340 for ( int i = 0; i < this.complexStructure.size(); i++ ) { 341 if ( !this.attributes.contains( this.complexStructure.get( i ) ) ) { 342 list.add( this.complexStructure.get( i ) ); 343 } 344 } 345 return list; 346 } 347 348 352 public String getPrintDescription( String indent, StyledDocument doc ) { 353 String retVal = ""; 354 try { 355 StringBuffer all = new StringBuffer (); 356 StringBuffer attributes = new StringBuffer (); 358 int size = this.attributes.size(); 359 for ( int i = 0; i < size; i++ ) { 360 XMLAttribute attr = ( ( XMLAttribute )this.attributes.get( i ) ); 361 attributes.append( attr.toName() + " = " + attr.toString() ); 362 if ( i != size - 1 ) { 363 attributes.append( ", " ); 364 } 365 } 366 367 all.append( toName() + " : " + attributes.toString() ); 368 doc.insertString( doc.getLength(), toName() + " : " + attributes.toString(), atts ); 369 370 if ( !this.isCollapsed() ) { 372 String value = ""; 374 for ( int i = 0; i < this.complexStructure.size(); i++ ) { 375 if ( !this.attributes.contains( this.complexStructure.get( i ) ) ) { 376 value = ( ( XMLElement )this.complexStructure.get( i ) ).getPrintDescription( 377 indent + 378 OFFSET, doc ); 379 if ( value != null && !value.trim().equals( "" ) ) { 380 all.append( NEWLINE ); 381 all.append( indent ); 382 all.append( value ); 383 } 384 } 385 } 386 } 387 retVal = all.toString(); 388 } 389 catch ( Exception e ) { 390 e.printStackTrace(); 391 } 392 return retVal; 393 } 394 395 } 396 | Popular Tags |