001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and others.
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 * Contributors:
017 *    Mariana Cedica
018 */
019package org.nuxeo.ecm.platform.importer.properties;
020
021import java.io.File;
022import java.io.FileInputStream;
023import java.io.IOException;
024import java.io.Serializable;
025import java.util.Enumeration;
026import java.util.HashMap;
027import java.util.Map;
028import java.util.Properties;
029
030/**
031 * The properties are mapped by the collector using as key the path of the file/folder to import.
032 */
033public class IndividualMetadataCollector extends MetadataCollector {
034
035    @Override
036    public void addPropertyFile(File propertyFile) throws IOException {
037        String contextPath = propertyFile.getAbsolutePath();
038        addPropertyFile(propertyFile, contextPath);
039    }
040
041    public void addPropertyFile(File propertyFile, String contextPath) throws IOException {
042        Properties mdProperties = new Properties();
043        mdProperties.load(new FileInputStream(propertyFile));
044        Map<String, String> stringMap = new HashMap<String, String>();
045        Enumeration<?> names = mdProperties.propertyNames();
046        while (names.hasMoreElements()) {
047            String name = (String) names.nextElement();
048            stringMap.put(name, mdProperties.getProperty(name));
049        }
050        addPropertiesFromStrings(contextPath, stringMap);
051    }
052
053    public Map<String, Serializable> getProperties(File file) {
054        return getProperties(file.getPath());
055    }
056}