View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2012-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.xml;
6   
7   import java.io.File;
8   import java.io.FileNotFoundException;
9   import java.io.IOException;
10  import java.io.InputStream;
11  import java.net.URI;
12  import java.net.URL;
13  import java.nio.file.Path;
14  import java.util.Collection;
15  import javax.xml.transform.Source;
16  import lombok.EqualsAndHashCode;
17  import org.xml.sax.ErrorHandler;
18  import org.xml.sax.SAXParseException;
19  
20  /**
21   * Implementation of {@link XSD}.
22   *
23   * <p>Objects of this class are immutable and thread-safe.
24   *
25   * @since 0.5
26   * @deprecated This class is deprecated since 0.31.0. Instead, you can
27   *  use {@link StrictXML} with a schema provided in the constructor. Otherwise,
28   *  you can use {@link XMLDocument} and validate the XML against the schema
29   *  via the {@link XMLDocument#validate(XML)} method.
30   * @checkstyle AbbreviationAsWordInNameCheck (5 lines)
31   */
32  @Deprecated
33  @EqualsAndHashCode(of = "xsd")
34  public final class XSDDocument implements XSD {
35  
36      /**
37       * XSD document.
38       */
39      private final transient String xsd;
40  
41      /**
42       * Public ctor, from XSD as a source.
43       * @param src XSD document body
44       */
45      public XSDDocument(final XML src) {
46          this(src.toString());
47      }
48  
49      /**
50       * Public ctor, from XSD as a string.
51       * @param src XSD document body
52       */
53      public XSDDocument(final String src) {
54          this.xsd = src;
55      }
56  
57      /**
58       * Public ctor, from URL.
59       * @param url Location of document
60       * @throws IOException If fails to read
61       * @since 0.7.4
62       */
63      public XSDDocument(final URL url) throws IOException {
64          this(new TextResource(url).toString());
65      }
66  
67      /**
68       * Public ctor, from file.
69       * @param file Location of document
70       * @throws FileNotFoundException If fails to read
71       * @since 0.21
72       */
73      public XSDDocument(final Path file) throws IOException {
74          this(file.toFile());
75      }
76  
77      /**
78       * Public ctor, from file.
79       * @param file Location of document
80       * @throws FileNotFoundException If fails to read
81       * @since 0.21
82       */
83      public XSDDocument(final File file) throws IOException {
84          this(new TextResource(file).toString());
85      }
86  
87      /**
88       * Public ctor, from URI.
89       * @param uri Location of document
90       * @throws IOException If fails to read
91       * @since 0.15
92       */
93      public XSDDocument(final URI uri) throws IOException {
94          this(new TextResource(uri).toString());
95      }
96  
97      /**
98       * Public ctor, from XSD as an input stream.
99       * @param stream XSD input stream
100      */
101     public XSDDocument(final InputStream stream) {
102         this(new TextResource(stream).toString());
103     }
104 
105     /**
106      * Make an instance of XSD schema without I/O exceptions.
107      *
108      * <p>This factory method is useful when you need to create
109      * an instance of XSD schema as a static final variable. In this
110      * case you can't catch an exception but this method can help, for example:
111      *
112      * <pre> class Foo {
113      *   private static final XSD SCHEMA = XSDDocument.make(
114      *     Foo.class.getResourceAsStream("my-schema.xsd")
115      *   );
116      * }</pre>
117      *
118      * @param stream Input stream
119      * @return XSD schema
120      */
121     @SuppressWarnings("PMD.ProhibitPublicStaticMethods")
122     public static XSD make(final InputStream stream) {
123         return new XSDDocument(stream);
124     }
125 
126     /**
127      * Make an instance of XSD schema without I/O exceptions.
128      * @param url URL with content
129      * @return XSD schema
130      * @see #make(InputStream)
131      * @since 0.7.4
132      */
133     @SuppressWarnings("PMD.ProhibitPublicStaticMethods")
134     public static XSD make(final URL url) {
135         try {
136             return new XSDDocument(url);
137         } catch (final IOException ex) {
138             throw new IllegalStateException(ex);
139         }
140     }
141 
142     @Override
143     public String toString() {
144         return new XMLDocument(this.xsd).toString();
145     }
146 
147     @Override
148     public Collection<SAXParseException> validate(final Source xml) {
149         return new XMLDocument(xml).validate(new XMLDocument(this.xsd));
150     }
151 
152     /**
153      * Validation error handler.
154      *
155      * @since 0.1
156      */
157     static final class ValidationHandler implements ErrorHandler {
158         /**
159          * Errors.
160          */
161         private final transient Collection<SAXParseException> errors;
162 
163         /**
164          * Constructor.
165          * @param errs Collection of errors
166          */
167         ValidationHandler(final Collection<SAXParseException> errs) {
168             this.errors = errs;
169         }
170 
171         @Override
172         public void warning(final SAXParseException error) {
173             this.errors.add(error);
174         }
175 
176         @Override
177         public void error(final SAXParseException error) {
178             this.errors.add(error);
179         }
180 
181         @Override
182         public void fatalError(final SAXParseException error) {
183             this.errors.add(error);
184         }
185     }
186 
187 }