Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
   *
   * Licensed under the Apache License, Version 2.0 (the "License").
   * You may not use this file except in compliance with the License.
   * A copy of the License is located at
   *
   *  http://aws.amazon.com/apache2.0
   *
  * or in the "license" file accompanying this file. This file is distributed
  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  * express or implied. See the License for the specific language governing
  * permissions and limitations under the License.
  */
 package com.amazonaws.transform;
 
 import java.util.List;
 import java.util.Map;
 
Contains the unmarshalling state for the parsing of an XML response. The unmarshallers are stateless so that they can be reused, so this class holds the state while different unmarshallers work together to parse an XML response. It also tracks the current position and element depth of the document being parsed and provides utilties for accessing the next XML event from the parser, reading element text, handling attribute XML events, etc.
 
 public class StaxUnmarshallerContext {
 
     private XMLEvent currentEvent;
     private final XMLEventReader eventReader;
 
     public final Stack<Stringstack = new Stack<String>();
     private String stackString = "";
 
     private Map<StringStringmetadata = new HashMap<StringString>();
 
     private Iterator<?> attributeIterator;
     private final Map<StringStringheaders;

    
Constructs a new unmarshaller context using the specified source of XML events.

Parameters:
eventReader The source of XML events for this unmarshalling context.
 
     public StaxUnmarshallerContext(XMLEventReader eventReader) {
         this(eventReadernull);
     }

    
Constructs a new unmarshaller context using the specified source of XML events, and a set of response headers.

Parameters:
eventReader The source of XML events for this unmarshalling context.
headers The set of response headers associated with this unmarshaller context.
 
     public StaxUnmarshallerContext(XMLEventReader eventReaderMap<StringStringheaders) {
         this. = eventReader;
         this. = headers;
     }

    
Returns the value of the header with the specified name from the response, or null if not present.

Parameters:
header The name of the header to lookup.
Returns:
The value of the header with the specified name from the response, or null if not present.
 
     public String getHeader(String header) {
         if ( == nullreturn null;
 
         return .get(header);
     }

    
Returns the text contents of the current element being parsed.

Returns:
The text contents of the current element being parsed.
Throws:
javax.xml.stream.XMLStreamException
 
     public String readText() throws XMLStreamException {
        if (.isAttribute()) {
            Attribute attribute = (Attribute);
            return attribute.getValue();
        }
        StringBuilder sb = new StringBuilder();
        while (true) {
            XMLEvent event = .peek();
            if (event.getEventType() == .) {
                .nextEvent();
                sb.append(event.asCharacters().getData());
            } else if (event.getEventType() == .) {
                return sb.toString();
            } else {
                throw new RuntimeException("Encountered unexpected event: " + event.toString());
            }
        }
    }

    
Returns the element depth of the parser's current position in the XML document being parsed.

Returns:
The element depth of the parser's current position in the XML document being parsed.
    public int getCurrentDepth() {
        return .size();
    }

    
Tests the specified expression against the current position in the XML document being parsed.

Parameters:
expression The psuedo-xpath expression to test.
Returns:
True if the expression matches the current document position, otherwise false.
    public boolean testExpression(String expression) {
        if (expression.equals(".")) return true;
        return .endsWith(expression);
    }

    
Tests the specified expression against the current position in the XML document being parsed, and restricts the expression to matching at the specified stack depth.

Parameters:
expression The psuedo-xpath expression to test.
startingStackDepth The depth in the stack representing where the expression must start matching in order for this method to return true.
Returns:
True if the specified expression matches the current position in the XML document, starting from the specified depth.
    public boolean testExpression(String expressionint startingStackDepth) {
        if (expression.equals(".")) return true;
        int index = -1;
        while ((index = expression.indexOf("/"index + 1)) > -1) {
            // Don't consider attributes a new depth level
            if (expression.charAt(index + 1) != '@') {
                startingStackDepth++;
            }
        }
        return (startingStackDepth == getCurrentDepth()
                && .endsWith("/" + expression));
    }

    
Returns true if this unmarshaller context is at the very beginning of a source document (i.e. no data has been parsed from the document yet).

Returns:
true if this unmarshaller context is at the very beginning of a source document (i.e. no data has been parsed from the document yet).
    public boolean isStartOfDocument() throws XMLStreamException {
        return .peek().isStartDocument();
    }

    
Returns the next XML event for the document being parsed.

Returns:
The next XML event for the document being parsed.
Throws:
javax.xml.stream.XMLStreamException
    public XMLEvent nextEvent() throws XMLStreamException {
        if ( != null && .hasNext()) {
             = (XMLEvent).next();
        } else {
             = .nextEvent();
        }
        if (.isStartElement()) {
        }
        updateContext();
        if (.hasNext()) {
            XMLEvent nextEvent = .peek();
            if (nextEvent != null && nextEvent.isCharacters()) {
                for (MetadataExpression metadataExpression : ) {
                    if (testExpression(metadataExpression.expressionmetadataExpression.targetDepth)) {
                        .put(metadataExpression.keynextEvent.asCharacters().getData());
                    }
                }
            }
        }
        return ;
    }

    
Returns any metadata collected through metadata expressions while this context was reading the XML events from the XML document.

Returns:
A map of any metadata collected through metadata expressions while this context was reading the XML document.
    public Map<StringStringgetMetadata() {
        return ;
    }

    
Registers an expression, which if matched, will cause the data for the matching element to be stored in the metadata map under the specified key.

Parameters:
expression The expression an element must match in order for it's data to be pulled out and stored in the metadata map.
targetDepth The depth in the XML document where the expression match must start.
storageKey The key under which to store the matching element's data.
    public void registerMetadataExpression(String expressionint targetDepthString storageKey) {
        .add(new MetadataExpression(expressiontargetDepthstorageKey));
    }
    /*
     * Private Interface
     */

    
Simple container for the details of a metadata expression this unmarshaller context is looking for.
    private static class MetadataExpression {
        public String expression;
        public int targetDepth;
        public String key;
        public MetadataExpression(String expressionint targetDepthString key) {
            this. = expression;
            this. = targetDepth;
            this. = key;
        }
    }
    private void updateContext(XMLEvent event) {
        if (event == nullreturn;
        if (event.isEndElement()) {
            .pop();
             = "";
            for (String s : ) {
                 += "/" + s;
            }
        } else if (event.isStartElement()) {
            .push(event.asStartElement().getName().getLocalPart());
             += "/" + event.asStartElement().getName().getLocalPart();
        } else if (event.isAttribute()) {
            Attribute attribute = (Attribute)event;
             = "";
            for (String s : ) {
                 += "/" + s;
            }
             += "/@" + attribute.getName().getLocalPart();
        }
    }
New to GrepCode? Check out our FAQ X