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