package at.gv.egovernment.moa.spss.api.xmlbind;
import java.util.ArrayList;
import java.util.List;
import org.w3c.dom.Element;
import org.w3c.dom.traversal.NodeIterator;
import at.gv.egovernment.moa.util.BoolUtils;
import at.gv.egovernment.moa.util.Constants;
import at.gv.egovernment.moa.util.DOMUtils;
import at.gv.egovernment.moa.util.XPathUtils;
import at.gv.egovernment.moa.spss.MOAApplicationException;
import at.gv.egovernment.moa.spss.api.SPSSFactory;
import at.gv.egovernment.moa.spss.api.common.Content;
import at.gv.egovernment.moa.spss.api.xmlsign.CreateSignatureEnvironmentProfile;
import at.gv.egovernment.moa.spss.api.xmlsign.CreateSignatureInfo;
import at.gv.egovernment.moa.spss.api.xmlsign.CreateTransformsInfoProfile;
import at.gv.egovernment.moa.spss.api.xmlsign.CreateXMLSignatureRequest;
import at.gv.egovernment.moa.spss.api.xmlsign.DataObjectInfo;
import at.gv.egovernment.moa.spss.api.xmlsign.SingleSignatureInfo;
/**
* A parser to parse CreateXMLSignatureRequest
DOM trees into
* CreateXMLSignatureRequest
API objects.
*
* @author Patrick Peck
* @version $Id$
*/
public class CreateXMLSignatureRequestParser {
//
// XPath expresssions to select elements in the CreateXMLSignatureRequest
//
private static final String MOA = Constants.MOA_PREFIX + ":";
private static final String KEY_IDENTIFIER_XPATH =
"/" + MOA + "CreateXMLSignatureRequest/" + MOA + "KeyIdentifier";
private static final String SINGLE_SIGNATURE_INFO_XPATH =
"/" + MOA + "CreateXMLSignatureRequest/" + MOA + "SingleSignatureInfo";
private static final String DATA_OBJECT_INFO_XPATH = MOA + "DataObjectInfo";
private static final String DATA_OBJECT_XPATH = MOA + "DataObject";
private static final String CREATE_SIGNATURE_INFO_XPATH =
MOA + "CreateSignatureInfo";
private static final String CREATE_TRANSFORMS_INFO_PROFILE_XPATH =
(MOA + "CreateTransformsInfoProfile | ")
+ (MOA + "CreateTransformsInfoProfileID");
private static final String CREATE_SIGNATURE_ENVIRONMENT_XPATH =
MOA + "CreateSignatureEnvironment";
private static final String CREATE_SIGNATURE_ENVIRONMENT_PROFILE_XPATH =
(MOA + "CreateSignatureEnvironmentProfile | ")
+ (MOA + "CreateSignatureEnvironmentProfileID");
private static final String SL_CONFORM_ATTR_NAME = "SecurityLayerConformity";
/** The factory to create API objects. */
private SPSSFactory factory;
/**
* Create a new CreateXMLSignatureRequestParser
.
*/
public CreateXMLSignatureRequestParser() {
this.factory = SPSSFactory.getInstance();
}
/**
* Parse a CreateXMLSignatureRequest
DOM element, as defined
* by the MOA schema.
*
* @param requestElem The CreateXMLSignatureRequest
to parse. The
* request must have been successfully parsed against the schema for this
* method to succeed.
* @return A CreateXMLSignatureRequest
API object containing
* the data from the DOM element.
* @throws MOAApplicationException An error occurred parsing the request.
*/
public CreateXMLSignatureRequest parse(Element requestElem)
throws MOAApplicationException {
List singleSignatureInfos = parseSingleSignatureInfos(requestElem);
String keyIdentifier =
XPathUtils.getElementValue(requestElem, KEY_IDENTIFIER_XPATH, null);
return factory.createCreateXMLSignatureRequest(
keyIdentifier,
singleSignatureInfos);
}
/**
* Parse all SingleSignatureInfo
elements of the
* CreateXMLSignatureRequest
.
*
* @param requestElem The CreateXMLSignatureRequest
to parse.
* @return A List
of SingleSignatureInfo
API
* objects.
* @throws MOAApplicationException An error occurred parsing on of the
* SingleSignatureInfo
elements.
*/
private List parseSingleSignatureInfos(Element requestElem)
throws MOAApplicationException {
List singleSignatureInfos = new ArrayList();
NodeIterator sigInfoElems =
XPathUtils.selectNodeIterator(requestElem, SINGLE_SIGNATURE_INFO_XPATH);
Element sigInfoElem;
while ((sigInfoElem = (Element) sigInfoElems.nextNode()) != null) {
singleSignatureInfos.add(parseSingleSignatureInfo(sigInfoElem));
}
return singleSignatureInfos;
}
/**
* Parse a SingleSignatureInfo
DOM element.
*
* @param sigInfoElem The SingleSignatureInfo
DOM element to
* parse.
* @return A SingleSignatureInfo
API object containing the
* information of sigInfoElem
.
* @throws MOAApplicationException An error occurred parsing the
* SingleSignatureInfo
.
*/
private SingleSignatureInfo parseSingleSignatureInfo(Element sigInfoElem)
throws MOAApplicationException {
List dataObjectInfos = parseDataObjectInfos(sigInfoElem);
CreateSignatureInfo createSignatureInfo =
parseCreateSignatureInfo(sigInfoElem);
boolean securityLayerConform;
if (sigInfoElem.hasAttribute(SL_CONFORM_ATTR_NAME)) {
securityLayerConform =
BoolUtils.valueOf(sigInfoElem.getAttribute(SL_CONFORM_ATTR_NAME));
} else {
securityLayerConform = true;
}
return factory.createSingleSignatureInfo(
dataObjectInfos,
createSignatureInfo,
securityLayerConform);
}
/**
* Parse the DataObjectInfo
DOM elements contained in the given
* SingleSignatureInfo
DOM element.
*
* @param sigInfoElem The SingleSignatureInfo
DOM element
* whose DataObjectInfo
s to parse.
* @return A List
of DataObjectInfo
API objects
* containing the data from the DataObjectInfo
DOM elements.
* @throws MOAApplicationException An error occurred parsing one of the
* DataObjectInfo
s.
*/
private List parseDataObjectInfos(Element sigInfoElem)
throws MOAApplicationException {
List dataObjectInfos = new ArrayList();
NodeIterator dataObjInfoElems =
XPathUtils.selectNodeIterator(sigInfoElem, DATA_OBJECT_INFO_XPATH);
Element dataObjInfoElem;
while ((dataObjInfoElem = (Element) dataObjInfoElems.nextNode()) != null) {
dataObjectInfos.add(parseDataObjectInfo(dataObjInfoElem));
}
return dataObjectInfos;
}
/**
* Parse a DataObjectInfo
DOM element.
*
* @param dataObjInfoElem The DataObjectInfo
DOM element to
* parse.
* @return A DataObjectInfo
API element containing the data
* from dataObjInfoElem
.
* @throws MOAApplicationException An error occurred parsing the
* DataObjectInfo
.
*/
private DataObjectInfo parseDataObjectInfo(Element dataObjInfoElem)
throws MOAApplicationException {
String structure = dataObjInfoElem.getAttribute("Structure");
Element dataObjectElem =
(Element) XPathUtils.selectSingleNode(dataObjInfoElem, DATA_OBJECT_XPATH);
Content dataObject = RequestParserUtils.parseContent(dataObjectElem);
CreateTransformsInfoProfile createTransformsInfoProfile =
parseCreateTransformsInfoProfile(dataObjInfoElem);
boolean childOfManifest;
if (dataObjInfoElem.hasAttribute("ChildOfManifest")) {
childOfManifest =
BoolUtils.valueOf(dataObjInfoElem.getAttribute("ChildOfManifest"));
} else {
childOfManifest = false;
}
return factory.createDataObjectInfo(
structure,
childOfManifest,
dataObject,
createTransformsInfoProfile);
}
/**
* Parse a CreateTransformsInfoProfile
DOM element.
*
* @param dataObjInfoElem The DataObjectInfo
DOM element
* containing the CreateTransformsInfoProfile
.
* @return The CreateTransformsInfoProfile
API object containing
* the profile found in dataObjInfoElem
.
* @throws MOAApplicationException An error occurred parsing the
* CreateTransformsInfoProfile
.
*/
private CreateTransformsInfoProfile parseCreateTransformsInfoProfile(Element dataObjInfoElem)
throws MOAApplicationException {
Element profileElem =
(Element) XPathUtils.selectSingleNode(
dataObjInfoElem,
CREATE_TRANSFORMS_INFO_PROFILE_XPATH);
if ("CreateTransformsInfoProfile".equals(profileElem.getLocalName())) {
ProfileParser profileParser = new ProfileParser();
return profileParser.parseCreateTransformsInfoProfile(profileElem);
} else {
String profileID = DOMUtils.getText(profileElem);
return factory.createCreateTransformsInfoProfile(profileID);
}
}
/**
* Parse the CreateSignatureInfo
DOM element contained in
* a SingleSignatureInfo
.
*
* @param sigInfoElem The SingleSignatureInfo
DOM element
* containing the CreateSignatureInfo
to be parsed.
* @return A CreateSignatureInfo
API object containing the
* data from the CreateSignatureInfo
DOM element, or
* null
, if none was found.
*/
private CreateSignatureInfo parseCreateSignatureInfo(Element sigInfoElem) {
Element createInfoElem =
(Element) XPathUtils.selectSingleNode(
sigInfoElem,
CREATE_SIGNATURE_INFO_XPATH);
if (createInfoElem != null) {
Element environmentElem =
(Element) XPathUtils.selectSingleNode(
createInfoElem,
CREATE_SIGNATURE_ENVIRONMENT_XPATH);
Content environment = RequestParserUtils.parseContent(environmentElem);
CreateSignatureEnvironmentProfile environmentProfile =
parseCreateSignatureEnvironmentProfile(createInfoElem);
return factory.createCreateSignatureInfo(environment, environmentProfile);
} else {
return null;
}
}
/**
* Parse the CreateSignatureEnvironmentProfile
contained in
* the given CreateSignatureInfo
DOM element.
*
* @param createInfoElem CreateSignatureInfo
DOM element to
* parse.
* @return The CreateSignatureEnvironmentProfile
contained
* in the given CreateSignatureInfo
DOM element..
*/
private CreateSignatureEnvironmentProfile parseCreateSignatureEnvironmentProfile(Element createInfoElem) {
Element profileElem =
(Element) XPathUtils.selectSingleNode(
createInfoElem,
CREATE_SIGNATURE_ENVIRONMENT_PROFILE_XPATH);
if ("CreateSignatureEnvironmentProfile"
.equals(profileElem.getLocalName())) {
ProfileParser profileParser = new ProfileParser();
return profileParser.parseCreateSignatureEnvironmentProfile(profileElem);
} else {
String profileID = DOMUtils.getText(profileElem);
return factory.createCreateSignatureEnvironmentProfile(profileID);
}
}
}