+++ /dev/null
-/*******************************************************************************\r
- * ============LICENSE_START====================================================\r
- * * org.onap.aaf\r
- * * ===========================================================================\r
- * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
- * * ===========================================================================\r
- * * Licensed under the Apache License, Version 2.0 (the "License");\r
- * * you may not use this file except in compliance with the License.\r
- * * You may obtain a copy of the License at\r
- * * \r
- * * http://www.apache.org/licenses/LICENSE-2.0\r
- * * \r
- * * Unless required by applicable law or agreed to in writing, software\r
- * * distributed under the License is distributed on an "AS IS" BASIS,\r
- * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * * See the License for the specific language governing permissions and\r
- * * limitations under the License.\r
- * * ============LICENSE_END====================================================\r
- * *\r
- * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
- * *\r
- ******************************************************************************/\r
-package org.onap.aaf.cadi.wsse;\r
-\r
-import javax.xml.namespace.QName;\r
-import javax.xml.stream.XMLStreamException;\r
-import javax.xml.stream.events.XMLEvent;\r
-\r
-/**\r
- * Match Class allows you to build an automatic Tree of StAX (or StAX like) \r
- * Objects for frequent use.\r
- * \r
- * OBJECT is a type which you which to do some end Actions on, similar to a Visitor pattern, see Action\r
- * \r
- * Note: We have implemented with XReader and XEvent, rather than StAX for performance reasons.\r
- * \r
- * @see Action\r
- * @see Match\r
- * @see XEvent\r
- * @see XReader\r
- * \r
- *\r
- * @param <OUTPUT>\r
- */\r
-//@SuppressWarnings("restriction")\r
-public class Match<OUTPUT> {\r
- private QName qname;\r
- private Match<OUTPUT>[] next;\r
- private Match<OUTPUT> prev;\r
- private Action<OUTPUT> action = null;\r
- private boolean stopAfter;\r
- private boolean exclusive;\r
- \r
-\r
- @SafeVarargs\r
- public Match(String ns, String name, Match<OUTPUT> ... next) {\r
- this.qname = new QName(ns,name);\r
- this.next = next;\r
- stopAfter = exclusive = false;\r
- for(Match<OUTPUT> m : next) { // add the possible tags to look for\r
- if(!m.stopAfter)m.prev = this;\r
- }\r
- }\r
- \r
- public Match<OUTPUT> onMatch(OUTPUT output, XReader reader) throws XMLStreamException {\r
- while(reader.hasNext()) {\r
- XEvent event = reader.nextEvent();\r
- switch(event.getEventType()) {\r
- case XMLEvent.START_ELEMENT:\r
- QName e_qname = event.asStartElement().getName();\r
- //System.out.println("Start - " + e_qname);\r
- boolean match = false;\r
- for(Match<OUTPUT> m : next) {\r
- if(e_qname.equals(m.qname)) {\r
- match=true;\r
- if(m.onMatch(output, reader)==null) {\r
- return null; // short circuit Parsing\r
- }\r
- break;\r
- }\r
- }\r
- if(exclusive && !match) // When Tag MUST be present, i.e. the Root Tag, versus info we're not interested in\r
- return null;\r
- break;\r
- case XMLEvent.CHARACTERS:\r
- //System.out.println("Data - " +event.asCharacters().getData());\r
- if(action!=null) {\r
- if(!action.content(output,event.asCharacters().getData())) {\r
- return null;\r
- }\r
- }\r
- break;\r
- case XMLEvent.END_ELEMENT:\r
- //System.out.println("End - " + event.asEndElement().getName());\r
- if(event.asEndElement().getName().equals(qname)) {\r
- return prev;\r
- }\r
- break;\r
- case XMLEvent.END_DOCUMENT:\r
- return null; // Exit Chain\r
- }\r
- }\r
- return this;\r
- }\r
-\r
- /**\r
- * When this Matched Tag has completed, Stop parsing and end\r
- * @return\r
- */\r
- public Match<OUTPUT> stopAfter() {\r
- stopAfter = true;\r
- return this;\r
- }\r
- \r
- /**\r
- * Mark that this Object MUST be matched at this level or stop parsing and end\r
- * \r
- * @param action\r
- * @return\r
- */\r
- public Match<OUTPUT> exclusive() {\r
- exclusive = true;\r
- return this;\r
- }\r
-\r
- public Match<OUTPUT> set(Action<OUTPUT> action) {\r
- this.action = action;\r
- return this;\r
- }\r
-}\r