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
16 package org.openo.commontosca.catalog.model.externalservice.container;
\r
18 import org.xml.sax.Attributes;
\r
19 import org.xml.sax.SAXException;
\r
20 import org.xml.sax.helpers.XMLFilterImpl;
\r
27 public class NamespaceFilter extends XMLFilterImpl {
\r
29 private String usedNamespaceUri;
\r
30 private boolean addNamespace;
\r
33 private boolean addedNamespace = false;
\r
35 public NamespaceFilter(String namespaceUri,
\r
36 boolean addNamespace) {
\r
40 this.usedNamespaceUri = namespaceUri;
\r
42 this.usedNamespaceUri = "";
\r
43 this.addNamespace = addNamespace;
\r
49 public void startDocument() throws SAXException {
\r
50 super.startDocument();
\r
52 startControlledPrefixMapping();
\r
59 public void startElement(String arg0, String arg1, String arg2,
\r
60 Attributes arg3) throws SAXException {
\r
62 super.startElement(this.usedNamespaceUri, arg1, arg2, arg3);
\r
66 public void endElement(String arg0, String arg1, String arg2)
\r
67 throws SAXException {
\r
69 super.endElement(this.usedNamespaceUri, arg1, arg2);
\r
73 public void startPrefixMapping(String prefix, String url)
\r
74 throws SAXException {
\r
78 this.startControlledPrefixMapping();
\r
80 //Remove the namespace, i.e. don“t call startPrefixMapping for parent!
\r
85 private void startControlledPrefixMapping() throws SAXException {
\r
87 if (this.addNamespace && !this.addedNamespace) {
\r
88 //We should add namespace since it is set and has not yet been done.
\r
89 super.startPrefixMapping("", this.usedNamespaceUri);
\r
91 //Make sure we dont do it twice
\r
92 this.addedNamespace = true;
\r