001: /*
002: * $Id: AttributeHandler.java 408 2005-02-14 12:42:30Z hengels $
003: * (c) Copyright 2004 con:cern development team.
004: *
005: * This file is part of con:cern (http://concern.org).
006: *
007: * con:cern is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU Lesser General Public License
009: * as published by the Free Software Foundation; either version 2.1
010: * of the License, or (at your option) any later version.
011: *
012: * Please see COPYING for the complete licence.
013: */
014: package org.concern.model.cpd;
015:
016: import org.xml.sax.Attributes;
017: import org.xml.sax.SAXException;
018:
019: import java.lang.reflect.Constructor;
020:
021: public class AttributeHandler extends ScopeHandler {
022: private TextHandler attributeNameHandler = new TextHandler();
023: private TextHandler attributeTypeHandler = new TextHandler();
024: private TextHandler attributeValueHandler = new TextHandler();
025: private Attribute attribute;
026:
027: public AttributeHandler() {
028: }
029:
030: public Attribute getAttribute() {
031: return attribute;
032: }
033:
034: public void startElement(String uri, String localName,
035: String qName, Attributes attributes) throws SAXException {
036: attribute = new Attribute();
037: }
038:
039: public void characters(char ch[], int start, int length)
040: throws SAXException {
041: }
042:
043: public void endElement(String uri, String localName, String qName)
044: throws SAXException {
045: }
046:
047: public ScopeHandler getHandler(String localName, String qName) {
048: if ("attribute-name".equals(qName))
049: return attributeNameHandler;
050: else if ("attribute-type".equals(qName))
051: return attributeTypeHandler;
052: else if ("attribute-value".equals(qName))
053: return attributeValueHandler;
054: else
055: throw new RuntimeException("unexpected tag " + localName
056: + " " + qName);
057: }
058:
059: public void fetchChild(ScopeHandler handler) {
060: if (handler == attributeNameHandler)
061: attribute.setName(attributeNameHandler.getText());
062: else if (handler == attributeTypeHandler)
063: attribute.setType(attributeTypeHandler.getText());
064: else if (handler == attributeValueHandler)
065: attribute.setString(attributeValueHandler.getText());
066: }
067:
068: public static class Attribute {
069: private String name;
070: private Object value;
071: private String string;
072: private String type;
073:
074: public String getName() {
075: return name;
076: }
077:
078: public void setName(String name) {
079: this .name = name;
080: }
081:
082: public Object getValue() {
083: return value;
084: }
085:
086: public void setType(String type) {
087: this .type = type;
088: value();
089: }
090:
091: public void setString(String string) {
092: this .string = string;
093: value();
094: }
095:
096: private void value() {
097: if (string != null && type != null) {
098: try {
099: Class clazz = Class.forName(type);
100: Constructor constructor = clazz
101: .getConstructor(new Class[] { String.class });
102: value = constructor
103: .newInstance(new Object[] { string });
104: } catch (Exception e) {
105: e.printStackTrace();
106: }
107: }
108: }
109: }
110: }
|