2 * Copyright 2016 ZTE Corporation.
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.
16 package org.openo.commontosca.catalog.model.externalservice.container;
18 import org.xml.sax.Attributes;
19 import org.xml.sax.SAXException;
20 import org.xml.sax.helpers.XMLFilterImpl;
28 public class NamespaceFilter extends XMLFilterImpl {
30 private String usedNamespaceUri;
31 private boolean addNamespace;
34 private boolean addedNamespace = false;
38 * @param namespaceUri namspace uri
39 * @param addNamespace add namespace or not
41 public NamespaceFilter(String namespaceUri, boolean addNamespace) {
45 this.usedNamespaceUri = namespaceUri;
47 this.usedNamespaceUri = "";
49 this.addNamespace = addNamespace;
55 public void startDocument() throws SAXException {
56 super.startDocument();
58 startControlledPrefixMapping();
65 public void startElement(String arg0, String arg1, String arg2, Attributes arg3)
68 super.startElement(this.usedNamespaceUri, arg1, arg2, arg3);
72 public void endElement(String arg0, String arg1, String arg2) throws SAXException {
74 super.endElement(this.usedNamespaceUri, arg1, arg2);
78 public void startPrefixMapping(String prefix, String url) throws SAXException {
82 this.startControlledPrefixMapping();
84 // Remove the namespace, i.e. don“t call startPrefixMapping for parent!
89 private void startControlledPrefixMapping() throws SAXException {
91 if (this.addNamespace && !this.addedNamespace) {
92 // We should add namespace since it is set and has not yet been done.
93 super.startPrefixMapping("", this.usedNamespaceUri);
95 // Make sure we dont do it twice
96 this.addedNamespace = true;