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.extended;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.ByteArrayOutputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023:
024: import org.compass.core.Property;
025: import org.compass.core.Resource;
026: import org.compass.core.ResourceFactory;
027: import org.compass.core.converter.ConversionException;
028: import org.compass.core.converter.Converter;
029: import org.compass.core.mapping.Mapping;
030: import org.compass.core.mapping.ResourcePropertyMapping;
031: import org.compass.core.marshall.MarshallingContext;
032:
033: /**
034: * @author kimchy
035: */
036: public class InputStreamConverter implements Converter {
037:
038: private static final int DEFAULT_BUFFER_SIZE = 1024 * 1;
039:
040: public boolean marshall(Resource resource, Object root,
041: Mapping mapping, MarshallingContext context)
042: throws ConversionException {
043:
044: ResourcePropertyMapping resourcePropertyMapping = (ResourcePropertyMapping) mapping;
045: ResourceFactory resourceFactory = context.getResourceFactory();
046:
047: // don't save a null value
048: if (root == null) {
049: return false;
050: }
051:
052: String propertyName = resourcePropertyMapping.getPath()
053: .getPath();
054:
055: InputStream input = (InputStream) root;
056: ByteArrayOutputStream output = new ByteArrayOutputStream();
057:
058: byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
059: int n = 0;
060: try {
061: while (-1 != (n = input.read(buffer))) {
062: output.write(buffer, 0, n);
063: }
064: } catch (IOException e) {
065: throw new ConversionException(
066: "Failed to read input stream for [" + propertyName
067: + "]", e);
068: }
069:
070: Property p = resourceFactory.createProperty(propertyName,
071: output.toByteArray(), resourcePropertyMapping
072: .getStore());
073: p.setBoost(resourcePropertyMapping.getBoost());
074: resource.addProperty(p);
075:
076: return resourcePropertyMapping.getStore() != Property.Store.NO;
077: }
078:
079: public Object unmarshall(Resource resource, Mapping mapping,
080: MarshallingContext context) throws ConversionException {
081: ResourcePropertyMapping resourcePropertyMapping = (ResourcePropertyMapping) mapping;
082:
083: if (resourcePropertyMapping.getStore() == Property.Store.NO) {
084: // it is not stored, so don't bother with converting it
085: return null;
086: }
087:
088: String propertyName = resourcePropertyMapping.getPath()
089: .getPath();
090: Property p = resource.getProperty(propertyName);
091:
092: // don't set anything if null
093: if (p == null) {
094: return null;
095: }
096:
097: return new ByteArrayInputStream(p.getBinaryValue());
098: }
099:
100: }
|