Gridarta Editor
ParseUtils.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.utils.xml;
21 
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.net.URISyntaxException;
25 import java.net.URL;
26 import nu.xom.Builder;
27 import nu.xom.DocType;
28 import nu.xom.Document;
29 import nu.xom.ParsingException;
30 import nu.xom.xinclude.XIncludeException;
31 import nu.xom.xinclude.XIncluder;
32 import org.jetbrains.annotations.NotNull;
33 import org.jetbrains.annotations.Nullable;
34 import org.xml.sax.ErrorHandler;
35 import org.xml.sax.SAXException;
36 import org.xml.sax.XMLReader;
37 import org.xml.sax.helpers.XMLReaderFactory;
38 
43 public class ParseUtils {
44 
48  private ParseUtils() {
49  }
50 
67  @NotNull
68  public static Document readXmlUrl(@NotNull final URL url, @NotNull final String rootElement, @NotNull final String systemId, @NotNull final String resourceName, @Nullable final ErrorHandler errorHandler) throws IOException, ParsingException, SAXException, URISyntaxException, XIncludeException {
69  try (InputStream inputStream = url.openStream()) {
70  return readXmlStream(inputStream, url.toURI().toASCIIString(), rootElement, systemId, resourceName, errorHandler);
71  }
72  }
73 
90  @NotNull
91  private static Document readXmlStream(@NotNull final InputStream inputStream, @NotNull final String baseURI, @NotNull final String rootElement, @NotNull final String systemId, @NotNull final String resourceName, @Nullable final ErrorHandler errorHandler) throws IOException, ParsingException, SAXException, XIncludeException {
92  final XMLReader xmlReader = XMLReaderFactory.createXMLReader();
93  xmlReader.setEntityResolver(new FixedDtdEntityResolver(systemId, resourceName));
94  if (errorHandler != null) {
95  xmlReader.setErrorHandler(errorHandler);
96  }
97  final Builder builder = new Builder(xmlReader, true);
98  final Document document;
99  try (InputStream in2 = new FixedDtdInputStream(inputStream, rootElement, systemId)) {
100  document = builder.build(in2, baseURI);
101  }
102  final DocType docType = document.getDocType();
103  if (docType == null) {
104  // using a FixedDtdInputStream ensures that a DOCTYPE declaration exists
105  throw new AssertionError("document contains no DOCTYPE declaration");
106  }
107  final String documentSystemId = docType.getSystemID();
108  if (documentSystemId == null) {
109  throw new ParsingException("DOCTYPE declaration has no system ID; expecting '" + systemId + "'");
110  }
111  if (!documentSystemId.equals(systemId)) {
112  throw new ParsingException("system ID in DOCTYPE declaration is '" + documentSystemId + "'; expecting '" + systemId + "'");
113  }
114  final String documentRootElement = docType.getRootElementName();
115  if (!documentRootElement.equals(rootElement)) {
116  throw new ParsingException("root element in DOCTYPE declaration is '" + documentRootElement + "'; expecting '" + rootElement + "'");
117  }
118  XIncluder.resolveInPlace(document, builder);
119  return document;
120  }
121 
122 }
net.sf.gridarta.utils.xml.FixedDtdInputStream
Injects a <!DOCTYPE...> declaration into an.
Definition: FixedDtdInputStream.java:34
net.sf.gridarta.utils.xml.ParseUtils.readXmlStream
static Document readXmlStream(@NotNull final InputStream inputStream, @NotNull final String baseURI, @NotNull final String rootElement, @NotNull final String systemId, @NotNull final String resourceName, @Nullable final ErrorHandler errorHandler)
Reads an XML file.
Definition: ParseUtils.java:91
net.sf.gridarta.utils.xml.ParseUtils.readXmlUrl
static Document readXmlUrl(@NotNull final URL url, @NotNull final String rootElement, @NotNull final String systemId, @NotNull final String resourceName, @Nullable final ErrorHandler errorHandler)
Reads an XML file from an URL.
Definition: ParseUtils.java:68
net.sf.gridarta.utils.xml.ParseUtils
Utility class for parsing XML files.
Definition: ParseUtils.java:43
net.sf.gridarta.utils.xml.FixedDtdEntityResolver
An.
Definition: FixedDtdEntityResolver.java:35
net.sf.gridarta.utils.xml.ParseUtils.ParseUtils
ParseUtils()
Private constructor to prevent instantiation.
Definition: ParseUtils.java:48