2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===========================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END====================================================
22 package org.onap.aaf.cadi.wsse;
24 import javax.xml.namespace.QName;
25 import javax.xml.stream.XMLStreamException;
26 import javax.xml.stream.events.XMLEvent;
29 * Match Class allows you to build an automatic Tree of StAX (or StAX like)
30 * Objects for frequent use.
32 * OBJECT is a type which you which to do some end Actions on, similar to a Visitor pattern, see Action
34 * Note: We have implemented with XReader and XEvent, rather than StAX for performance reasons.
45 //@SuppressWarnings("restriction")
46 public class Match<OUTPUT> {
48 private Match<OUTPUT>[] next;
49 private Match<OUTPUT> prev;
50 private Action<OUTPUT> action = null;
51 private boolean stopAfter;
52 private boolean exclusive;
56 public Match(String ns, String name, Match<OUTPUT> ... next) {
57 this.qname = new QName(ns,name);
59 stopAfter = exclusive = false;
60 for(Match<OUTPUT> m : next) { // add the possible tags to look for
61 if(!m.stopAfter)m.prev = this;
65 public Match<OUTPUT> onMatch(OUTPUT output, XReader reader) throws XMLStreamException {
66 while(reader.hasNext()) {
67 XEvent event = reader.nextEvent();
68 switch(event.getEventType()) {
69 case XMLEvent.START_ELEMENT:
70 QName e_qname = event.asStartElement().getName();
71 //System.out.println("Start - " + e_qname);
72 boolean match = false;
73 for(Match<OUTPUT> m : next) {
74 if(e_qname.equals(m.qname)) {
76 if(m.onMatch(output, reader)==null) {
77 return null; // short circuit Parsing
82 if(exclusive && !match) // When Tag MUST be present, i.e. the Root Tag, versus info we're not interested in
85 case XMLEvent.CHARACTERS:
86 //System.out.println("Data - " +event.asCharacters().getData());
88 if(!action.content(output,event.asCharacters().getData())) {
93 case XMLEvent.END_ELEMENT:
94 //System.out.println("End - " + event.asEndElement().getName());
95 if(event.asEndElement().getName().equals(qname)) {
99 case XMLEvent.END_DOCUMENT:
100 return null; // Exit Chain
107 * When this Matched Tag has completed, Stop parsing and end
110 public Match<OUTPUT> stopAfter() {
116 * Mark that this Object MUST be matched at this level or stop parsing and end
121 public Match<OUTPUT> exclusive() {
126 public Match<OUTPUT> set(Action<OUTPUT> action) {
127 this.action = action;