001package ball.xml; 002/*- 003 * ########################################################################## 004 * Utilities 005 * $Id: FluentDocumentBuilderFactory.java 5855 2020-04-27 20:01:17Z ball $ 006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-util/trunk/src/main/java/ball/xml/FluentDocumentBuilderFactory.java $ 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; 026import lombok.ToString; 027 028import static java.util.Objects.requireNonNull; 029 030/** 031 * {@link FluentDocument.Builder} {@link DocumentBuilderFactory}. 032 * 033 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball} 034 * @version $Revision: 5855 $ 035 */ 036@ToString 037public class FluentDocumentBuilderFactory extends DocumentBuilderFactory { 038 private final DocumentBuilderFactory factory; 039 040 /** 041 * See {@link DocumentBuilderFactory#newInstance()}. 042 * 043 * @return {@link FluentDocumentBuilderFactory} 044 */ 045 public static FluentDocumentBuilderFactory newInstance() throws FactoryConfigurationError { 046 return new FluentDocumentBuilderFactory(); 047 } 048 049 /** 050 * See {@link DocumentBuilderFactory#newInstance(String,ClassLoader)}. 051 * 052 * @param name The {@link Class} name. 053 * @param loader The {@link ClassLoader}. 054 * 055 * @return {@link FluentDocumentBuilderFactory} 056 */ 057 public static FluentDocumentBuilderFactory newInstance(String name, 058 ClassLoader loader) throws FactoryConfigurationError { 059 DocumentBuilderFactory factory = 060 DocumentBuilderFactory.newInstance(name, loader); 061 062 if (! (factory instanceof FluentDocumentBuilderFactory)) { 063 factory = new FluentDocumentBuilderFactory(factory); 064 } 065 066 return (FluentDocumentBuilderFactory) factory; 067 } 068 069 /** 070 * Sole public constructor. 071 */ 072 public FluentDocumentBuilderFactory() throws FactoryConfigurationError { 073 this(DocumentBuilderFactory.newInstance()); 074 } 075 076 private FluentDocumentBuilderFactory(DocumentBuilderFactory factory) { 077 super(); 078 079 this.factory = requireNonNull(factory); 080 } 081 082 @Override 083 public void setAttribute(String name, 084 Object value) throws IllegalArgumentException { 085 factory.setAttribute(name, value); 086 } 087 088 @Override 089 public Object getAttribute(String name) throws IllegalArgumentException { 090 return factory.getAttribute(name); 091 } 092 093 @Override 094 public FluentDocument.Builder newDocumentBuilder() throws ParserConfigurationException { 095 return new FluentDocument.Builder(factory.newDocumentBuilder()); 096 } 097 098 @Override 099 public void setFeature(String name, 100 boolean value) throws ParserConfigurationException { 101 factory.setFeature(name, value); 102 } 103 104 @Override 105 public boolean getFeature(String name) throws ParserConfigurationException { 106 return factory.getFeature(name); 107 } 108}