2 * Copyright 2016 [ZTE] and others.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 package org.openo.commontosca.catalog.model.externalservice.container;
\r
19 import org.xml.sax.Attributes;
\r
20 import org.xml.sax.SAXException;
\r
21 import org.xml.sax.helpers.XMLFilterImpl;
\r
29 public class NamespaceFilter extends XMLFilterImpl {
\r
31 private String usedNamespaceUri;
\r
32 private boolean addNamespace;
\r
35 private boolean addedNamespace = false;
\r
39 * @param namespaceUri namspace uri
\r
40 * @param addNamespace add namespace or not
\r
42 public NamespaceFilter(String namespaceUri, boolean addNamespace) {
\r
46 this.usedNamespaceUri = namespaceUri;
\r
48 this.usedNamespaceUri = "";
\r
50 this.addNamespace = addNamespace;
\r
56 public void startDocument() throws SAXException {
\r
57 super.startDocument();
\r
59 startControlledPrefixMapping();
\r
66 public void startElement(String arg0, String arg1, String arg2, Attributes arg3)
\r
67 throws SAXException {
\r
69 super.startElement(this.usedNamespaceUri, arg1, arg2, arg3);
\r
73 public void endElement(String arg0, String arg1, String arg2) throws SAXException {
\r
75 super.endElement(this.usedNamespaceUri, arg1, arg2);
\r
79 public void startPrefixMapping(String prefix, String url) throws SAXException {
\r
83 this.startControlledPrefixMapping();
\r
85 // Remove the namespace, i.e. don“t call startPrefixMapping for parent!
\r
90 private void startControlledPrefixMapping() throws SAXException {
\r
92 if (this.addNamespace && !this.addedNamespace) {
\r
93 // We should add namespace since it is set and has not yet been done.
\r
94 super.startPrefixMapping("", this.usedNamespaceUri);
\r
96 // Make sure we dont do it twice
\r
97 this.addedNamespace = true;
\r