001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.configuration2.builder;
018
019import javax.xml.parsers.DocumentBuilder;
020
021import java.util.Map;
022
023import org.xml.sax.EntityResolver;
024
025/**
026 * <p>
027 * A specialized parameters class for XML configuration.
028 * </p>
029 * <p>
030 * This parameters class defines some properties which allow customizing the
031 * parsing of XML documents. The location of the XML document to be loaded can
032 * be specified, too.
033 * </p>
034 * <p>
035 * This class is not thread-safe. It is intended that an instance is constructed
036 * and initialized by a single thread during configuration of a
037 * {@code ConfigurationBuilder}.
038 * </p>
039 *
040 * @since 2.0
041 */
042public class XMLBuilderParametersImpl extends HierarchicalBuilderParametersImpl
043        implements XMLBuilderProperties<XMLBuilderParametersImpl>
044{
045    /** The key for the entity resolver property. */
046    private static final String PROP_ENTITY_RESOLVER = "entityResolver";
047
048    /** The key for the document builder property. */
049    private static final String PROP_DOCUMENT_BUILDER = "documentBuilder";
050
051    /** The key for the public ID property. */
052    private static final String PROP_PUBLIC_ID = "publicID";
053
054    /** The key for the system ID property. */
055    private static final String PROP_SYSTEM_ID = "systemID";
056
057    /** The key for the validating property. */
058    private static final String PROP_VALIDATING = "validating";
059
060    /** The key for the schema validation flag. */
061    private static final String PROP_SCHEMA_VALIDATION = "schemaValidation";
062
063    @Override
064    public void inheritFrom(final Map<String, ?> source)
065    {
066        super.inheritFrom(source);
067        copyPropertiesFrom(source, PROP_DOCUMENT_BUILDER, PROP_ENTITY_RESOLVER,
068                PROP_SCHEMA_VALIDATION, PROP_VALIDATING);
069    }
070
071    @Override
072    public XMLBuilderParametersImpl setDocumentBuilder(
073            final DocumentBuilder docBuilder)
074    {
075        storeProperty(PROP_DOCUMENT_BUILDER, docBuilder);
076        return this;
077    }
078
079    @Override
080    public XMLBuilderParametersImpl setEntityResolver(final EntityResolver resolver)
081    {
082        storeProperty(PROP_ENTITY_RESOLVER, resolver);
083        return this;
084    }
085
086    /**
087     * Returns the {@code EntityResolver} stored in this parameters object.
088     * Result is <b>null</b> if no such object has been set.
089     *
090     * @return the {@code EntityResolver} or <b>null</b>
091     */
092    public EntityResolver getEntityResolver()
093    {
094        return (EntityResolver) fetchProperty(PROP_ENTITY_RESOLVER);
095    }
096
097    @Override
098    public XMLBuilderParametersImpl setPublicID(final String pubID)
099    {
100        storeProperty(PROP_PUBLIC_ID, pubID);
101        return this;
102    }
103
104    @Override
105    public XMLBuilderParametersImpl setSystemID(final String sysID)
106    {
107        storeProperty(PROP_SYSTEM_ID, sysID);
108        return this;
109    }
110
111    @Override
112    public XMLBuilderParametersImpl setValidating(final boolean f)
113    {
114        storeProperty(PROP_VALIDATING, Boolean.valueOf(f));
115        return this;
116    }
117
118    @Override
119    public XMLBuilderParametersImpl setSchemaValidation(final boolean f)
120    {
121        storeProperty(PROP_SCHEMA_VALIDATION, Boolean.valueOf(f));
122        return this;
123    }
124}