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 java.io.IOException;
022import java.util.stream.Stream;
023import javax.xml.parsers.DocumentBuilder;
024import lombok.NonNull;
025import lombok.RequiredArgsConstructor;
026import lombok.ToString;
027import org.w3c.dom.DOMImplementation;
028import org.w3c.dom.Document;
029import org.w3c.dom.Node;
030import org.xml.sax.EntityResolver;
031import org.xml.sax.ErrorHandler;
032import org.xml.sax.InputSource;
033import org.xml.sax.SAXException;
034
035/**
036 * Fluent {@link Document} interface.  Note: This interface is an
037 * implementation detail of {@link FluentDocument.Builder} and should not be
038 * extended directly.
039 *
040 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
041 */
042public interface FluentDocument extends FluentNode, Document {
043    @Override
044    default FluentDocument owner() { return this; }
045
046    @Override
047    default FluentDocument add(Stream<Node> stream) {
048        return add(stream.toArray(Node[]::new));
049    }
050
051    @Override
052    default FluentDocument add(Node... nodes) {
053        return (FluentDocument) FluentNode.super.add(nodes);
054    }
055
056    /**
057     * {@link FluentDocument} {@link DocumentBuilder}.
058     */
059    @RequiredArgsConstructor @ToString
060    public class Builder extends DocumentBuilder {
061        @NonNull private final DocumentBuilder builder;
062
063        @Override
064        public FluentDocument newDocument() {
065            Document document = builder.newDocument();
066
067            return (FluentDocument) new InvocationHandler().enhance(document);
068        }
069
070        @Override
071        public Document parse(InputSource in) throws SAXException, IOException {
072            Document document = builder.parse(in);
073
074            return (FluentDocument) new InvocationHandler().enhance(document);
075        }
076
077        @Override
078        public boolean isNamespaceAware() {
079            return builder.isNamespaceAware();
080        }
081
082        @Override
083        public boolean isValidating() { return builder.isValidating(); }
084
085        @Override
086        public void setEntityResolver(EntityResolver resolver) {
087            builder.setEntityResolver(resolver);
088        }
089
090        @Override
091        public void setErrorHandler(ErrorHandler handler) {
092            builder.setErrorHandler(handler);
093        }
094
095        @Override
096        public DOMImplementation getDOMImplementation() {
097            return builder.getDOMImplementation();
098        }
099    }
100}