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