001/*
002 * $Id: FluentDocument.Builder.html 3972 2019-03-30 22:20:34Z ball $
003 *
004 * Copyright 2019 Allen D. Ball.  All rights reserved.
005 */
006package ball.xml;
007
008import java.io.IOException;
009import java.util.stream.Stream;
010import javax.xml.parsers.DocumentBuilder;
011import org.w3c.dom.DOMImplementation;
012import org.w3c.dom.Document;
013import org.w3c.dom.Node;
014import org.xml.sax.EntityResolver;
015import org.xml.sax.ErrorHandler;
016import org.xml.sax.InputSource;
017import org.xml.sax.SAXException;
018
019import static java.util.Objects.requireNonNull;
020
021/**
022 * Fluent {@link Document} interface.  Note: This interface is an
023 * implementation detail of {@link FluentDocument.Builder} and should not be
024 * extended directly.
025 *
026 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
027 * @version $Revision: 3972 $
028 */
029public interface FluentDocument extends FluentNode, Document {
030    @Override
031    default FluentDocument owner() { return this; }
032
033    @Override
034    default FluentDocument add(Stream<Node> stream) {
035        return add(stream.toArray(Node[]::new));
036    }
037
038    @Override
039    default FluentDocument add(Iterable<Node> iterable) {
040        return (FluentDocument) FluentNode.super.add(iterable);
041    }
042
043    @Override
044    default FluentDocument add(Node... nodes) {
045        return (FluentDocument) FluentNode.super.add(nodes);
046    }
047
048    /**
049     * {@link FluentDocument} {@link DocumentBuilder}
050     */
051    public class Builder extends DocumentBuilder {
052        private final DocumentBuilder builder;
053
054        /**
055         * Sole constructor.
056         *
057         * @param   builder         The "wrapped" {@link DocumentBuilder}.
058         */
059        protected Builder(DocumentBuilder builder) {
060            super();
061
062            this.builder = requireNonNull(builder);
063        }
064
065        @Override
066        public FluentDocument newDocument() {
067            Document document = builder.newDocument();
068
069            return (FluentDocument) new InvocationHandler().enhance(document);
070        }
071
072        @Override
073        public Document parse(InputSource in) throws SAXException,
074                                                     IOException {
075            Document document = builder.parse(in);
076
077            return (FluentDocument) new InvocationHandler().enhance(document);
078        }
079
080        @Override
081        public boolean isNamespaceAware() {
082            return builder.isNamespaceAware();
083        }
084
085        @Override
086        public boolean isValidating() { return builder.isValidating(); }
087
088        @Override
089        public void setEntityResolver(EntityResolver resolver) {
090            builder.setEntityResolver(resolver);
091        }
092
093        @Override
094        public void setErrorHandler(ErrorHandler handler) {
095            builder.setErrorHandler(handler);
096        }
097
098        @Override
099        public DOMImplementation getDOMImplementation() {
100            return builder.getDOMImplementation();
101        }
102
103        @Override
104        public String toString() { return super.toString(); }
105    }
106}