Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
file stats: LOC: 167   Methods: 10
NCLOC: 110   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
CollectionConfigurationHelper.java 63.6% 75% 60% 70.2%
coverage coverage
 1    /*
 2    * Licensed to the Apache Software Foundation (ASF) under one or more
 3    * contributor license agreements. See the NOTICE file distributed with
 4    * this work for additional information regarding copyright ownership.
 5    * The ASF licenses this file to You under the Apache License, Version 2.0
 6    * (the "License"); you may not use this file except in compliance with
 7    * the License. You may obtain a copy of the License at
 8    *
 9    * http://www.apache.org/licenses/LICENSE-2.0
 10    *
 11    * Unless required by applicable law or agreed to in writing, software
 12    * distributed under the License is distributed on an "AS IS" BASIS,
 13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14    * See the License for the specific language governing permissions and
 15    * limitations under the License.
 16    *
 17    * $Id: CollectionConfigurationHelper.java 541515 2007-05-25 02:45:06Z vgritsenko $
 18    */
 19   
 20    package org.apache.xindice.webadmin.util;
 21   
 22    import org.apache.xindice.core.Collection;
 23    import org.apache.xindice.util.Configuration;
 24    import org.apache.xindice.util.ReadOnlyException;
 25    import org.apache.xindice.xml.TextWriter;
 26    import org.apache.xindice.xml.dom.DocumentImpl;
 27    import org.apache.commons.logging.Log;
 28    import org.apache.commons.logging.LogFactory;
 29    import org.w3c.dom.Document;
 30    import org.w3c.dom.Element;
 31   
 32    /**
 33    * Helper class for oprations on collection configuration
 34    *
 35    * @author <a href="mailto:jmetzner@apache.org">Jan Metzner</a>
 36    * @version $Revision: 541515 $, $Date: 2007-05-24 19:45:06 -0700 (Thu, 24 May 2007) $
 37    */
 38    public class CollectionConfigurationHelper {
 39   
 40    private static final Log log = LogFactory.getLog(CollectionConfigurationHelper.class);
 41    public static final String CONF_ELE = "collection";
 42    public static final String COL_NAME_ATTR = "name";
 43    public static final String INLINE_META_ATTR = "inline-metadata";
 44    public static final String COMPRESSED_ATTR = "compressed";
 45    public static final String FILER_ELE = "filer";
 46    public static final String FILER_CLASS_ATTR = "class";
 47    public static final boolean DEFAULT_INLINE_META = true;
 48    public static final boolean DEFAULT_COMPRESSED = true;
 49    public static final String DEFAULT_FILER_CLASS = "org.apache.xindice.core.filer.BTreeFiler";
 50   
 51    /**
 52    * Configures the CollectionConfigurationHelper with the default
 53    * Collection Configuration. Example:
 54    * <pre>
 55    * &lt;col-config id="col-config" autoenableinlinemeta="true"&gt;
 56    * &lt;collection compressed="true" inline-meta="true"&gt;
 57    * &lt;filer class="org.apache.xindice.core.filer.BTreeFiler" /&gt;
 58    * &lt;/collection&gt;
 59    * &lt;/col-config&gt;
 60    * </pre>
 61    * A name Attribute will be ignored.
 62    * If the Attribute autoenableinlinemeta is set true, the Collection is not Inline
 63    * Metadata enabled and a Binary is inserted, the Collection will be automatically
 64    * Inline Metadata enabled.
 65    */
 66  0 public static boolean getDefaultInlineMetadata() {
 67  0 return DEFAULT_INLINE_META;
 68    }
 69   
 70  0 public static boolean getDefaultCompressed() {
 71  0 return DEFAULT_COMPRESSED;
 72    }
 73   
 74  0 public static String getDefaultFilerClass() {
 75  0 return DEFAULT_FILER_CLASS;
 76    }
 77   
 78  2 public static boolean isInlineMetaEnabled(final Collection col) {
 79  2 Configuration config = col.getConfig();
 80  2 return config.getBooleanAttribute(INLINE_META_ATTR, false);
 81    }
 82   
 83  1 public static Configuration createDefaultConfiguration(String name) {
 84  1 return createConfiguration(name, null, null, null);
 85    }
 86   
 87  1 public static Configuration createConfiguration(String name, String compressed, String inlineMeta, String filerClass) {
 88  1 Document doc = new DocumentImpl();
 89  1 Element colEle = doc.createElement(CONF_ELE);
 90  1 if (name == null || name.length() == 0) {
 91  0 throw new IllegalArgumentException("null name");
 92    }
 93  1 colEle.setAttribute(COL_NAME_ATTR, name);
 94  1 if (compressed == null || (!compressed.equalsIgnoreCase("true") && !compressed.equalsIgnoreCase("false"))) {
 95  1 compressed = Boolean.toString(DEFAULT_COMPRESSED);
 96    }
 97  1 colEle.setAttribute(COMPRESSED_ATTR, compressed);
 98  1 if (inlineMeta == null || (!inlineMeta.equalsIgnoreCase("true") && !inlineMeta.equalsIgnoreCase("false"))) {
 99  1 inlineMeta = Boolean.toString(DEFAULT_INLINE_META);
 100    }
 101  1 colEle.setAttribute(INLINE_META_ATTR, inlineMeta);
 102  1 doc.appendChild(colEle);
 103  1 Element filEle = doc.createElement(FILER_ELE);
 104  1 if (filerClass == null || filerClass.length() == 0) {
 105  1 filerClass = DEFAULT_FILER_CLASS;
 106    }
 107  1 filEle.setAttribute(FILER_CLASS_ATTR, filerClass);
 108  1 colEle.appendChild(filEle);
 109  1 if (log.isDebugEnabled()) {
 110  0 log.debug("Created Configuration: \n" + TextWriter.toString(doc));
 111    }
 112  1 return new Configuration(doc.getDocumentElement(), false);
 113    }
 114   
 115  0 public static Configuration copyConfiguration(final Configuration srcConfig) {
 116  0 Configuration newConfig = createConfiguration();
 117  0 try {
 118  0 return copyConfigurationContent(srcConfig, newConfig);
 119    } catch (ReadOnlyException e) {
 120  0 log.error(e);
 121  0 return null;
 122    }
 123    }
 124   
 125  3 public static Configuration copyConfiguration(String name, final Configuration srcConfig) {
 126  3 Configuration destConfig = createConfiguration();
 127  3 try {
 128  3 copyConfigurationContent(srcConfig, destConfig);
 129  3 destConfig.setAttribute(COL_NAME_ATTR, name);
 130    } catch (ReadOnlyException e) {
 131    // ignore, cannot happen
 132    }
 133   
 134  3 return destConfig;
 135    }
 136   
 137  17 private static Configuration copyConfigurationContent(final Configuration srcConfig, Configuration destConfig)
 138    throws ReadOnlyException {
 139   
 140  17 if (srcConfig.hasAttributes()) {
 141  10 String[] attrs = srcConfig.listAttributes();
 142  10 for (int i = 0; i < attrs.length; i++) {
 143  20 destConfig.setAttribute(attrs[i], srcConfig.getAttribute(attrs[i]));
 144    }
 145    }
 146  17 if (srcConfig.hasValue()) {
 147  0 String value = srcConfig.getValue(null);
 148  0 if (value != null) {
 149  0 destConfig.setValue(value);
 150    }
 151    }
 152  17 if (srcConfig.hasChildren()) {
 153  7 Configuration[] childConf = srcConfig.getChildren();
 154  7 for (int i = 0; i < childConf.length; i++) {
 155  14 Configuration childDestConfig = destConfig.getChild(childConf[i].getName(), true);
 156  14 copyConfigurationContent(childConf[i], childDestConfig);
 157    }
 158    }
 159   
 160  17 return destConfig;
 161    }
 162   
 163  3 private static Configuration createConfiguration() {
 164  3 Document doc = new DocumentImpl();
 165  3 return new Configuration(doc.createElement(CONF_ELE), false);
 166    }
 167    }