1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\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
11 * * http://www.apache.org/licenses/LICENSE-2.0
\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
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package com.att.cadi.wsse;
\r
25 import javax.xml.namespace.QName;
\r
26 import javax.xml.stream.XMLStreamException;
\r
27 import javax.xml.stream.events.XMLEvent;
\r
30 * Match Class allows you to build an automatic Tree of StAX (or StAX like)
\r
31 * Objects for frequent use.
\r
33 * OBJECT is a type which you which to do some end Actions on, similar to a Visitor pattern, see Action
\r
35 * Note: We have implemented with XReader and XEvent, rather than StAX for performance reasons.
\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
56 public Match(String ns, String name, Match<OUTPUT> ... next) {
\r
57 this.qname = new QName(ns,name);
\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
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
76 if(m.onMatch(output, reader)==null) {
\r
77 return null; // short circuit Parsing
\r
82 if(exclusive && !match) // When Tag MUST be present, i.e. the Root Tag, versus info we're not interested in
\r
85 case XMLEvent.CHARACTERS:
\r
86 //System.out.println("Data - " +event.asCharacters().getData());
\r
88 if(!action.content(output,event.asCharacters().getData())) {
\r
93 case XMLEvent.END_ELEMENT:
\r
94 //System.out.println("End - " + event.asEndElement().getName());
\r
95 if(event.asEndElement().getName().equals(qname)) {
\r
99 case XMLEvent.END_DOCUMENT:
\r
100 return null; // Exit Chain
\r
107 * When this Matched Tag has completed, Stop parsing and end
\r
110 public Match<OUTPUT> stopAfter() {
\r
116 * Mark that this Object MUST be matched at this level or stop parsing and end
\r
121 public Match<OUTPUT> exclusive() {
\r
126 public Match<OUTPUT> set(Action<OUTPUT> action) {
\r
127 this.action = action;
\r