You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultConfigurationBuilder.java 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id: Accessibility.java 1343632 2012-05-29 09:48:03Z vhennebert $ */
  18. package org.apache.fop.configuration;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import javax.xml.parsers.DocumentBuilder;
  23. import javax.xml.parsers.ParserConfigurationException;
  24. import org.w3c.dom.DOMException;
  25. import org.w3c.dom.Document;
  26. import org.xml.sax.SAXException;
  27. import org.apache.commons.logging.Log;
  28. import org.apache.commons.logging.LogFactory;
  29. public class DefaultConfigurationBuilder {
  30. private static final Log LOG = LogFactory.getLog(DefaultConfigurationBuilder.class.getName());
  31. public DefaultConfiguration build(InputStream confStream) throws ConfigurationException {
  32. try {
  33. DocumentBuilder builder = DefaultConfiguration.DBF.newDocumentBuilder();
  34. Document document = builder.parse(confStream);
  35. return new DefaultConfiguration(document.getDocumentElement());
  36. } catch (DOMException e) {
  37. throw new ConfigurationException("xml parse error", e);
  38. } catch (ParserConfigurationException e) {
  39. throw new ConfigurationException("xml parse error", e);
  40. } catch (IOException e) {
  41. throw new ConfigurationException("xml parse error", e);
  42. } catch (SAXException e) {
  43. throw new ConfigurationException("xml parse error", e);
  44. } finally {
  45. try {
  46. confStream.close();
  47. } catch (IOException e) {
  48. throw new IllegalStateException(e);
  49. }
  50. }
  51. }
  52. public DefaultConfiguration buildFromFile(File file) throws ConfigurationException {
  53. try {
  54. DocumentBuilder builder = DefaultConfiguration.DBF.newDocumentBuilder();
  55. Document document = builder.parse(file);
  56. return new DefaultConfiguration(document.getDocumentElement());
  57. } catch (DOMException e) {
  58. throw new ConfigurationException("xml parse error", e);
  59. } catch (ParserConfigurationException e) {
  60. throw new ConfigurationException("xml parse error", e);
  61. } catch (IOException e) {
  62. throw new ConfigurationException("xml parse error", e);
  63. } catch (SAXException e) {
  64. throw new ConfigurationException("xml parse error", e);
  65. }
  66. }
  67. }