View Javadoc
1   /**
2    * Copyright (C) 2014-2018 Philip Helger (www.helger.com)
3    * philip[at]helger[dot]com
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * 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  package com.helger.schematron.pure.exchange;
18  
19  import java.io.File;
20  import java.io.OutputStream;
21  import java.io.Writer;
22  
23  import javax.annotation.Nonnull;
24  import javax.annotation.Nullable;
25  import javax.annotation.WillClose;
26  
27  import com.helger.commons.ValueEnforcer;
28  import com.helger.commons.annotation.OverrideOnDemand;
29  import com.helger.commons.state.ESuccess;
30  import com.helger.commons.string.ToStringGenerator;
31  import com.helger.schematron.pure.model.IPSElement;
32  import com.helger.xml.microdom.IMicroDocument;
33  import com.helger.xml.microdom.IMicroElement;
34  import com.helger.xml.microdom.IMicroNode;
35  import com.helger.xml.microdom.MicroDocument;
36  import com.helger.xml.microdom.serialize.MicroWriter;
37  
38  /**
39   * This class serializes the Schematron created within the domain object
40   *
41   * @author Philip Helger
42   */
43  public class PSWriter
44  {
45    private final IPSWriterSettings m_aWriterSettings;
46  
47    /**
48     * Constructor using the default {@link PSWriterSettings} instance.
49     */
50    public PSWriter ()
51    {
52      this (PSWriterSettings.DEFAULT_SETTINGS);
53    }
54  
55    /**
56     * Constructor.
57     *
58     * @param aWriterSettings
59     *        The writer settings to be used. May not be <code>null</code>.
60     */
61    public PSWriter (@Nonnull final IPSWriterSettings aWriterSettings)
62    {
63      m_aWriterSettings = ValueEnforcer.notNull (aWriterSettings, "WriterSettings");
64    }
65  
66    /**
67     * @return The writer settings specified in the constructor.
68     */
69    @Nonnull
70    public IPSWriterSettings getWriterSettings ()
71    {
72      return m_aWriterSettings;
73    }
74  
75    @Nonnull
76    @OverrideOnDemand
77    protected IMicroNode getAsDocument (@Nonnull final IMicroElement aElement)
78    {
79      final IMicroDocument aDoc = new MicroDocument ();
80      aDoc.appendChild (aElement);
81      return aDoc;
82    }
83  
84    /**
85     * Write the passed Schematron element to the passed file.
86     *
87     * @param aPSElement
88     *        The schematron element to write. May not be <code>null</code>.
89     * @param aFile
90     *        The file to write things to. May not be <code>null</code>.
91     * @return {@link ESuccess}.
92     */
93    @Nonnull
94    public ESuccess writeToFile (@Nonnull final IPSElement aPSElement, @Nonnull final File aFile)
95    {
96      ValueEnforcer.notNull (aPSElement, "PSElement");
97      final IMicroElement eXML = aPSElement.getAsMicroElement ();
98      return MicroWriter.writeToFile (getAsDocument (eXML), aFile, m_aWriterSettings.getXMLWriterSettings ());
99    }
100 
101   /**
102    * Write the passed Schematron element to the passed output stream.
103    *
104    * @param aPSElement
105    *        The schematron element to write. May not be <code>null</code>.
106    * @param aOS
107    *        The output stream to write things to. May not be <code>null</code>.
108    *        The stream is automatically closed.
109    * @return {@link ESuccess}.
110    */
111   @Nonnull
112   public ESuccess writeToStream (@Nonnull final IPSElement aPSElement, @Nonnull @WillClose final OutputStream aOS)
113   {
114     ValueEnforcer.notNull (aPSElement, "PSElement");
115     final IMicroElement eXML = aPSElement.getAsMicroElement ();
116     return MicroWriter.writeToStream (getAsDocument (eXML), aOS, m_aWriterSettings.getXMLWriterSettings ());
117   }
118 
119   /**
120    * Write the passed Schematron element to the passed writer.
121    *
122    * @param aPSElement
123    *        The schematron element to write. May not be <code>null</code>.
124    * @param aWriter
125    *        The writer to write things to. May not be <code>null</code>. The
126    *        writer is automatically closed.
127    * @return {@link ESuccess}.
128    */
129   @Nonnull
130   public ESuccess writeToWriter (@Nonnull final IPSElement aPSElement, @Nonnull @WillClose final Writer aWriter)
131   {
132     ValueEnforcer.notNull (aPSElement, "PSElement");
133     final IMicroElement eXML = aPSElement.getAsMicroElement ();
134     return MicroWriter.writeToWriter (getAsDocument (eXML), aWriter, m_aWriterSettings.getXMLWriterSettings ());
135   }
136 
137   /**
138    * Get the passed Schematron element as a String
139    *
140    * @param aPSElement
141    *        The schematron element to convert to a string. May not be
142    *        <code>null</code>.
143    * @return The passed element as a string or <code>null</code> if
144    *         serialization failed.
145    */
146   @Nullable
147   public String getXMLString (@Nonnull final IPSElement aPSElement)
148   {
149     ValueEnforcer.notNull (aPSElement, "PSElement");
150     final IMicroElement eXML = aPSElement.getAsMicroElement ();
151     return MicroWriter.getNodeAsString (getAsDocument (eXML), m_aWriterSettings.getXMLWriterSettings ());
152   }
153 
154   /**
155    * Get the passed Schematron element as a String
156    *
157    * @param aPSElement
158    *        The schematron element to convert to a string. May not be
159    *        <code>null</code>.
160    * @return The passed element as a string and never <code>null</code>.
161    * @throws IllegalStateException
162    *         if serialization failed
163    */
164   @Nullable
165   public String getXMLStringNotNull (@Nonnull final IPSElement aPSElement)
166   {
167     final String ret = getXMLString (aPSElement);
168     if (ret == null)
169       throw new IllegalStateException ("Failed to serialize the passed PS element: " + aPSElement);
170     return ret;
171   }
172 
173   @Override
174   public String toString ()
175   {
176     return new ToStringGenerator (this).append ("writerSettings", m_aWriterSettings).getToString ();
177   }
178 }