View Javadoc
1   /*
2    * Copyright 2019-2022 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.bremersee.comparator.model;
18  
19  import java.io.StringWriter;
20  import java.util.HashMap;
21  import java.util.Map;
22  import java.util.Set;
23  import jakarta.xml.bind.SchemaOutputResolver;
24  import javax.xml.transform.Result;
25  import javax.xml.transform.stream.StreamResult;
26  
27  /**
28   * The buffer schema output resolver.
29   *
30   * @author Christian Bremer
31   */
32  @SuppressWarnings({"WeakerAccess", "unused"})
33  public class BufferSchemaOutputResolver extends SchemaOutputResolver {
34  
35    /**
36     * The buffers.
37     */
38    protected Map<String, StreamResult> buffers = new HashMap<>();
39  
40    @Override
41    public Result createOutput(String namespaceUri, String suggestedFileName) {
42      StringWriter out = new StringWriter();
43      StreamResult res = new StreamResult(out);
44      res.setSystemId(suggestedFileName);
45      buffers.put(namespaceUri, res);
46      return res;
47    }
48  
49    /**
50     * Gets namespaces.
51     *
52     * @return the namespaces
53     */
54    public Set<String> getNamespaces() {
55      return buffers.keySet();
56    }
57  
58    /**
59     * Gets schema.
60     *
61     * @param namespaceUri the namespace uri
62     * @return the schema
63     */
64    public String getSchema(String namespaceUri) {
65      return buffers.get(namespaceUri).getWriter().toString();
66    }
67  
68    /**
69     * Gets system id.
70     *
71     * @param namespaceUri the namespace uri
72     * @return the system id
73     */
74    public String getSystemId(String namespaceUri) {
75      return buffers.get(namespaceUri).getSystemId();
76    }
77  
78    @Override
79    public String toString() {
80      StringBuilder sb = new StringBuilder();
81      for (String s : buffers.keySet()) {
82        sb.append("***** Begin schema ").append(s).append(", system-id=").append(getSystemId(s))
83            .append(" *****");
84        sb.append(System.getProperty("line.separator"));
85        sb.append(getSchema(s));
86        sb.append("***** End schema ").append(s).append(" *****");
87        sb.append(System.getProperty("line.separator"));
88        sb.append(System.getProperty("line.separator"));
89      }
90      return sb.toString();
91    }
92  
93    /**
94     * Clear.
95     */
96    public void clear() {
97      buffers.clear();
98    }
99  
100 }