/* * Copyright (C) 2005-2007 Alfresco Software Limited. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * As a special exception to the terms and conditions of version 2.0 of * the GPL, you may redistribute this Program in connection with Free/Libre * and Open Source Software ("FLOSS") applications as described in Alfresco's * FLOSS exception. You should have recieved a copy of the text describing * the FLOSS exception, and it is also available here: * http://www.alfresco.com/legal/licensing" */ package org.alfresco.repo.content.metadata; import java.io.InputStream; import java.io.Serializable; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Properties; import java.util.Set; import java.util.StringTokenizer; import org.alfresco.error.AlfrescoRuntimeException; import org.alfresco.service.cmr.repository.ContentIOException; import org.alfresco.service.cmr.repository.ContentReader; import org.alfresco.service.cmr.repository.MimetypeService; import org.alfresco.service.namespace.InvalidQNameException; import org.alfresco.service.namespace.QName; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * Support class for metadata extracters that support dynamic and config-driven * mapping between extracted values and model properties. Extraction is broken * up into two phases: *
* Migrating an existing extracter to use this class is straightforward: *
1.0
if the mimetype is supported, otherwise 0.0
*
* @see #isSupported(String)
*/
public double getReliability(String mimetype)
{
return isSupported(mimetype) ? 1.0D : 0.0D;
}
/**
* @param overwritePolicy the policy to apply when there are existing system properties
*/
public void setOverwritePolicy(OverwritePolicy overwritePolicy)
{
this.overwritePolicy = overwritePolicy;
}
/**
* Set if the property mappings augment or override the mapping generically provided by the
* extracter implementation. The default is false, i.e. any mapping set completely
* replaces the {@link #getDefaultMapping() default mappings}.
*
* @param inheritDefaultMapping true to add the configured mapping
* to the list of default mappings.
*
* @see #getDefaultMapping()
* @see #setMapping(Map)
* @see #setMappingProperties(Properties)
*/
public void setInheritDefaultMapping(boolean inheritDefaultMapping)
{
this.inheritDefaultMapping = inheritDefaultMapping;
}
/**
* Set the mapping from document metadata to system metadata. It is possible to direct
* an extracted document property to several system properties. The conversion between
* the document property types and the system property types will be done by the
* {@link org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter default converter}.
*
* @param mapping a mapping from document metadata to system metadata
*/
public void setMapping(Map* # Namespaces prefixes * namespace.prefix.cm=http://www.alfresco.org/model/content/1.0 * namespace.prefix.my=http://www....com/alfresco/1.0 * * # Mapping * editor=cm:author, my:editor * title=cm:title * user1=cm:summary * user2=cm:description ** The mapping can therefore be from a single document property onto several system properties. * * @param mappingProperties the properties that map document properties to system properties */ public void setMappingProperties(Properties mappingProperties) { mapping = readMappingProperties(mappingProperties); } /** * A utility method to read mapping properties from a resource file and convert to the map form. * * @param propertiesUrl A standard Properties file URL location * * @see #setMappingProperties(Properties) */ protected Map
toString
representation used directly.
*
* @param key the destination map key
* @param value the value to check and put.
* @param destination map to put values into
* @return Returns true if set, false otherwise
*/
protected boolean putSafeRawValue(String key, Object value, Map* The default implementation looks for the default mapping file in the location * given by the class name and .properties. If the extracter's class is * x.y.z.MyExtracter then the default properties will be picked up at * classpath:/x/y/z/MyExtracter.properties. * Inner classes are supported, but the '$' in the class name is replaced with '-', so * default properties for x.y.z.MyStuff$MyExtracter will be located using * x.y.z.MyStuff-MyExtracter.properties. *
* The default mapping implementation should include thorough Javadocs so that the * system administrators can accurately determine how to best enhance or override the * default mapping. *
* If the default mapping is declared in a properties file, then the * {@link #readMappingProperties(String)} method can be used to quickly generate the * return value: *
* protected Map* The map can also be created in code either statically or during the call. * * @return Returns the default, static mapping. It may not be null. * * @see #setInheritDefaultMapping(boolean inherit) */ protected Map> getDefaultMapping() * { * return readMappingProperties(DEFAULT_MAPPING); * } *
* Raw values must not be trimmed or removed for any reason. Null values and empty * strings are *
* Properties extracted and their meanings and types should be thoroughly described in * the class-level javadocs of the extracter implementation, for example: *
* editor: - the document editor --> cm:author * title: - the document title --> cm:title * user1: - the document summary * user2: - the document description --> cm:description * user3: - * user4: - ** * @param reader the document to extract the values from. This stream provided by * the reader must be closed if accessed directly. * @return Returns a map of document property values keyed by property name. * @throws All exception conditions can be handled. * * @see #getDefaultMapping() */ protected abstract Map