2 * Copyright 2016 [ZTE] and others.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openo.commontosca.catalog.model.externalservice.container;
19 import org.xml.sax.Attributes;
20 import org.xml.sax.SAXException;
21 import org.xml.sax.helpers.XMLFilterImpl;
29 public class NamespaceFilter extends XMLFilterImpl {
31 private String usedNamespaceUri;
32 private boolean addNamespace;
35 private boolean addedNamespace = false;
39 * @param namespaceUri namspace uri
40 * @param addNamespace add namespace or not
42 public NamespaceFilter(String namespaceUri, boolean addNamespace) {
46 this.usedNamespaceUri = namespaceUri;
48 this.usedNamespaceUri = "";
50 this.addNamespace = addNamespace;
56 public void startDocument() throws SAXException {
57 super.startDocument();
59 startControlledPrefixMapping();
66 public void startElement(String arg0, String arg1, String arg2, Attributes arg3)
69 super.startElement(this.usedNamespaceUri, arg1, arg2, arg3);
73 public void endElement(String arg0, String arg1, String arg2) throws SAXException {
75 super.endElement(this.usedNamespaceUri, arg1, arg2);
79 public void startPrefixMapping(String prefix, String url) throws SAXException {
83 this.startControlledPrefixMapping();
85 // Remove the namespace, i.e. don“t call startPrefixMapping for parent!
90 private void startControlledPrefixMapping() throws SAXException {
92 if (this.addNamespace && !this.addedNamespace) {
93 // We should add namespace since it is set and has not yet been done.
94 super.startPrefixMapping("", this.usedNamespaceUri);
96 // Make sure we dont do it twice
97 this.addedNamespace = true;