Update AAF Version 1.0.0
[aaf/cadi.git] / core / src / main / java / org / onap / aaf / cadi / wsse / Match.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package org.onap.aaf.cadi.wsse;\r
24 \r
25 import javax.xml.namespace.QName;\r
26 import javax.xml.stream.XMLStreamException;\r
27 import javax.xml.stream.events.XMLEvent;\r
28 \r
29 /**\r
30  * Match Class allows you to build an automatic Tree of StAX (or StAX like) \r
31  * Objects for frequent use.\r
32  * \r
33  * OBJECT is a type which you which to do some end Actions on, similar to a Visitor pattern, see Action\r
34  * \r
35  * Note: We have implemented with XReader and XEvent, rather than StAX for performance reasons.\r
36  * \r
37  * @see Action\r
38  * @see Match\r
39  * @see XEvent\r
40  * @see XReader\r
41  * \r
42  *\r
43  * @param <OUTPUT>\r
44  */\r
45 //@SuppressWarnings("restriction")\r
46 public class Match<OUTPUT> {\r
47         private QName qname;\r
48         private Match<OUTPUT>[] next;\r
49         private Match<OUTPUT> prev;\r
50         private Action<OUTPUT> action = null;\r
51         private boolean stopAfter;\r
52         private boolean exclusive;\r
53         \r
54 \r
55         @SafeVarargs\r
56         public Match(String ns, String name, Match<OUTPUT> ... next) {\r
57                 this.qname = new QName(ns,name);\r
58                 this.next = next;\r
59                 stopAfter = exclusive = false;\r
60                 for(Match<OUTPUT> m : next) { // add the possible tags to look for\r
61                         if(!m.stopAfter)m.prev = this;\r
62                 }\r
63         }\r
64         \r
65         public Match<OUTPUT> onMatch(OUTPUT output, XReader reader) throws XMLStreamException {\r
66                 while(reader.hasNext()) {\r
67                         XEvent event = reader.nextEvent();\r
68                         switch(event.getEventType()) {\r
69                                 case XMLEvent.START_ELEMENT:\r
70                                         QName e_qname = event.asStartElement().getName();\r
71                                         //System.out.println("Start - " + e_qname);\r
72                                         boolean match = false;\r
73                                         for(Match<OUTPUT> m : next) {\r
74                                                 if(e_qname.equals(m.qname)) {\r
75                                                         match=true;\r
76                                                         if(m.onMatch(output, reader)==null) {\r
77                                                                 return null; // short circuit Parsing\r
78                                                         }\r
79                                                         break;\r
80                                                 }\r
81                                         }\r
82                                         if(exclusive && !match) // When Tag MUST be present, i.e. the Root Tag, versus info we're not interested in\r
83                                                 return null;\r
84                                         break;\r
85                                 case XMLEvent.CHARACTERS:\r
86                                         //System.out.println("Data - " +event.asCharacters().getData());\r
87                                         if(action!=null) {\r
88                                                 if(!action.content(output,event.asCharacters().getData())) {\r
89                                                         return null;\r
90                                                 }\r
91                                         }\r
92                                         break;\r
93                                 case XMLEvent.END_ELEMENT:\r
94                                         //System.out.println("End - " + event.asEndElement().getName());\r
95                                         if(event.asEndElement().getName().equals(qname)) {\r
96                                                 return prev;\r
97                                         }\r
98                                         break;\r
99                                 case XMLEvent.END_DOCUMENT:\r
100                                         return null; // Exit Chain\r
101                         }\r
102                 }\r
103                 return this;\r
104         }\r
105 \r
106         /**\r
107          * When this Matched Tag has completed, Stop parsing and end\r
108          * @return\r
109          */\r
110         public Match<OUTPUT> stopAfter() {\r
111                 stopAfter = true;\r
112                 return this;\r
113         }\r
114         \r
115         /**\r
116          * Mark that this Object MUST be matched at this level or stop parsing and end\r
117          * \r
118          * @param action\r
119          * @return\r
120          */\r
121         public Match<OUTPUT> exclusive() {\r
122                 exclusive = true;\r
123                 return this;\r
124         }\r
125 \r
126         public Match<OUTPUT> set(Action<OUTPUT> action) {\r
127                 this.action = action;\r
128                 return this;\r
129         }\r
130 }\r