Gridarta Editor
FixedDtdEntityResolver.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 org.jetbrains.annotations.NotNull;
25 import org.jetbrains.annotations.Nullable;
26 import org.xml.sax.EntityResolver;
27 import org.xml.sax.InputSource;
28 import org.xml.sax.SAXException;
29 
35 public class FixedDtdEntityResolver implements EntityResolver {
36 
40  @NotNull
41  private final String systemId;
42 
46  @NotNull
47  private final String resourceName;
48 
55  public FixedDtdEntityResolver(@NotNull final String systemId, @NotNull final String resourceName) {
56  this.systemId = systemId;
57  this.resourceName = resourceName;
58  }
59 
60  @NotNull
61  @Override
62  public InputSource resolveEntity(@Nullable final String publicId, @NotNull final String systemId) throws SAXException, IOException {
63  if (!systemId.equals(this.systemId) && !systemId.endsWith("/" + this.systemId)) {
64  throw new SAXException("unsupported system ID '" + systemId + "'");
65  }
66  final InputStream inputStream = FixedDtdEntityResolver.class.getResourceAsStream(resourceName);
67  if (inputStream == null) {
68  throw new IOException("the resource '" + resourceName + "' does not exist");
69  }
70  return new InputSource(inputStream);
71  }
72 
73 }
net.sf.gridarta.utils.xml.FixedDtdEntityResolver.systemId
final String systemId
The expected system ID.
Definition: FixedDtdEntityResolver.java:41
net.sf.gridarta.utils.xml.FixedDtdEntityResolver.FixedDtdEntityResolver
FixedDtdEntityResolver(@NotNull final String systemId, @NotNull final String resourceName)
Creates a new instance.
Definition: FixedDtdEntityResolver.java:55
net.sf.gridarta.utils.xml.FixedDtdEntityResolver.resolveEntity
InputSource resolveEntity(@Nullable final String publicId, @NotNull final String systemId)
Definition: FixedDtdEntityResolver.java:62
net.sf.gridarta.utils.xml.FixedDtdEntityResolver
An.
Definition: FixedDtdEntityResolver.java:35
net.sf.gridarta.utils.xml.FixedDtdEntityResolver.resourceName
final String resourceName
The name of the resource to return for systemId.
Definition: FixedDtdEntityResolver.java:47