Skip to content

Package: XmlNamespaceFilter

XmlNamespaceFilter

nameinstructionbranchcomplexitylinemethod
XmlNamespaceFilter(String, boolean)
M: 15 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
endElement(String, String, String)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
startControlledPrefixMapping()
M: 15 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
startDocument()
M: 8 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
startElement(String, String, String, Attributes)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
startPrefixMapping(String, String)
M: 6 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2016, 2022 Eurotech and/or its affiliates and others
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Eurotech - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.commons.util.xml;
14:
15: import org.xml.sax.Attributes;
16: import org.xml.sax.SAXException;
17: import org.xml.sax.helpers.XMLFilterImpl;
18:
19: /**
20: * Xml namespace filter implementation.<br>
21: * This implementation adds only a namespace uri if enabled via addNamespace flag.
22: *
23: * @since 1.0
24: *
25: */
26: public class XmlNamespaceFilter extends XMLFilterImpl {
27:
28: /**
29: * Namespace uri to be used in the mapping operations
30: */
31: private String usedNamespaceUri;
32:
33: /**
34: * Flag to choose if add or not the namespace uri
35: */
36: private boolean addNamespace;
37:
38: // State variable
39: private boolean addedNamespace;
40:
41: /**
42: * Constructor
43: *
44: * @param namespaceUri
45: * @param addNamespace
46: */
47: public XmlNamespaceFilter(String namespaceUri,
48: boolean addNamespace) {
49: super();
50:
51:• if (addNamespace) {
52: this.usedNamespaceUri = namespaceUri;
53: } else {
54: this.usedNamespaceUri = "";
55: }
56: this.addNamespace = addNamespace;
57: }
58:
59: @Override
60: public void startDocument()
61: throws SAXException {
62: super.startDocument();
63:• if (addNamespace) {
64: startControlledPrefixMapping();
65: }
66: }
67:
68: @Override
69: public void startElement(String arg0, String arg1, String arg2,
70: Attributes arg3)
71: throws SAXException {
72: super.startElement(usedNamespaceUri, arg1, arg2, arg3);
73: }
74:
75: @Override
76: public void endElement(String arg0, String arg1, String arg2)
77: throws SAXException {
78: super.endElement(usedNamespaceUri, arg1, arg2);
79: }
80:
81: @Override
82: public void startPrefixMapping(String prefix, String url)
83: throws SAXException {
84:• if (addNamespace) {
85: startControlledPrefixMapping();
86: }
87: }
88:
89: private void startControlledPrefixMapping()
90: throws SAXException {
91:• if (addNamespace && !addedNamespace) {
92: // We should add namespace since it is set and has not yet been done.
93: super.startPrefixMapping("", usedNamespaceUri);
94:
95: // Make sure we dont do it twice
96: addedNamespace = true;
97: }
98: }
99: }