001: package org.conform.mdl;
002:
003: import org.conform.*;
004: import org.xml.sax.AttributeList;
005: import org.xml.sax.HandlerBase;
006: import org.xml.sax.SAXException;
007: import org.apache.commons.logging.LogFactory;
008:
009: import java.util.Arrays;
010: import java.util.List;
011:
012: public class MetaDescriptionLanguageHandler extends HandlerBase {
013: private static org.apache.commons.logging.Log LOG = LogFactory
014: .getLog(Meta.class);
015:
016: HandlerFactory handlerFactory = DefaultHandlerFactory.getInstance();
017: private BeanMeta bean;
018: private PropertyMeta property;
019: private ScopeHandler handler;
020: static List knownHandlers = Arrays.asList(new String[] {
021: "readable", "mandatory", "writable", "priority",
022: "relation-type", "relation-bean", "relation-property",
023: "attribute", "domain-provider", "default-value",
024: "validation", "format", });
025:
026: public MetaDescriptionLanguageHandler() {
027: }
028:
029: public void characters(char ch[], int start, int length)
030: throws SAXException {
031: if (handler != null)
032: handler.characters(ch, start, length);
033: }
034:
035: public void startElement(String name, AttributeList attributes)
036: throws SAXException {
037: LOG.debug("mdl.startElement(name) = " + name);
038: if (handler == null && getScope() != null)
039: handler = handlerFactory.getHandler(name);
040:
041: if (handler != null) {
042: LOG.debug("start handler.getClass() = "
043: + handler.getClass() + " on " + getScope());
044: handler.setScope(getScope());
045: handler.startElement(name, attributes);
046: } else if ("meta-description".equals(name))
047: return;
048: else if ("bean".equals(name))
049: addressBean(attributes.getValue("name"));
050: else if ("property".equals(name))
051: addressProperty(attributes.getValue("name"));
052: }
053:
054: public void endElement(String name) throws SAXException {
055: LOG.debug("mdl.endElement(name) = " + name);
056: if (handler != null) {
057: LOG.debug("end handler.getClass() = " + handler.getClass()
058: + " on " + getScope());
059: handler.endElement(name);
060: }
061: if (knownHandlers.contains(name))
062: handler = null;
063: else if ("meta-description".equals(name))
064: return;
065: else if ("bean".equals(name))
066: bean = null;
067: else if (handler == null && "property".equals(name)) {
068: completeRelation(property);
069: property = null;
070: }
071: }
072:
073: private void completeRelation(PropertyMeta property) {
074: if (property == null)
075: return;
076: String propertyName = (String) property
077: .getAttribute("relation-property");
078: if (propertyName == null)
079: return;
080: PropertyMeta relationProperty = property.getRelationBean()
081: .getProperty(propertyName);
082: LOG.trace("THAT'S IT relationProperty = " + relationProperty);
083: ((PropertyMeta) property).setRelationProperty(relationProperty);
084: ((PropertyMeta) relationProperty).setRelationProperty(property);
085:
086: }
087:
088: protected void addressBean(String name) {
089: ClassLoader classLoader = Thread.currentThread()
090: .getContextClassLoader();
091: if (classLoader == null)
092: classLoader = getClass().getClassLoader();
093: try {
094: bean = getBeans().getBeanMeta(classLoader.loadClass(name));
095: } catch (ClassNotFoundException e) {
096: LOG.warn("class not loadable " + name);
097: }
098: if (bean == null)
099: LOG.warn("no bean with name " + name);
100: }
101:
102: protected void addressProperty(String name) {
103: if (bean == null) {
104: int pos = name.indexOf(".");
105: if (pos == -1)
106: return;
107: String beanName = name.substring(0, pos);
108: addressBean(beanName);
109: name = name.substring(pos + 1);
110: }
111: property = bean.getProperty(name);
112: if (property == null)
113: LOG.warn("no property with name " + name);
114: }
115:
116: private Object getScope() {
117: if (property != null)
118: return property;
119: return bean;
120: }
121:
122: public BeanMetaProvider getBeans() {
123: return MDLModifier.getBeans();
124: }
125: }
|