001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.config.builder;
018:
019: import java.io.File;
020:
021: import org.compass.core.config.CompassConfiguration;
022: import org.compass.core.config.CompassEnvironment;
023: import org.compass.core.config.ConfigurationException;
024: import org.compass.core.util.ClassUtils;
025: import org.compass.core.util.DTDEntityResolver;
026: import org.compass.core.util.DomUtils;
027: import org.w3c.dom.Document;
028: import org.w3c.dom.Element;
029: import org.w3c.dom.Node;
030: import org.w3c.dom.NodeList;
031: import org.xml.sax.EntityResolver;
032:
033: /**
034: * @author kimchy
035: */
036: public class DTDConfigurationBuilder extends
037: AbstractXmlConfigurationBuilder {
038:
039: protected void doProcess(Document doc, CompassConfiguration config)
040: throws ConfigurationException {
041: Element root = doc.getDocumentElement();
042: // the root is the compass element
043: NodeList nl = root.getChildNodes();
044: for (int i = 0; i < nl.getLength(); i++) {
045: Node node = nl.item(i);
046: if (node instanceof Element) {
047: if ("compass".equals(node.getNodeName())) {
048: processCompass((Element) node, config);
049: }
050: }
051: }
052: }
053:
054: protected void processCompass(Element compassElement,
055: CompassConfiguration config) throws ConfigurationException {
056: String name = DomUtils.getElementAttribute(compassElement,
057: "name", "default");
058: config.getSettings().setSetting(CompassEnvironment.NAME, name);
059: NodeList nl = compassElement.getChildNodes();
060: for (int i = 0; i < nl.getLength(); i++) {
061: Node node = nl.item(i);
062: if (node instanceof Element) {
063: Element ele = (Element) node;
064: if ("setting".equals(ele.getNodeName())) {
065: String settingName = ele.getAttribute("name");
066: String settingValue = DomUtils
067: .getTrimmedTextValue(ele);
068: config.setSetting(settingName, settingValue);
069: } else if ("mapping".equals(ele.getNodeName())
070: || "meta-data".equals(ele.getNodeName())) {
071: String rsrc = DomUtils.getElementAttribute(ele,
072: "resource", null);
073: String file = DomUtils.getElementAttribute(ele,
074: "file", null);
075: String jar = DomUtils.getElementAttribute(ele,
076: "jar", null);
077: String pckg = DomUtils.getElementAttribute(ele,
078: "package", null);
079: String clazz = DomUtils.getElementAttribute(ele,
080: "class", null);
081: if (rsrc != null) {
082: config.addResource(rsrc);
083: } else if (jar != null) {
084: config.addJar(new File(jar));
085: } else if (pckg != null) {
086: config.addPackage(pckg);
087: } else if (clazz != null) {
088: try {
089: config.addClass(ClassUtils.forName(clazz,
090: config.getClassLoader()));
091: } catch (ClassNotFoundException e) {
092: throw new ConfigurationException(
093: "Failed map class [" + clazz + "]",
094: e);
095: }
096: } else {
097: if (file == null) {
098: throw new ConfigurationException(
099: "<mapping> or <meta-data> element in configuration specifies no attributes");
100: }
101: config.addFile(file);
102: }
103: }
104: }
105: }
106:
107: log.info("Configured Compass [" + name + "]");
108: if (log.isDebugEnabled()) {
109: log.debug("--with settings [" + config.getSettings() + "]");
110: }
111: }
112:
113: protected EntityResolver doGetEntityResolver() {
114: return new DTDEntityResolver();
115: }
116: }
|