in ch-sxmp/src/main/java/com/cloudhopper/sxmp/SxmpParser.java [397:499]
public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
//
// get the tag stripped of any namespace
//
String tag = (StringUtil.isEmpty(uri)) ? qName : localName;
// debug
//logger.debug("startElement: tag=" + tag);
// logger.trace("startElement: uri=" + uri + ", localName=" + localName + ", qName=" + qName);
// always increment our depth
depth++;
//
// Depth: zero
// Element(s): operation
//
if (depth == 0) {
// make sure our encoding is valid
if (this.locator != null) {
this.encoding = this.locator.getEncoding();
}
if (version.equals(VERSION_1_1)) {
if (!this.encoding.toLowerCase().equals("utf-8")) {
throw new SxmpParsingException(SxmpErrorCode.UNSUPPORTED_TEXT_ENCODING, "Invalid encoding: "+encoding, null);
}
}
if (!tag.equals("operation")) {
throw new SxmpParsingException(SxmpErrorCode.INVALID_XML, "Root element must be an [operation]", null);
}
// must contain an attribute with the type
String opType = getRequiredAttributeValue("operation", attrs, "type");
// parse it via the enum
this.operationType = Operation.Type.parse(opType);
if (this.operationType == null) {
throw new SxmpParsingException(SxmpErrorCode.UNSUPPORTED_OPERATION, "Unsupported operation type [" + opType + "]", null);
}
//
// Depth: 1
// Element(s): account, submitRequest
//
} else if (depth == 1) {
if (tag.equals("account")) {
// should only occur once
if (elements.contains(tag)) {
throw new SxmpParsingException(SxmpErrorCode.MULTIPLE_ELEMENTS_NOT_SUPPORTED, "Multiple [account] elements are not supported", new PartialOperation(this.operationType));
}
elements.add(tag);
// should have two attributes, username and password
String username = getRequiredAttributeValue("account", attrs, "username");
String password = getRequiredAttributeValue("account", attrs, "password");
this.account = new Account(username, password);
} else if (tag.equals("application")) {
// should only occur once
if (elements.contains(tag)) {
throw new SxmpParsingException(SxmpErrorCode.MULTIPLE_ELEMENTS_NOT_SUPPORTED, "Multiple [application] elements are not supported", new PartialOperation(this.operationType));
}
} else if (tag.equals("error")) {
// this is a top level error -- indicates this should be
// the only element to return
ErrorCodeMessage ecm = parseErrorElement(tag, currentAttrs);
ErrorResponse errorResponse = new ErrorResponse(this.operationType, ecm.code, ecm.message);
this.operation = errorResponse;
} else if (tag.equals("submitRequest")) {
parseRequestElement(tag, new SubmitRequest(version), attrs);
} else if (tag.equals("deliverRequest")) {
parseRequestElement(tag, new DeliverRequest(), attrs);
} else if (tag.equals("deliveryReportRequest")) {
parseRequestElement(tag, new DeliveryReportRequest(), attrs);
} else if (tag.equals("submitResponse")) {
parseResponseElement(tag, new SubmitResponse());
} else if (tag.equals("deliverResponse")) {
parseResponseElement(tag, new DeliverResponse());
} else if (tag.equals("deliveryReportResponse")) {
parseResponseElement(tag, new DeliveryReportResponse());
} else {
throw new SxmpParsingException(SxmpErrorCode.UNSUPPORTED_ELEMENT, "Unsupported [" + tag + "] element found at depth [" + depth + "]", new PartialOperation(this.operationType));
}
//
// Depth: 2
// Element(s): operatorId, etc.
//
} else if (depth == 2) {
// do nothing here, always process in endElement callback
} else {
// NOTE: no other depths are supported in SXMP
throw new SxmpParsingException(SxmpErrorCode.UNSUPPORTED_ELEMENT, "Unsupported [" + tag + "] element found at depth [" + depth + "]", this.operation);
}
// starting element means reset our character buffer
charBuffer.setLength(0);
// save reference to attributes for processing in endElement
currentAttrs = attrs;
}