View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.wss4j.stax.test.utils;
20  
21  import org.apache.xml.security.stax.ext.stax.XMLSecEvent;
22  import org.apache.xml.security.stax.ext.stax.XMLSecEventFactory;
23  import org.apache.xml.security.stax.ext.stax.XMLSecStartElement;
24  
25  import javax.xml.stream.XMLStreamConstants;
26  import javax.xml.stream.XMLStreamException;
27  import javax.xml.stream.XMLStreamReader;
28  import javax.xml.stream.events.XMLEvent;
29  import javax.xml.stream.util.XMLEventAllocator;
30  import javax.xml.stream.util.XMLEventConsumer;
31  
32  /**
33   * <p/>
34   * An extended XMLEventAllocator to collect namespaces and C14N relevant attributes
35   */
36  public class XMLSecEventAllocator implements XMLEventAllocator {
37  
38      private XMLSecStartElement parentXmlSecStartElement;
39  
40      @Override
41      public XMLEventAllocator newInstance() {
42          try {
43              return new XMLSecEventAllocator();
44          } catch (Exception e) {
45              throw new RuntimeException(e);
46          }
47      }
48  
49      @Override
50      public XMLEvent allocate(XMLStreamReader xmlStreamReader) throws XMLStreamException {
51          XMLSecEvent xmlSecEvent = XMLSecEventFactory.allocate(xmlStreamReader, parentXmlSecStartElement);
52          switch (xmlSecEvent.getEventType()) {
53              case XMLStreamConstants.START_ELEMENT:
54                  parentXmlSecStartElement = (XMLSecStartElement) xmlSecEvent;
55                  break;
56              case XMLStreamConstants.END_ELEMENT:
57                  if (parentXmlSecStartElement != null) {
58                      parentXmlSecStartElement = parentXmlSecStartElement.getParentXMLSecStartElement();
59                  }
60                  break;
61          }
62          return xmlSecEvent;
63      }
64  
65      @Override
66      public void allocate(XMLStreamReader reader, XMLEventConsumer consumer) throws XMLStreamException {
67          //xmlEventAllocator.allocate(reader, consumer);
68      }
69  }