001: package org.conform.mdl;
002:
003: import org.conform.*;
004: import org.xml.sax.SAXException;
005:
006: import javax.xml.parsers.ParserConfigurationException;
007: import javax.xml.parsers.SAXParser;
008: import javax.xml.parsers.SAXParserFactory;
009: import java.beans.ExceptionListener;
010: import java.io.IOException;
011: import java.net.URL;
012:
013: public class MDLModifier implements Modifier {
014: private static ThreadLocal threadBeans = new ThreadLocal();
015: private URL url;
016: private ExceptionListener exceptionListener;
017: private MetaDescriptionLanguageHandler handler = new MetaDescriptionLanguageHandler();
018: private BeanMetaProvider beanMetaProvider;
019:
020: public MDLModifier() {
021: }
022:
023: public MDLModifier(BeanMetaProvider beanMetaProvider) {
024: setBeanMetaProvider(beanMetaProvider);
025: }
026:
027: public MDLModifier(URL url) {
028: if (url == null)
029: throw new NullPointerException("url must not be null");
030: this .url = url;
031: }
032:
033: public URL getUrl() {
034: return url;
035: }
036:
037: public void setUrl(URL url) {
038: if (url == null)
039: throw new NullPointerException("url must not be null");
040: this .url = url;
041: }
042:
043: public BeanMetaProvider getBeanMetaProvider() {
044: return beanMetaProvider;
045: }
046:
047: public void setBeanMetaProvider(BeanMetaProvider beanMetaProvider) {
048: this .beanMetaProvider = beanMetaProvider; //new CachingBeanMetaProvider(beanMetaProvider);
049: }
050:
051: public static BeanMetaProvider getBeans() {
052: return (BeanMetaProvider) threadBeans.get();
053: }
054:
055: public void modify(BeanMeta beanMeta) {
056: setExceptionListener(getExceptionListener());
057: Statement.setCaching(true);
058: SAXParserFactory factory = SAXParserFactory.newInstance();
059: try {
060: threadBeans.set(beanMetaProvider);
061: SAXParser saxParser = factory.newSAXParser();
062: saxParser.parse(url.openStream(), handler);
063: } catch (ParserConfigurationException e) {
064: getExceptionListener().exceptionThrown(e);
065: } catch (SAXException se) {
066: Exception e = se.getException();
067: getExceptionListener()
068: .exceptionThrown((e == null) ? se : e);
069: } catch (IOException ioe) {
070: getExceptionListener().exceptionThrown(ioe);
071: } finally {
072: threadBeans.set(null);
073: }
074: }
075:
076: /**
077: * Sets the exception handler for this stream to <code>exceptionListener</code>.
078: * The exception handler is notified when this stream catches recoverable
079: * exceptions.
080: *
081: * @param exceptionListener The exception handler for this stream.
082: *
083: * @see #getExceptionListener
084: */
085: public void setExceptionListener(ExceptionListener exceptionListener) {
086: this .exceptionListener = exceptionListener;
087: }
088:
089: /**
090: * Gets the exception handler for this stream.
091: *
092: * @return The exception handler for this stream.
093: *
094: * @see #setExceptionListener
095: */
096: public ExceptionListener getExceptionListener() {
097: return (exceptionListener != null) ? exceptionListener
098: : Statement.defaultExceptionListener;
099: }
100:
101: public void apply(URL resource) {
102: setUrl(resource);
103: modify((BeanMeta) null);
104: }
105: }
|