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;
018:
019: import org.compass.core.converter.ConversionException;
020: import org.compass.core.metadata.Alias;
021: import org.compass.core.metadata.CompassMetaData;
022: import org.compass.core.metadata.MetaData;
023: import org.compass.core.metadata.MetaDataGroup;
024:
025: /**
026: * @author kimchy
027: */
028: public class CommonMetaDataLookup {
029:
030: private CompassMetaData metaData;
031:
032: public CommonMetaDataLookup(CompassMetaData metaData) {
033: this .metaData = metaData;
034: }
035:
036: public Alias lookupAlias(String value)
037: throws ConfigurationException {
038: if (value == null) {
039: return null;
040: }
041:
042: if (!value.startsWith("${") || !value.endsWith("}")) {
043: return null;
044: }
045:
046: String val = value.substring(2, value.length() - 1);
047: int dotIndex = val.indexOf(".");
048: if (dotIndex == -1) {
049: throw new ConfigurationException(
050: "Mailformed alias lookup value, must have a '.'");
051: }
052:
053: String groupId = val.substring(0, dotIndex);
054: MetaDataGroup group = metaData.getGroup(groupId);
055: if (group == null) {
056: throw new ConfigurationException("Couldn't find group ["
057: + groupId + "] in lookup value [" + val + "]");
058: }
059:
060: String aliasId = val.substring(dotIndex + 1, val.length());
061: dotIndex = aliasId.indexOf(".");
062: if (dotIndex != -1) {
063: aliasId = aliasId.substring(0, dotIndex);
064: }
065:
066: Alias alias = group.getAlias(aliasId);
067: if (alias == null) {
068: throw new ConfigurationException(
069: "Couldn't find alias for [" + val + "]");
070: }
071:
072: return alias;
073: }
074:
075: public String lookupAliasName(String value)
076: throws ConfigurationException {
077: Alias alias = lookupAlias(value);
078: if (alias == null) {
079: return value;
080: }
081:
082: return alias.getName();
083: }
084:
085: public MetaData lookupMetaData(String value)
086: throws ConfigurationException {
087: if (value == null) {
088: return null;
089: }
090:
091: if (!value.startsWith("${") || !value.endsWith("}")) {
092: return null;
093: }
094:
095: String val = value.substring(2, value.length() - 1);
096: int dotIndex = val.indexOf(".");
097: if (dotIndex == -1) {
098: throw new ConfigurationException(
099: "Mailformed meta-data lookup value, must have a '.' with a meta-data-group as the prefix");
100: }
101:
102: String groupId = val.substring(0, dotIndex);
103: MetaDataGroup group = metaData.getGroup(groupId);
104: if (group == null) {
105: throw new ConfigurationException("Couldn't find group ["
106: + groupId + "] in lookup value [" + val + "]");
107: }
108:
109: String metaDataId = val.substring(dotIndex + 1, val.length());
110: dotIndex = metaDataId.indexOf(".");
111: if (dotIndex != -1) {
112: metaDataId = metaDataId.substring(0, dotIndex);
113: }
114:
115: MetaData metaData = group.getMetaData(metaDataId);
116: if (metaData == null) {
117: throw new ConfigurationException(
118: "Couldn't find meta-data for [" + val + "]");
119: }
120:
121: return metaData;
122: }
123:
124: public String lookupMetaDataName(String value)
125: throws ConfigurationException {
126: MetaData metaData = lookupMetaData(value);
127: if (metaData == null) {
128: return value;
129: }
130: return metaData.getName();
131: }
132:
133: public String lookupMetaDataFormat(String value)
134: throws ConfigurationException {
135: MetaData metaData = lookupMetaData(value);
136: if (metaData == null) {
137: // this part return null here, and not the original value
138: return null;
139: }
140:
141: return metaData.getFormat();
142: }
143:
144: public String lookupMetaDataValue(String value)
145: throws ConfigurationException {
146: MetaData metaData = lookupMetaData(value);
147: if (metaData == null) {
148: return value;
149: }
150:
151: int dotIndex = value.lastIndexOf(".");
152: if (dotIndex == -1) {
153: throw new ConversionException(
154: "Must defined a [.] after the meta data for the id of the value with lookup ["
155: + value + "]");
156: }
157:
158: String valueId = value.substring(dotIndex + 1,
159: value.length() - 1);
160: return metaData.getValue(valueId);
161: }
162:
163: }
|