ba4bcbf6fc63b024f3d0e5686f4b1e12a2820ac0
[vfc/nfvo/catalog.git] /
1 /**\r
2  *     Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)\r
3  *\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
7  *\r
8  *             http://www.apache.org/licenses/LICENSE-2.0\r
9  *\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
15  */\r
16 package org.openo.orchestrator.nfv.catalog.model.externalservice.container;\r
17 \r
18 import org.xml.sax.Attributes;\r
19 import org.xml.sax.SAXException;\r
20 import org.xml.sax.helpers.XMLFilterImpl;\r
21 \r
22 /**\r
23  * NamespaceFilter\r
24  * @author 10189609\r
25  *\r
26  */\r
27 public class NamespaceFilter extends XMLFilterImpl {\r
28 \r
29     private String usedNamespaceUri;\r
30     private boolean addNamespace;\r
31 \r
32     //State variable\r
33     private boolean addedNamespace = false;\r
34 \r
35     public NamespaceFilter(String namespaceUri,\r
36             boolean addNamespace) {\r
37         super();\r
38 \r
39         if (addNamespace)\r
40             this.usedNamespaceUri = namespaceUri;\r
41         else \r
42             this.usedNamespaceUri = "";\r
43         this.addNamespace = addNamespace;\r
44     }\r
45 \r
46 \r
47 \r
48     @Override\r
49     public void startDocument() throws SAXException {\r
50         super.startDocument();\r
51         if (addNamespace) {\r
52             startControlledPrefixMapping();\r
53         }\r
54     }\r
55 \r
56 \r
57 \r
58     @Override\r
59     public void startElement(String arg0, String arg1, String arg2,\r
60             Attributes arg3) throws SAXException {\r
61 \r
62         super.startElement(this.usedNamespaceUri, arg1, arg2, arg3);\r
63     }\r
64 \r
65     @Override\r
66     public void endElement(String arg0, String arg1, String arg2)\r
67             throws SAXException {\r
68 \r
69         super.endElement(this.usedNamespaceUri, arg1, arg2);\r
70     }\r
71 \r
72     @Override\r
73     public void startPrefixMapping(String prefix, String url)\r
74             throws SAXException {\r
75 \r
76 \r
77         if (addNamespace) {\r
78             this.startControlledPrefixMapping();\r
79         } else {\r
80             //Remove the namespace, i.e. donĀ“t call startPrefixMapping for parent!\r
81         }\r
82 \r
83     }\r
84 \r
85     private void startControlledPrefixMapping() throws SAXException {\r
86 \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
90 \r
91             //Make sure we dont do it twice\r
92             this.addedNamespace = true;\r
93         }\r
94     }\r
95 \r
96 }\r