View Javadoc
1   /**
2    * Copyright 2014 Internet2
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  /*
17   * Copyright (C) 2006 Joe Walnes.
18   * Copyright (C) 2006, 2007 XStream Committers.
19   * All rights reserved.
20   *
21   * The software in this package is published under the terms of the BSD
22   * style license a copy of which has been included with this distribution in
23   * the LICENSE.txt file.
24   * 
25   * Created on 12. April 2006 by Joerg Schaible
26   */
27  package edu.internet2.middleware.grouperClientExt.com.thoughtworks.xstream.io.xml;
28  
29  import java.io.IOException;
30  import java.io.InputStream;
31  import java.io.OutputStream;
32  import java.io.OutputStreamWriter;
33  import java.io.Reader;
34  import java.io.Writer;
35  
36  import nu.xom.Builder;
37  import nu.xom.Document;
38  import nu.xom.ParsingException;
39  import nu.xom.ValidityException;
40  import edu.internet2.middleware.grouperClientExt.com.thoughtworks.xstream.io.HierarchicalStreamReader;
41  import edu.internet2.middleware.grouperClientExt.com.thoughtworks.xstream.io.HierarchicalStreamWriter;
42  import edu.internet2.middleware.grouperClientExt.com.thoughtworks.xstream.io.StreamException;
43  
44  public class XomDriver extends AbstractXmlDriver {
45  
46      private final Builder builder;
47  
48      public XomDriver() {
49          this(new Builder());
50      }
51  
52      public XomDriver(Builder builder) {
53          this(builder, new XmlFriendlyReplacer());
54      }
55  
56      /**
57       * @since 1.2
58       */
59      public XomDriver(XmlFriendlyReplacer replacer) {
60          this(new Builder(), replacer);        
61      }
62      
63      /**
64       * @since 1.2
65       */
66      public XomDriver(Builder builder, XmlFriendlyReplacer replacer) {
67          super(replacer);    
68          this.builder = builder;
69      }
70  
71      protected Builder getBuilder() {
72          return this.builder;
73      }
74  
75      public HierarchicalStreamReader createReader(Reader text) {
76          try {
77              Document document = builder.build(text);
78              return new XomReader(document, xmlFriendlyReplacer());
79          } catch (ValidityException e) {
80              throw new StreamException(e);
81          } catch (ParsingException e) {
82              throw new StreamException(e);
83          } catch (IOException e) {
84              throw new StreamException(e);
85          }
86      }
87  
88      public HierarchicalStreamReader createReader(InputStream in) {
89          try {
90              Document document = builder.build(in);
91              return new XomReader(document, xmlFriendlyReplacer());
92          } catch (ValidityException e) {
93              throw new StreamException(e);
94          } catch (ParsingException e) {
95              throw new StreamException(e);
96          } catch (IOException e) {
97              throw new StreamException(e);
98          }
99      }
100 
101     public HierarchicalStreamWriter createWriter(final Writer out) {
102         return new PrettyPrintWriter(out, xmlFriendlyReplacer());
103     }
104 
105     public HierarchicalStreamWriter createWriter(final OutputStream out) {
106         return new PrettyPrintWriter(new OutputStreamWriter(out), xmlFriendlyReplacer());
107     }
108 }