View Javadoc
1   /**
2    * Copyright (C) 2014-2016 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.svrl;
18  
19  import javax.annotation.Nonnull;
20  import javax.annotation.Nullable;
21  import javax.annotation.concurrent.Immutable;
22  import javax.xml.transform.Result;
23  
24  import org.oclc.purl.dsdl.svrl.SchematronOutputType;
25  import org.w3c.dom.Document;
26  
27  import com.helger.commons.annotation.PresentForCodeCoverage;
28  import com.helger.commons.state.ESuccess;
29  
30  /**
31   * This is the XML writer for Schematron SVRL documents. It reads
32   * {@link SchematronOutputType} elements and converts them to W3C nodes. The
33   * writing itself is done with JAXB.
34   *
35   * @author Philip Helger
36   */
37  @Immutable
38  public final class SVRLWriter
39  {
40    @PresentForCodeCoverage
41    private static final SVRLWriter s_aInstance = new SVRLWriter ();
42  
43    private SVRLWriter ()
44    {}
45  
46    /**
47     * Convert the passed schematron output element into an W3C Document node.
48     *
49     * @param aSchematronOutput
50     *        The schematron output to be converted. May not be <code>null</code>.
51     * @param aResult
52     *        The result object to write to.
53     * @return {@link ESuccess}
54     */
55    @Nonnull
56    public static ESuccess writeSVRL (@Nonnull final SchematronOutputType aSchematronOutput,
57                                      @Nonnull final Result aResult)
58    {
59      return new SVRLMarshaller ().write (aSchematronOutput, aResult);
60    }
61  
62    /**
63     * Convert the passed schematron output element into an W3C Document node.
64     *
65     * @param aSchematronOutput
66     *        The schematron output to be converted. May not be <code>null</code>.
67     * @return <code>null</code> if conversion failed.
68     */
69    @Nullable
70    public static Document createXML (@Nonnull final SchematronOutputType aSchematronOutput)
71    {
72      return new SVRLMarshaller ().getAsDocument (aSchematronOutput);
73    }
74  
75    /**
76     * Utility method to directly convert the passed SVRL domain object to an XML
77     * string.
78     *
79     * @param aSchematronOutput
80     *        The SVRL domain object to be converted. May not be null.
81     * @return <code>null</code> if the passed domain object could not be
82     *         converted because of validation errors.
83     */
84    @Nullable
85    public static String createXMLString (@Nonnull final SchematronOutputType aSchematronOutput)
86    {
87      return new SVRLMarshaller ().getAsString (aSchematronOutput);
88    }
89  }