All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.day.cq.searchpromote.xml.StAXParser Maven / Gradle / Ivy

/*************************************************************************
 *
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2011 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and may be covered by U.S. and Foreign Patents,
 * patents in process, and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 **************************************************************************/
package com.day.cq.searchpromote.xml;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import java.io.StringReader;

import com.day.cq.searchpromote.xml.result.CustomerResult;

//TODO Use this
public class StAXParser {
    
    private static final String SELECTED_NODE = "selected";

	private XMLEventReader reader;
	
	public StAXParser(String resultXML) throws Exception{
		XMLInputFactory f = XMLInputFactory.newInstance();
		f.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
        f.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
		reader = f.createXMLEventReader( new StringReader( resultXML) );
	}
	
	public CustomerResult parse(  ) throws Exception {
		getNextEvent(reader);
		getNextEvent(reader);
		
		CustomerResult customerResult = new CustomerResult(  );
		customerResult.parse(reader);
		return customerResult;
	}

    public static boolean isSelected(StartElement startElement) {
        Attribute attribute = startElement.getAttributeByName(new QName(SELECTED_NODE));
        return attribute!= null && strToBool(attribute.getValue());
    }
    
    public static XMLEvent getNextEvent( XMLEventReader reader ) throws XMLStreamException {
        XMLEvent event = null;
        while( reader.hasNext() && 
                (event = reader.nextEvent()).isCharacters() );
        return event;
    }
    
    public static boolean strToBool( String value ) {
        if( value != null && value.equals("1") || value.toLowerCase().equals("true"))
            return true;
        return false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy