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.converter.xsem;
018:
019: import org.compass.core.Property;
020: import org.compass.core.Resource;
021: import org.compass.core.converter.ConversionException;
022: import org.compass.core.converter.Converter;
023: import org.compass.core.engine.naming.PropertyPath;
024: import org.compass.core.mapping.Mapping;
025: import org.compass.core.mapping.ResourcePropertyMapping;
026: import org.compass.core.mapping.xsem.XmlPropertyMapping;
027: import org.compass.core.marshall.MarshallingContext;
028: import org.compass.core.xml.XmlObject;
029:
030: /**
031: * A simple converter which uses the String value of {@link org.compass.core.xml.XmlObject#getValue()}.
032: * It is the deafult converter associated with the value converter of {@link XmlPropertyMapping} if none
033: * is specified. It can also be used as a base class for more specialized converters.
034: *
035: * @author kimchy
036: */
037: public class SimpleXmlValueConverter implements Converter {
038:
039: public boolean marshall(Resource resource, Object root,
040: Mapping mapping, MarshallingContext context)
041: throws ConversionException {
042: XmlPropertyMapping xmlPropertyMapping = (XmlPropertyMapping) mapping;
043: // don't save a null value if the context does not states so
044: if (root == null && !handleNulls(xmlPropertyMapping, context)) {
045: return false;
046: }
047: XmlObject xmlObject = (XmlObject) root;
048: String sValue = getNullValue(xmlPropertyMapping, context);
049: if (root != null) {
050: sValue = toString(xmlObject, xmlPropertyMapping);
051: }
052: PropertyPath path = xmlPropertyMapping.getPath();
053: String propertyName = path == null ? null : path.getPath();
054: if (propertyName == null) {
055: if (xmlObject == null) {
056: // nothing we can do here, no name, no nothing...
057: return false;
058: }
059: propertyName = xmlObject.getName();
060: }
061: Property p = context.getResourceFactory().createProperty(
062: propertyName, sValue, xmlPropertyMapping);
063: doSetBoost(p, root, xmlPropertyMapping, context);
064: resource.addProperty(p);
065:
066: return xmlPropertyMapping.getStore() != Property.Store.NO;
067: }
068:
069: /**
070: * <p>Should the converter handle nulls? Handling nulls means should the
071: * converter process nulls or not. Usually the converter will not
072: * persist null values, but sometimes it might be needed
073: * ({@link org.compass.core.marshall.MarshallingContext#handleNulls()}).
074: *
075: * <p>If a specific null value is configured with the {@link org.compass.core.mapping.ResourcePropertyMapping}
076: * then the converter will always handle nulls and write it.
077: *
078: * @param context The marshalling context
079: * @return <code>true</code> if the converter should handle null values
080: */
081: protected boolean handleNulls(
082: ResourcePropertyMapping resourcePropertyMapping,
083: MarshallingContext context) {
084: return resourcePropertyMapping.hasNullValue()
085: || context.handleNulls();
086: }
087:
088: /**
089: * If the converter handle nulls, the value that will be stored in the
090: * search engine for <code>null</code> values (during the marshall process). Uses
091: * {@link org.compass.core.mapping.ResourcePropertyMapping#getNullValue()}.
092: *
093: * @param resourcePropertyMapping The resource proeprty mapping to get the null value from
094: * @param context The marshalling context
095: * @return Null value that will be inserted for <code>null</code>s.
096: */
097: protected String getNullValue(
098: ResourcePropertyMapping resourcePropertyMapping,
099: MarshallingContext context) {
100: return resourcePropertyMapping.getNullValue();
101: }
102:
103: /**
104: * A simple extension point that allows to set the boost value for the created {@link Property}.
105: * <p/>
106: * The default implemenation uses the statically defined boost value in the mapping definition
107: * ({@link org.compass.core.mapping.ResourcePropertyMapping#getBoost()}) to set the boost level
108: * using {@link Property#setBoost(float)}
109: *
110: * @param property The property to set the boost on
111: * @param root The object that is marshalled into a property
112: * @param resourcePropertyMapping The Resource Property Mapping definition
113: * @throws ConversionException
114: */
115: protected void doSetBoost(Property property, Object root,
116: ResourcePropertyMapping resourcePropertyMapping,
117: MarshallingContext context) throws ConversionException {
118: property.setBoost(resourcePropertyMapping.getBoost());
119: }
120:
121: /**
122: * Default implementation of toString, simply calls {@link org.compass.core.xml.XmlObject#getValue()}.
123: */
124: public String toString(XmlObject xmlObject,
125: ResourcePropertyMapping resourcePropertyMapping) {
126: return xmlObject.getValue();
127: }
128:
129: /**
130: * Not supported operation.
131: */
132: public Object unmarshall(Resource resource, Mapping mapping,
133: MarshallingContext context) throws ConversionException {
134: return new ConversionException("Not supported");
135: }
136: }
|