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