1 16 17 package org.apache.commons.jocl; 18 19 import org.xml.sax.*; 20 import org.xml.sax.helpers.*; 21 import java.util.ArrayList ; 22 import java.lang.reflect.InvocationTargetException ; 23 import java.io.InputStream ; 24 import java.io.Reader ; 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileNotFoundException ; 28 import java.io.IOException ; 29 30 35 212 public class JOCLContentHandler extends DefaultHandler implements ContentHandler { 213 214 221 public static void main(String [] args) throws Exception { 222 JOCLContentHandler jocl = JOCLContentHandler.parse(System.in,null); 223 for(int i=0;i<jocl.size();i++) { 224 System.out.println("<" + jocl.getType(i) + ">\t" + jocl.getValue(i)); 225 } 226 } 227 228 237 public static JOCLContentHandler parse(File f) throws SAXException, FileNotFoundException , IOException { 238 return JOCLContentHandler.parse(new FileInputStream (f),null); 239 } 240 241 250 public static JOCLContentHandler parse(Reader in) throws SAXException, IOException { 251 return JOCLContentHandler.parse(new InputSource(in),null); 252 } 253 254 263 public static JOCLContentHandler parse(InputStream in) throws SAXException, IOException { 264 return JOCLContentHandler.parse(new InputSource(in),null); 265 } 266 267 276 public static JOCLContentHandler parse(InputSource in) throws SAXException, IOException { 277 return JOCLContentHandler.parse(in,null); 278 } 279 280 290 public static JOCLContentHandler parse(File f, XMLReader reader) throws SAXException, FileNotFoundException , IOException { 291 return JOCLContentHandler.parse(new FileInputStream (f),reader); 292 } 293 294 303 public static JOCLContentHandler parse(Reader in, XMLReader reader) throws SAXException, IOException { 304 return JOCLContentHandler.parse(new InputSource(in),reader); 305 } 306 307 316 public static JOCLContentHandler parse(InputStream in, XMLReader reader) throws SAXException, IOException { 317 return JOCLContentHandler.parse(new InputSource(in),reader); 318 } 319 320 329 public static JOCLContentHandler parse(InputSource in, XMLReader reader) throws SAXException, IOException { 330 JOCLContentHandler jocl = new JOCLContentHandler(); 331 if(null == reader) { 332 reader = XMLReaderFactory.createXMLReader(); 333 } 334 reader.setContentHandler(jocl); 335 reader.parse(in); 336 return jocl; 337 } 338 339 341 344 public JOCLContentHandler() { 345 this(true,true,true,true); 346 } 347 348 355 public JOCLContentHandler(boolean emptyEltNS, boolean joclEltPrefix, boolean emptyAttrNS, boolean joclAttrPrefix) { 356 _acceptEmptyNamespaceForElements = emptyEltNS; 357 _acceptJoclPrefixForElements = joclEltPrefix; 358 _acceptEmptyNamespaceForAttributes = emptyAttrNS; 359 _acceptJoclPrefixForAttributes = joclAttrPrefix; 360 } 361 362 364 368 public int size() { 369 return _typeList.size(); 370 } 371 372 375 public void clear() { 376 _typeList = new ArrayList (); 377 _valueList = new ArrayList (); 378 } 379 380 383 public void clear(int i) { 384 _typeList.remove(i); 385 _valueList.remove(i); 386 } 387 388 391 public Class getType(int i) { 392 return(Class )(_typeList.get(i)); 393 } 394 395 398 public Object getValue(int i) { 399 return _valueList.get(i); 400 } 401 402 405 public Object [] getValueArray() { 406 return _valueList.toArray(); 407 } 408 409 412 public Object [] getTypeArray() { 413 return _typeList.toArray(); 414 } 415 416 418 public void startElement(String uri, String localName, String qname, Attributes attr) throws SAXException { 419 try { 420 if(isJoclNamespace(uri,localName,qname)) { 421 if(ELT_OBJECT.equals(localName)) { 422 String cname = getAttributeValue(ATT_CLASS,attr); 423 String isnullstr = getAttributeValue(ATT_ISNULL,attr,"false"); 424 boolean isnull = ("true".equalsIgnoreCase(isnullstr) || "yes".equalsIgnoreCase(isnullstr)); 425 _cur = new ConstructorDetails(cname,_cur,isnull); 426 } else if(ELT_BOOLEAN.equals(localName)) { 427 String valstr = getAttributeValue(ATT_VALUE,attr,"false"); 428 boolean val = ("true".equalsIgnoreCase(valstr) || "yes".equalsIgnoreCase(valstr)); 429 addObject(Boolean.TYPE,new Boolean (val)); 430 } else if(ELT_BYTE.equals(localName)) { 431 byte val = Byte.parseByte(getAttributeValue(ATT_VALUE,attr,"0")); 432 addObject(Byte.TYPE,new Byte (val)); 433 } else if(ELT_CHAR.equals(localName)) { 434 char val = '\u0000'; 435 String valstr = getAttributeValue(ATT_VALUE,attr); 436 if(null == valstr) { 437 val = '\u0000'; 438 } else if(valstr.length() > 1) { 439 throw new SAXException("if present, char value must be exactly one character long"); 440 } else if(valstr.length()==1) { 441 val = valstr.charAt(0); 442 } else if(valstr.length()==0) { 443 throw new SAXException("if present, char value must be exactly one character long"); 444 } 445 addObject(Character.TYPE,new Character (val)); 446 } else if(ELT_DOUBLE.equals(localName)) { 447 double val = Double.parseDouble(getAttributeValue(ATT_VALUE,attr,"0")); 448 addObject(Double.TYPE,new Double (val)); 449 } else if(ELT_FLOAT.equals(localName)) { 450 float val = Float.parseFloat(getAttributeValue(ATT_VALUE,attr,"0")); 451 addObject(Float.TYPE,new Float (val)); 452 } else if(ELT_INT.equals(localName)) { 453 int val = Integer.parseInt(getAttributeValue(ATT_VALUE,attr,"0")); 454 addObject(Integer.TYPE,new Integer (val)); 455 } else if(ELT_LONG.equals(localName)) { 456 long val = Long.parseLong(getAttributeValue(ATT_VALUE,attr,"0")); 457 addObject(Long.TYPE,new Long (val)); 458 } else if(ELT_SHORT.equals(localName)) { 459 short val = Short.parseShort(getAttributeValue(ATT_VALUE,attr,"0")); 460 addObject(Short.TYPE,new Short (val)); 461 } else if(ELT_STRING.equals(localName)) { 462 String val = getAttributeValue(ATT_VALUE,attr); 463 addObject("".getClass(),val); 464 } else { 465 } 467 } 468 } catch(Exception e) { 469 throw new SAXException(e); 470 } 471 } 472 473 public void endElement(String uri, String localName, String qname) throws SAXException { 474 try { 475 if(isJoclNamespace(uri,localName,qname)) { 476 if(ELT_OBJECT.equals(localName)) { 477 ConstructorDetails temp = _cur; 478 _cur = _cur.getParent(); 479 if(null == _cur) { 480 _typeList.add(temp.getType()); 481 _valueList.add(temp.createObject()); 482 } else { 483 _cur.addArgument(temp.getType(),temp.createObject()); 484 } 485 } else if(ELT_BOOLEAN.equals(localName)) { 486 } else if(ELT_BYTE.equals(localName)) { 488 } else if(ELT_CHAR.equals(localName)) { 490 } else if(ELT_DOUBLE.equals(localName)) { 492 } else if(ELT_FLOAT.equals(localName)) { 494 } else if(ELT_INT.equals(localName)) { 496 } else if(ELT_LONG.equals(localName)) { 498 } else if(ELT_SHORT.equals(localName)) { 500 } else if(ELT_STRING.equals(localName)) { 502 } else { 504 } 506 } 507 } catch(Exception e) { 508 throw new SAXException(e); 509 } 510 } 511 512 public void setDocumentLocator(Locator locator) { 513 _locator = locator; 514 } 515 516 518 526 protected boolean isJoclNamespace(String uri, String localname, String qname) { 527 if(JOCL_NAMESPACE_URI.equals(uri)) { 528 return true; 529 } else if(_acceptEmptyNamespaceForElements && (null == uri || "".equals(uri))) { 530 return true; 531 } else if(_acceptJoclPrefixForElements && (null == uri || "".equals(uri)) && qname.startsWith(JOCL_PREFIX)) { 532 return true; 533 } else { 534 return false; 535 } 536 } 537 538 541 protected String getAttributeValue(String localname, Attributes attr) { 542 return getAttributeValue(localname,attr,null); 543 } 544 545 561 protected String getAttributeValue(String localname, Attributes attr, String implied) { 562 String val = attr.getValue(JOCL_NAMESPACE_URI,localname); 563 if(null == val && _acceptEmptyNamespaceForAttributes) { 564 val = attr.getValue("",localname); 565 } 566 if(null == val && _acceptJoclPrefixForAttributes) { 567 val = attr.getValue("",JOCL_PREFIX + localname); 568 } 569 return(null == val ? implied : val); 570 } 571 572 576 protected void addObject(Class type, Object val) { 577 if(null == _cur) { 578 _typeList.add(type); 579 _valueList.add(val); 580 } else { 581 _cur.addArgument(type,val); 582 } 583 } 584 585 587 590 public static final String JOCL_NAMESPACE_URI = "http://apache.org/xml/xmlns/jakarta/commons/jocl"; 591 592 595 public static final String JOCL_PREFIX = "jocl:"; 596 597 600 protected ArrayList _typeList = new ArrayList (); 601 602 605 protected ArrayList _valueList = new ArrayList (); 606 607 610 protected ConstructorDetails _cur = null; 611 612 618 protected boolean _acceptEmptyNamespaceForElements = true; 619 620 628 protected boolean _acceptJoclPrefixForElements = true; 629 630 636 protected boolean _acceptEmptyNamespaceForAttributes = true; 637 638 646 protected boolean _acceptJoclPrefixForAttributes = true; 647 648 649 protected Locator _locator = null; 650 651 652 protected static final String ELT_OBJECT = "object"; 653 654 655 protected static final String ATT_CLASS = "class"; 656 657 658 protected static final String ATT_ISNULL = "null"; 659 660 661 protected static final String ELT_BOOLEAN = "boolean"; 662 663 664 protected static final String ELT_BYTE = "byte"; 665 666 667 protected static final String ELT_CHAR = "char"; 668 669 670 protected static final String ELT_DOUBLE = "double"; 671 672 673 protected static final String ELT_FLOAT = "float"; 674 675 676 protected static final String ELT_INT = "int"; 677 678 679 protected static final String ELT_LONG = "long"; 680 681 682 protected static final String ELT_SHORT = "short"; 683 684 685 protected static final String ELT_STRING = "string"; 686 687 688 protected static final String ATT_VALUE = "value"; 689 690 class ConstructorDetails { 691 private ConstructorDetails _parent = null; 692 private Class _type = null; 693 private ArrayList _argTypes = null; 694 private ArrayList _argValues = null; 695 private boolean _isnull = false; 696 697 public ConstructorDetails(String classname, ConstructorDetails parent) throws ClassNotFoundException { 698 this(Class.forName(classname),parent,false); 699 } 700 701 public ConstructorDetails(String classname, ConstructorDetails parent, boolean isnull) throws ClassNotFoundException { 702 this(Class.forName(classname),parent,isnull); 703 } 704 705 public ConstructorDetails(Class type, ConstructorDetails parent, boolean isnull) { 706 _parent = parent; 707 _type = type; 708 _argTypes = new ArrayList (); 709 _argValues = new ArrayList (); 710 _isnull = isnull; 711 } 712 713 public void addArgument(Object value) { 714 addArgument(value.getClass(),value); 715 } 716 717 public void addArgument(Class type, Object val) { 718 if(_isnull) { 719 throw new NullPointerException ("can't add arguments to null instances"); 720 } 721 _argTypes.add(type); 722 _argValues.add(val); 723 } 724 725 public Class getType() { 726 return _type; 727 } 728 729 public ConstructorDetails getParent() { 730 return _parent; 731 } 732 733 public Object createObject() throws InstantiationException , ClassNotFoundException , IllegalAccessException , InvocationTargetException { 734 if(_isnull) { 735 return null; 736 } else { 737 Class k = getType(); 738 Class [] argtypes = (Class [])_argTypes.toArray(new Class [0]); 739 Object [] argvals = _argValues.toArray(); 740 return ConstructorUtil.invokeConstructor(k,argtypes,argvals); 741 } 742 } 743 } 744 745 } 746 | Popular Tags |