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