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.binding;
018:
019: import org.compass.core.config.ConfigurationException;
020: import org.compass.core.mapping.MappingException;
021: import org.compass.core.metadata.Alias;
022: import org.compass.core.metadata.CompassMetaData;
023: import org.compass.core.metadata.MetaData;
024: import org.compass.core.metadata.MetaDataGroup;
025: import org.compass.core.metadata.impl.AbstractMetaDataItem;
026: import org.compass.core.metadata.impl.DefaultAlias;
027: import org.compass.core.metadata.impl.DefaultCompassMetaData;
028: import org.compass.core.metadata.impl.DefaultMetaData;
029: import org.compass.core.metadata.impl.DefaultMetaDataGroup;
030: import org.compass.core.util.DTDEntityResolver;
031: import org.compass.core.util.config.ConfigurationHelper;
032: import org.xml.sax.EntityResolver;
033:
034: /**
035: * @author kimchy
036: */
037: public class XmlMetaDataBinding extends AbstractXmlMappingBinding {
038:
039: protected EntityResolver doGetEntityResolver() {
040: return new DTDEntityResolver();
041: }
042:
043: protected String getSuffix() {
044: return ".cmd.xml";
045: }
046:
047: public boolean doAddConfiguration(ConfigurationHelper doc)
048: throws ConfigurationException, MappingException {
049: if (!doc.getName().equals("compass-core-meta-data")) {
050: return false;
051: }
052:
053: ConfigurationHelper[] groups = doc
054: .getChildren("meta-data-group");
055: for (int i = 0; i < groups.length; i++) {
056: DefaultMetaDataGroup group = new DefaultMetaDataGroup();
057: bindGroup(groups[i], group, metaData);
058: MetaDataGroup tmpGroup = metaData.getGroup(group.getName());
059: if (tmpGroup != null) {
060: throw new ConfigurationException("Group ["
061: + group.getName() + "] found twice");
062: }
063: ((DefaultCompassMetaData) metaData).addGroup(group);
064: }
065:
066: return true;
067: }
068:
069: private void bindGroup(ConfigurationHelper groupConf,
070: DefaultMetaDataGroup group, CompassMetaData compassMetaData)
071: throws ConfigurationException {
072: bindAbstractMetaDataItem(groupConf, group);
073:
074: ConfigurationHelper[] aliases = groupConf.getChildren("alias");
075: for (int i = 0; i < aliases.length; i++) {
076: DefaultAlias alias = new DefaultAlias();
077: alias.setGroup(group);
078: bindAlias(aliases[i], alias);
079: Alias tmpAlias = compassMetaData.getAlias(alias.getId());
080: if (tmpAlias != null) {
081: throw new ConfigurationException("Alias [" + alias
082: + "] found twice");
083: }
084: group.addAlias(alias);
085: }
086:
087: ConfigurationHelper[] metadatas = groupConf
088: .getChildren("meta-data");
089: for (int i = 0; i < metadatas.length; i++) {
090: DefaultMetaData metaData = new DefaultMetaData();
091: metaData.setGroup(group);
092: bindMetaData(metadatas[i], metaData);
093: MetaData tmpMetaData = compassMetaData.getMetaData(metaData
094: .getId());
095: if (tmpMetaData != null) {
096: throw new ConfigurationException("MetaData ["
097: + metaData + "] found twice");
098: }
099: group.addMetaData(metaData);
100: }
101: }
102:
103: private void bindAlias(ConfigurationHelper aliasConf,
104: DefaultAlias alias) throws ConfigurationException {
105: bindAbstractMetaDataItem(aliasConf, alias);
106: }
107:
108: private void bindMetaData(ConfigurationHelper metaDataConf,
109: DefaultMetaData metaData) throws ConfigurationException {
110: bindAbstractMetaDataItem(metaDataConf, metaData);
111: ConfigurationHelper nameConf = metaDataConf.getChild("name");
112: metaData.setFormat(nameConf.getAttribute("format", null));
113:
114: ConfigurationHelper[] values = metaDataConf
115: .getChildren("value");
116: for (int i = 0; i < values.length; i++) {
117: String id = values[i].getAttribute("id");
118: metaData.setValue(id, values[i].getValue().trim());
119: }
120: }
121:
122: private void bindAbstractMetaDataItem(ConfigurationHelper absConf,
123: AbstractMetaDataItem item) {
124: item.setId(absConf.getAttribute("id"));
125: item.setDisplayName(absConf.getAttribute("displayName", item
126: .getName()));
127: ConfigurationHelper descriptionConf = absConf.getChild(
128: "description", false);
129: if (descriptionConf != null) {
130: item.setDescription(descriptionConf.getValue().trim());
131: } else {
132: item.setDescription(item.getName());
133: }
134:
135: ConfigurationHelper uriConf = absConf.getChild("uri", false);
136: if (uriConf != null) {
137: item.setUri(uriConf.getValue().trim());
138: } else {
139: item.setUri(item.getName());
140: }
141:
142: ConfigurationHelper nameConf = absConf.getChild("name", false);
143: if (nameConf != null) {
144: item.setName(nameConf.getValue().trim());
145: }
146: }
147:
148: }
|