Debian Codegraph @

src/com/github/weisj/jsvg/parser/SVGLoader.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2021-2025 Jannis Weis
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7 * associated documentation files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
9 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or
13 * substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 */
22 package com.github.weisj.jsvg.parser;
23
24 import java.io.*;
25 import java.net.URI;
26 import java.net.URISyntaxException;
27 import java.net.URL;
28
29 import org.jetbrains.annotations.NotNull;
30 import org.jetbrains.annotations.Nullable;
31
32 import com.github.weisj.jsvg.SVGDocument;
33 import com.github.weisj.jsvg.logging.Logger;
34 import com.github.weisj.jsvg.logging.Logger.Level;
35 import com.github.weisj.jsvg.logging.impl.LogFactory;
36 import com.github.weisj.jsvg.parser.impl.*;
37
38 /**
39 * Class for loading svg files as an {@link SVGDocument}.
40 * Note that this class isn't guaranteed to be thread safe and hence shouldn't be used across multiple threads.
41 */
42 public final class SVGLoader {
43
44 static final Logger LOGGER = LogFactory.createLogger(SVGLoader.class);
45 private final StaxSVGLoader loader = new StaxSVGLoader();
46
47 public @Nullable SVGDocument load(@NotNull URL xmlBase) {
48 return load(xmlBase, LoaderContext.createDefault());
49 }
50
51 public @Nullable SVGDocument load(@NotNull URL xmlBase, @NotNull LoaderContext loaderContext) {
52 try {
53 URI uri = xmlBase.toURI();
54 return load(xmlBase.openStream(), uri, loaderContext);
55 } catch (URISyntaxException | IOException e) {
56 LOGGER.log(Level.WARNING, String.format("Could not read %s", xmlBase), e);
57 }
58 return null;
59 }
60
61 /**
62 * Load an SVG document from the given input stream.
63 *
64 * @param inputStream the input stream to read the SVG document from
65 * @param xmlBase The uri of the document. This is used to resolve external documents (if enabled).
66 * @param loaderContext The loader context to use for loading the document.
67 * @return The loaded SVG document or null if an error occurred.
68 */
69 public @Nullable SVGDocument load(@NotNull InputStream inputStream, @Nullable URI xmlBase,
70 @NotNull LoaderContext loaderContext) {
71 try (InputStream is = StreamUtil.createDocumentInputStream(inputStream)) {
72 return load(loader.createXMLInput(is), xmlBase, loaderContext);
73 } catch (IOException e) {
74 LOGGER.log(Level.WARNING, "Could not wrap input stream", e);
75 }
76 return null;
77 }
78
79 /**
80 * Load an SVG document from the given input stream.
81 *
82 * @param xmlInput the supplier for the xml input events
83 * @param xmlBase The uri of the document. This is used to resolve external documents (if enabled).
84 * @param loaderContext The loader context to use for loading the document.
85 * @return The loaded SVG document or null if an error occurred.
86 */
87 public @Nullable SVGDocument load(@NotNull XMLInput xmlInput, @Nullable URI xmlBase,
88 @NotNull LoaderContext loaderContext) {
89 try {
90 return loader.load(xmlInput, xmlBase, loaderContext);
91 } catch (Exception e) {
92 LOGGER.log(Level.WARNING, "Could not load SVG", e);
93 }
94 return null;
95 }
96 }