001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixcore.beans.metadata;
021:
022: import java.io.FileNotFoundException;
023: import java.net.URL;
024: import java.util.Iterator;
025:
026: import javax.xml.XMLConstants;
027: import javax.xml.namespace.NamespaceContext;
028: import javax.xml.parsers.DocumentBuilder;
029: import javax.xml.parsers.DocumentBuilderFactory;
030:
031: import org.apache.log4j.Logger;
032: import org.w3c.dom.Document;
033: import org.w3c.dom.Element;
034: import org.w3c.dom.NodeList;
035: import org.xml.sax.ErrorHandler;
036: import org.xml.sax.SAXException;
037: import org.xml.sax.SAXParseException;
038:
039: /**
040: * @author mleidig@schlund.de
041: */
042: public class DOMInit {
043:
044: protected final static Logger LOG = Logger.getLogger(DOMInit.class);
045:
046: public final static String XMLNS_BEANMETADATA = "http://pustefix.sourceforge.net/beanmetadata";
047:
048: private Beans beans;
049:
050: public DOMInit() {
051: beans = new Beans();
052: }
053:
054: public DOMInit(Beans beans) {
055: this .beans = beans;
056: }
057:
058: public Beans getBeans() {
059: return beans;
060: }
061:
062: public void update(Document doc) throws DOMInitException {
063: NodeList beanNodeList = doc.getElementsByTagName("bean");
064: for (int i = 0; i < beanNodeList.getLength(); i++) {
065: Element beanElem = (Element) beanNodeList.item(i);
066: String val = getAttribute("class", beanElem);
067: if (val == null)
068: throw new DOMInitException(
069: "Missing attribute: /bean-metadata/bean[" + i
070: + "]/@class");
071: Bean bean = new Bean(val);
072: if (beans.getBean(val) != null)
073: LOG.warn("Override metadata for bean '" + val + "'.");
074: beans.setBean(bean);
075: val = getAttribute("exclude-by-default", beanElem);
076: if (val != null && Boolean.parseBoolean(val))
077: bean.excludeByDefault();
078: NodeList propNodeList = beanElem
079: .getElementsByTagName("property");
080: for (int j = 0; j < propNodeList.getLength(); j++) {
081: Element propElem = (Element) propNodeList.item(j);
082: val = getAttribute("name", propElem);
083: if (val == null)
084: throw new DOMInitException(
085: "Missing attribute: /bean-metadata/bean["
086: + i + "]/property[" + j + "]/@name");
087: Property prop = new Property(val);
088: bean.setProperty(prop);
089: val = getAttribute("alias", propElem);
090: if (val != null)
091: prop.setAlias(val);
092: val = getAttribute("exclude", propElem);
093: if (val != null && Boolean.parseBoolean(val))
094: prop.exclude();
095: val = getAttribute("include", propElem);
096: if (val != null && Boolean.parseBoolean(val))
097: prop.include();
098: }
099: }
100: }
101:
102: private String getAttribute(String name, Element element) {
103: String val = element.getAttribute(name);
104: if (val != null) {
105: val = val.trim();
106: if (val.equals(""))
107: val = null;
108: }
109: return val;
110: }
111:
112: public void update(URL metadataUrl) throws DOMInitException {
113: if (LOG.isDebugEnabled())
114: LOG.debug("Update metadata from " + metadataUrl);
115: DocumentBuilderFactory dbf = DocumentBuilderFactory
116: .newInstance();
117: dbf.setValidating(false);
118: dbf.setNamespaceAware(true);
119: try {
120: DocumentBuilder db = dbf.newDocumentBuilder();
121: db.setErrorHandler(new MyErrorHandler());
122: Document doc = db.parse(metadataUrl.openStream());
123: update(doc);
124: } catch (FileNotFoundException x) {
125: if (LOG.isDebugEnabled())
126: LOG.debug("No metadata file found: "
127: + metadataUrl.toString());
128: } catch (Exception x) {
129: throw new DOMInitException("Can't read metadata from '"
130: + metadataUrl + "'.", x);
131: }
132: }
133:
134: class MyNamespaceContext implements NamespaceContext {
135:
136: public Iterator<String> getPrefixes(String namespaceURI) {
137: return null;
138: }
139:
140: public String getNamespaceURI(String prefix) {
141: if (prefix.equals("bmd"))
142: return XMLNS_BEANMETADATA;
143: return XMLConstants.NULL_NS_URI;
144: }
145:
146: public String getPrefix(String namespace) {
147: if (namespace.equals(XMLNS_BEANMETADATA))
148: return "bmd";
149: return null;
150: }
151:
152: }
153:
154: static class MyErrorHandler implements ErrorHandler {
155:
156: public void error(SAXParseException exception)
157: throws SAXException {
158: LOG.error(exception.getMessage());
159: throw exception;
160: }
161:
162: public void fatalError(SAXParseException exception)
163: throws SAXException {
164: LOG.error(exception.getMessage());
165: throw exception;
166: }
167:
168: public void warning(SAXParseException exception)
169: throws SAXException {
170: LOG.warn(exception.getMessage());
171: }
172:
173: }
174:
175: }
|