package com.microsoft.azure.storage.file;
import com.microsoft.azure.storage.Constants;
import com.microsoft.azure.storage.core.ListResponse;
import com.microsoft.azure.storage.core.SR;
import com.microsoft.azure.storage.core.Utility;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.Stack;
public class HandleListHandler extends DefaultHandler {
private final Stack<String> elementStack = new Stack<String>();
private StringBuilder bld = new StringBuilder();
private final ListResponse<FileHandle> response = new ListResponse<FileHandle>();
private FileHandle handle;
private HandleListHandler() {
}
protected static ListResponse<FileHandle> getHandleList(final InputStream stream)
throws ParserConfigurationException, SAXException, IOException {
SAXParser saxParser = Utility.getSAXParser();
HandleListHandler handler = new HandleListHandler();
saxParser.parse(stream, handler);
return handler.response;
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
this.elementStack.push(localName);
if (FileConstants.HANDLE_ELEMENT.equals(localName)) {
this.handle = new FileHandle();
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
String currentNode = this.elementStack.pop();
if (!localName.equals(currentNode)) {
throw new SAXException(SR.INVALID_RESPONSE_RECEIVED);
}
String parentNode = null;
if (!this.elementStack.isEmpty()) {
parentNode = this.elementStack.peek();
}
String value = this.bld.toString();
if (value.isEmpty()) {
value = null;
}
if (FileConstants.HANDLE_ELEMENT.equals(currentNode)) {
this.response.getResults().add(this.handle);
}
else if (ListResponse.ENUMERATION_RESULTS.equals(parentNode)) {
if (Constants.NEXT_MARKER_ELEMENT.equals(currentNode)) {
this.response.setNextMarker(value);
}
}
else if (FileConstants.HANDLE_ELEMENT.equals(parentNode)) {
try {
this.setHandleProperties(currentNode, value);
}
catch (ParseException e) {
throw new SAXException(e);
}
}
this.bld = new StringBuilder();
}
@Override
public void characters(char ch[], int start, int length) throws SAXException {
this.bld.append(ch, start, length);
}
private void setHandleProperties(String currentNode, String value) throws ParseException {
if (FileConstants.HANDLE_ID_ELEMENT.equals(currentNode)) {
this.handle.setHandleID(value);
}
else if (FileConstants.PATH_ELEMENT.equals(currentNode)) {
this.handle.setPath(value);
}
else if (FileConstants.FILE_ID_ELEMENT.equals(currentNode)) {
this.handle.setFileID(value);
}
else if (FileConstants.PARENT_ID_ELEMENT.equals(currentNode)) {
this.handle.setParentFileID(value);
}
else if (FileConstants.SESSION_ID_ELEMENT.equals(currentNode)) {
this.handle.setSessionID(value);
}
else if (FileConstants.CLIENT_IP_ELEMENT.equals(currentNode)) {
this.handle.setClientIP(value);
}
else if (FileConstants.OPEN_TIME_ELEMENT.equals(currentNode)) {
this.handle.setOpenTime(value);
}
else if (FileConstants.LAST_RECONNECT_TIME_ELEMENT.equals(currentNode)) {
this.handle.setLastReconnectTime(value);
}
}
}