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.mapping.xsem;
018:
019: import java.util.Iterator;
020:
021: import org.compass.core.mapping.AbstractResourceMapping;
022: import org.compass.core.mapping.AliasMapping;
023: import org.compass.core.mapping.InvalidMappingException;
024: import org.compass.core.mapping.Mapping;
025: import org.compass.core.mapping.MappingException;
026: import org.compass.core.mapping.OverrideByNameMapping;
027: import org.compass.core.mapping.ResourcePropertyMapping;
028: import org.compass.core.xml.XmlXPathExpression;
029:
030: /**
031: * @author kimchy
032: */
033: public class XmlObjectMapping extends AbstractResourceMapping implements
034: XPathEnabledMapping {
035:
036: private ResourcePropertyMapping[] resourcePropertyMappings;
037:
038: private String xpath;
039:
040: private XmlXPathExpression xpathExpression;
041:
042: private XmlContentMapping xmlContentMapping;
043:
044: public Mapping copy() {
045: XmlObjectMapping copy = new XmlObjectMapping();
046: copy.setXPath(getXPath());
047: copy(copy);
048: return copy;
049: }
050:
051: public AliasMapping shallowCopy() {
052: XmlObjectMapping copy = new XmlObjectMapping();
053: copy.setXPath(getXPath());
054: shallowCopy(copy);
055: return copy;
056: }
057:
058: public int addMapping(Mapping mapping) {
059: // no duplicate mapping names are allowed
060: if (mapping instanceof ResourcePropertyMapping) {
061: ResourcePropertyMapping resourcePropertyMapping = (ResourcePropertyMapping) mapping;
062: if (mappingsByNameMap
063: .get(resourcePropertyMapping.getName()) != null) {
064: if (!(resourcePropertyMapping instanceof OverrideByNameMapping)
065: || !((OverrideByNameMapping) resourcePropertyMapping)
066: .isOverrideByName()) {
067: throw new InvalidMappingException(
068: "Two resource property mappings are mapped to property path ["
069: + resourcePropertyMapping.getPath()
070: .getPath()
071: + "], it is not allowed");
072: }
073: }
074: }
075: if (mapping instanceof XmlContentMapping) {
076: xmlContentMapping = (XmlContentMapping) mapping;
077: }
078: return super .addMapping(mapping);
079: }
080:
081: protected void doPostProcess() throws MappingException {
082: resourcePropertyMappings = new ResourcePropertyMapping[mappingsSize()];
083: int i = 0;
084: for (Iterator it = mappingsIt(); it.hasNext();) {
085: resourcePropertyMappings[i++] = (ResourcePropertyMapping) it
086: .next();
087: }
088: }
089:
090: public ResourcePropertyMapping getResourcePropertyMappingByDotPath(
091: String path) {
092: return (ResourcePropertyMapping) mappingsByNameMap.get(path);
093: }
094:
095: public ResourcePropertyMapping[] getResourcePropertyMappings() {
096: return resourcePropertyMappings;
097: }
098:
099: public String getXPath() {
100: return xpath;
101: }
102:
103: public void setXPath(String xpath) {
104: this .xpath = xpath;
105: }
106:
107: public XmlXPathExpression getXPathExpression() {
108: return xpathExpression;
109: }
110:
111: public void setXPathExpression(XmlXPathExpression xpathExpression) {
112: this .xpathExpression = xpathExpression;
113: }
114:
115: /**
116: * Returns the xml content mapping (might be <code>null</code>).
117: */
118: public XmlContentMapping getXmlContentMapping() {
119: return xmlContentMapping;
120: }
121: }
|