Package org.apache.xindice.webadmin.util

Source Code of org.apache.xindice.webadmin.util.CollectionConfigurationHelper

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $Id: CollectionConfigurationHelper.java 541515 2007-05-25 02:45:06Z vgritsenko $
*/

package org.apache.xindice.webadmin.util;

import org.apache.xindice.core.Collection;
import org.apache.xindice.util.Configuration;
import org.apache.xindice.util.ReadOnlyException;
import org.apache.xindice.xml.TextWriter;
import org.apache.xindice.xml.dom.DocumentImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
* Helper class for oprations on collection configuration
*
* @author <a href="mailto:jmetzner@apache.org">Jan Metzner</a>
* @version $Revision: 541515 $, $Date: 2007-05-24 22:45:06 -0400 (Thu, 24 May 2007) $
*/
public class CollectionConfigurationHelper {

    private static final Log log = LogFactory.getLog(CollectionConfigurationHelper.class);
    public static final String CONF_ELE = "collection";
    public static final String COL_NAME_ATTR = "name";
    public static final String INLINE_META_ATTR = "inline-metadata";
    public static final String COMPRESSED_ATTR = "compressed";
    public static final String FILER_ELE = "filer";
    public static final String FILER_CLASS_ATTR = "class";
    public static final boolean DEFAULT_INLINE_META = true;
    public static final boolean DEFAULT_COMPRESSED = true;
    public static final String DEFAULT_FILER_CLASS = "org.apache.xindice.core.filer.BTreeFiler";

    /**
     * Configures the CollectionConfigurationHelper with the default
     * Collection Configuration. Example:
     * <pre>
     * &lt;col-config id="col-config" autoenableinlinemeta="true"&gt;
     *   &lt;collection compressed="true" inline-meta="true"&gt;
     *     &lt;filer class="org.apache.xindice.core.filer.BTreeFiler" /&gt;
     *   &lt;/collection&gt;
     * &lt;/col-config&gt;
     * </pre>
     * A name Attribute will be ignored.
     * If the Attribute autoenableinlinemeta is set true, the Collection is not Inline
     * Metadata enabled and a Binary is inserted, the Collection will be automatically
     * Inline Metadata enabled.
     */
    public static boolean getDefaultInlineMetadata() {
        return DEFAULT_INLINE_META;
    }

    public static boolean getDefaultCompressed() {
        return DEFAULT_COMPRESSED;
    }

    public static String getDefaultFilerClass() {
        return DEFAULT_FILER_CLASS;
    }

    public static boolean isInlineMetaEnabled(final Collection col) {
        Configuration config = col.getConfig();
        return config.getBooleanAttribute(INLINE_META_ATTR, false);
    }

    public static Configuration createDefaultConfiguration(String name) {
        return createConfiguration(name, null, null, null);
    }

    public static Configuration createConfiguration(String name, String compressed, String inlineMeta, String filerClass) {
        Document doc = new DocumentImpl();
        Element colEle = doc.createElement(CONF_ELE);
        if (name == null || name.length() == 0) {
            throw new IllegalArgumentException("null name");
        }
        colEle.setAttribute(COL_NAME_ATTR, name);
        if (compressed == null || (!compressed.equalsIgnoreCase("true") && !compressed.equalsIgnoreCase("false"))) {
            compressed = Boolean.toString(DEFAULT_COMPRESSED);
        }
        colEle.setAttribute(COMPRESSED_ATTR, compressed);
        if (inlineMeta == null || (!inlineMeta.equalsIgnoreCase("true") && !inlineMeta.equalsIgnoreCase("false"))) {
            inlineMeta = Boolean.toString(DEFAULT_INLINE_META);
        }
        colEle.setAttribute(INLINE_META_ATTR, inlineMeta);
        doc.appendChild(colEle);
        Element filEle = doc.createElement(FILER_ELE);
        if (filerClass == null || filerClass.length() == 0) {
            filerClass = DEFAULT_FILER_CLASS;
        }
        filEle.setAttribute(FILER_CLASS_ATTR, filerClass);
        colEle.appendChild(filEle);
        if (log.isDebugEnabled()) {
            log.debug("Created Configuration: \n" + TextWriter.toString(doc));
        }
        return new Configuration(doc.getDocumentElement(), false);
    }

    public static Configuration copyConfiguration(final Configuration srcConfig) {
        Configuration newConfig = createConfiguration();
        try {
            return copyConfigurationContent(srcConfig, newConfig);
        } catch (ReadOnlyException e) {
            log.error(e);
            return null;
        }
    }

    public static Configuration copyConfiguration(String name, final Configuration srcConfig) {
        Configuration destConfig = createConfiguration();
        try {
            copyConfigurationContent(srcConfig, destConfig);
            destConfig.setAttribute(COL_NAME_ATTR, name);
        } catch (ReadOnlyException e) {
            // ignore, cannot happen
        }

        return destConfig;
    }

    private static Configuration copyConfigurationContent(final Configuration srcConfig, Configuration destConfig)
        throws ReadOnlyException {

        if (srcConfig.hasAttributes()) {
            String[] attrs = srcConfig.listAttributes();
            for (int i = 0; i < attrs.length; i++) {
                destConfig.setAttribute(attrs[i], srcConfig.getAttribute(attrs[i]));
            }
        }
        if (srcConfig.hasValue()) {
            String value = srcConfig.getValue(null);
            if (value != null) {
                destConfig.setValue(value);
            }
        }
        if (srcConfig.hasChildren()) {
            Configuration[] childConf = srcConfig.getChildren();
            for (int i = 0; i < childConf.length; i++) {
                Configuration childDestConfig = destConfig.getChild(childConf[i].getName(), true);
                copyConfigurationContent(childConf[i], childDestConfig);
            }
        }

        return destConfig;
    }

    private static Configuration createConfiguration() {
        Document doc = new DocumentImpl();
        return new Configuration(doc.createElement(CONF_ELE), false);
    }
}
TOP

Related Classes of org.apache.xindice.webadmin.util.CollectionConfigurationHelper

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.