001package ball.xml;
002/*-
003 * ##########################################################################
004 * Utilities
005 * $Id: FluentDocumentBuilderFactory.html 5431 2020-02-12 19:03:17Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/hcf-dev/blog/2019-03-30-java-interface-facades/src/main/resources/javadoc/src-html/ball/xml/FluentDocumentBuilderFactory.html $
007 * %%
008 * Copyright (C) 2008 - 2020 Allen D. Ball
009 * %%
010 * Licensed under the Apache License, Version 2.0 (the "License");
011 * you may not use this file except in compliance with the License.
012 * You may obtain a copy of the License at
013 *
014 *      http://www.apache.org/licenses/LICENSE-2.0
015 *
016 * Unless required by applicable law or agreed to in writing, software
017 * distributed under the License is distributed on an "AS IS" BASIS,
018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019 * See the License for the specific language governing permissions and
020 * limitations under the License.
021 * ##########################################################################
022 */
023import javax.xml.parsers.DocumentBuilderFactory;
024import javax.xml.parsers.FactoryConfigurationError;
025import javax.xml.parsers.ParserConfigurationException;
026
027import static java.util.Objects.requireNonNull;
028
029/**
030 * {@link FluentDocument.Builder} {@link DocumentBuilderFactory}.
031 *
032 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
033 * @version $Revision: 5431 $
034 */
035public class FluentDocumentBuilderFactory extends DocumentBuilderFactory {
036    private final DocumentBuilderFactory factory;
037
038    /**
039     * See {@link DocumentBuilderFactory#newInstance()}.
040     *
041     * @return  {@link FluentDocumentBuilderFactory}
042     */
043    public static FluentDocumentBuilderFactory newInstance() throws FactoryConfigurationError {
044        return new FluentDocumentBuilderFactory();
045    }
046
047    /**
048     * See {@link DocumentBuilderFactory#newInstance(String,ClassLoader)}.
049     *
050     * @param   name            The {@link Class} name.
051     * @param   loader          The {@link ClassLoader}.
052     *
053     * @return  {@link FluentDocumentBuilderFactory}
054     */
055    public static FluentDocumentBuilderFactory newInstance(String name,
056                                                           ClassLoader loader) throws FactoryConfigurationError {
057        DocumentBuilderFactory factory =
058            DocumentBuilderFactory.newInstance(name, loader);
059
060        if (! (factory instanceof FluentDocumentBuilderFactory)) {
061            factory = new FluentDocumentBuilderFactory(factory);
062        }
063
064        return (FluentDocumentBuilderFactory) factory;
065    }
066
067    /**
068     * Sole public constructor.
069     */
070    public FluentDocumentBuilderFactory() throws FactoryConfigurationError {
071        this(DocumentBuilderFactory.newInstance());
072    }
073
074    private FluentDocumentBuilderFactory(DocumentBuilderFactory factory) {
075        super();
076
077        this.factory = requireNonNull(factory);
078    }
079
080    @Override
081    public void setAttribute(String name,
082                             Object value) throws IllegalArgumentException {
083        factory.setAttribute(name, value);
084    }
085
086    @Override
087    public Object getAttribute(String name) throws IllegalArgumentException {
088        return factory.getAttribute(name);
089    }
090
091    @Override
092    public FluentDocument.Builder newDocumentBuilder() throws ParserConfigurationException {
093        return new FluentDocument.Builder(factory.newDocumentBuilder());
094    }
095
096    @Override
097    public void setFeature(String name,
098                           boolean value) throws ParserConfigurationException {
099        factory.setFeature(name, value);
100    }
101
102    @Override
103    public boolean getFeature(String name) throws ParserConfigurationException {
104        return factory.getFeature(name);
105    }
106}