Example: Creating a Selector
This code was written by JD Edwards EnterpriseOne to build the Version 1 XML selector. This sample code uses the XTS.jar file. You can use this code as a sample for creating your selector:
File: XTSMJDETemplateSelector.java
////////////////////////////////////////////////////////////////////////////////
package com.jdedwards.xts.xtsm;
import com.jdedwards.xts.xtsr.IXTSRepository;
import com.jdedwards.xts.xtsr.IXTSRKey;
import com.jdedwards.xts.xtsr.XTSRException;
import com.jdedwards.xts.xtsr.XTSRInvalidKeyStringException;
import com.jdedwards.xts.xtsr.XTSRInvalidKeyFieldException;
import com.jdedwards.xts.xtsr.XTSRKeyNotFoundException;
import com.jdedwards.xts.XTSDocument;
import com.jdedwards.xts.XTSFactory;
import com.jdedwards.xts.XTSLog;
import com.jdedwards.xts.XTSConfigurationException;
import com.jdedwards.xts.XTSXMLParseException;
import com.jdedwards.xts.xtsm.IXTSMTemplateSelector;
import com.jdedwards.xts.xtse.IXTSEngine;
import com.jdedwards.xts.xtse.IXTSECompiledProcessor;
import java.util.List;
import org.w3c.dom.*;
/**
* This class is the Template Selector. It recognizes
* JD Edwards EnterpriseOne standard XML documents and returns the
* appropriate XSL stylesheets necessary for transformation.
*/
public class XTSMJDETemplateSelector implements IXTSMTemplateSelector
{
/** Class constructor. */
public XTSMJDETemplateSelector()
{
XTSLog.trace(XTSMJDETemplateSelector()'', 3);
// get repository reference
XTSFactory factory = XTSFactory.getInstance();
m_repository = factory.createXTSRepository();
}
/**
* Fetch the appropriate XSLT documents and IXTSECompiledProcessors as
* indicated by the TPT stored in the <code>info</code> parameter.
* @param info - Selection Info that contains TPI and should be modified
* by the selector to specify transformation information.
* @exception IXTSMTemplateSelector.TemplateFetchException - thrown
* if an error occurs when extracting information from the
* inclement.
*/
public void fetchTemplates(IXTSMSelectionInfo info)
throws IXTSMTemplateSelector.TemplateFetchException
{
XTSLog.trace("XTSMJDETemplateSelector.fetchTemplates(XTSMSelectionResult)",
3);
NodeList nodes = info.getTPIElement().getElementsByTagName(JDE_TS_XTSR_KEY);
int numNodes = nodes.getLength();
for(int i = 0; i < numNodes; i++)
{
// extract key info & create a key
IXTSRKey key = createKeyFromNode((Element)nodes.item(i));
// fetch the doc and add it to the list
try
{
info.getXSLList().add(m_repository.fetch(key));
}
catch (XTSRKeyNotFoundException e)
{
throw new IXTSMTemplateSelector.TemplateFetchException(
"Selected XTSRKey not found in repository: "
+ JDE_TS_XTSR_KEY);
}
catch (XTSRException e)
{
throw new IXTSMTemplateSelector.TemplateFetchException(
"Unable to fetch the XSL document specified within '"
+ JDE_TS_XTSR_KEY +
"' from the XTSRepository");
}
}
}
/**
* Fetch the appropriate XSLT documents and compiled processors for
* the given document.
* @param inXML - the XTSDocument to try to recognize.
* @param info - Selection Info object to be modified by selector to
* indicate transformation information.
* @return - <code>true</code> if the selector has recognized the
* document and specified the appropriate selection info using
* <code>info</code>, <code>false</code> otherwise.
* @exception TemplateFetchException - thrown when an error occurs
* when trying to recognize the DOM.
* @exception XTSXMLParseException - thrown if <inXML> could not be
* parsed.
*/
public boolean fetchTemplates(XTSDocument inXML,
IXTSMSelectionInfo info)
throws IXTSMTemplateSelector.TemplateFetchException,
XTSXMLParseException
{
XTSLog.trace("XTSMJDETemplateSelector.fetchTemplates(Document, Element)", 3);
boolean recognized = false;
Document inDOM = inXML.getDOM();
// see if an XTSR key is specified within the document:
NodeList nodeList = inDOM.getElementsByTagName(JDE_XTSR_KEY);
if (nodeList.getLength() > 0)
{
try
{
// extract key info & create a key
IXTSRKey key = createKeyFromNode((Element)nodeList.item(0));
// add transformation path information to outElement
createNodeChildFromKey(info.getTPIElement(), key);
// fetch the doc and add it to the list
info.getXSLList().add(m_repository.fetch(key));
info.setResultXML(true);
info.setPathInfoStored(false);
recognized = true;
}
catch (XTSRException e)
{
throw new IXTSMTemplateSelector.TemplateFetchException(
"Unable to fetch the XSL document specified within '"
+ JDE_XTSR_KEY +
"' from the XTSRepository");
}
catch (XTSRKeyNotFoundException e)
{
throw new IXTSMTemplateSelector.TemplateFetchException(
"Key specified in TPI not found in repository"
+ JDE_XTSR_KEY);
}
}
else // no XTSR key, so look for JDE information:
{
nodeList = inDOM.getElementsByTagName(JDE_INT_BPAPI);
if (nodeList.getLength() != 0)
{
// add transformation path information to outElement
createNodeChildFromKey(info.getTPIElement(), getVersion1toNativeKey());
// fetch the doc and add it to the list
info.getXSLList().add(getVersion1toNativeXSL());
info.setResultXML(true);
info.setPathInfoStored(true);
recognized = true;
}
}
return recognized;
}
/**
* Extracts XTSRKey information from the given node, and creates an
* instance of IXTSRKey based on that information.
* @return - the new IXTSRKey.
* @param element - Element that contains the key information.
* @exception XTSMUnrecognizedElementException - thrown if the
* Element format is unrecognized.
*/
protected IXTSRKey createKeyFromNode(Element element)
throws XTSMUnrecognizedElementException
{
XTSLog.trace("XTSMJDETemplateSelector.createKeyFromNode(Element)", 4);
IXTSRKey key = null;
boolean request = false;
boolean response = false;
if (element.getNodeName().equals(JDE_XTSR_KEY))
{
request = true;
}
else if (element.getNodeName().equals(JDE_TS_XTSR_KEY))
{
response = true;
}
if (request || response)
{
key = m_repository.createKey();
try
{
String keyString = element.getAttribute(JDE_XTSR_KEY_ATTRIBUTE);
key.setFieldsFromString(keyString);
if (key.getFieldValue(SUBTYPE_FIELD).length() == 0)
{
if (request)
{
key.setFieldValue(SUBTYPE_FIELD, SUBTYPE_REQUEST);
}
else
{
key.setFieldValue(SUBTYPE_FIELD, SUBTYPE_RESPONSE);
}
}
}
catch (XTSRInvalidKeyStringException e)
{
throw new XTSMUnrecognizedElementException(
"Specified '" + JDE_XTSR_KEY +
"' element format is invalid for this XTSRepository");
}
catch (XTSRInvalidKeyFieldException e)
{
throw new XTSConfigurationException(
"Specified '" + SUBTYPE_FIELD +
"' field name not supported by repository key");
}
}
return key;
}
/**
* Creates a node that contains the key fields values and appends it
* to the given parentNode.
* @param parentNode - Node to which the key information should be
* appended.
* @param key - Key information to store in the node.*/
protected void createNodeChildFromKey(Node parentNode, IXTSRKey key)
{
XTSLog.trace("XTSMJDETemplateSelector.createKeyFromNode(Node,IXTSRKey)", 4);
try
{
IXTSRKey keyClone = key.getRepository().createKey();
keyClone.setFieldsFromString(key.getFieldsString());
// Do not store the sub type, clear it here:
keyClone.setFieldValue(SUBTYPE_FIELD, "");
// create new node and append it to the provided element:
Element element = (Element)parentNode.getOwnerDocument().createElement
(JDE_TS_XTSR_KEY);
element.setAttribute(JDE_XTSR_KEY_ATTRIBUTE, keyClone.getFieldsString());
parentNode.appendChild(element);
}
catch (XTSRInvalidKeyStringException e)
{
XTSLog.log("Unexpected ");
XTSLog.log(e);
throw new RuntimeException("Unexpected Exception: " + e.toString());
}
}
/**
* Returns the key of the stylesheet to use in converting
* JD Edwards EnterpriseOne version 1 documents into EnterpriseOne native
* documents.
* @return - The key for the XSL stylesheet.
*/
protected IXTSRKey getVersion1toNativeKey()
{
XTSLog.trace("XTSMJDETemplateSelector.getVersion1toNativeKey()", 5);
if (null == m_version1ToNativeKey)
{
try
{
// create standard xsl XTSRKey:
m_version1ToNativeKey = m_repository.createKey();
m_version1ToNativeKey.setFieldsFromString(V1_TO_NATIVE_KEY);
}
catch (XTSRInvalidKeyStringException e)
{
String error = "XTSRKey necessary for JDE template selection is invalid: "
+ V1_TO_NATIVE_KEY;
XTSLog.log(error);
XTSLog.log(e);
throw new XTSConfigurationException(error);
}
}
return m_version1ToNativeKey;
}
/**
* Returns the XTSDocument which contains the XSL stylesheet for
* converting JD Edwards EnterpriseOne version 1 documents into JD Edwards
* EnterpriseOne native documents.
* @return - XTSDocument containing the XSL stylesheet.
*/
protected IXTSECompiledProcessor getVersion1toNativeXSL()
{
XTSLog.trace("XTSMJDETemplateSelector.getVersion1toNativeXSL()", 5);
if (null == m_version1ToNativeXSL)
{
XTSDocument xsl = null;
Try
{
xsl = m_repository.fetch(getVersion1toNativeKey());
IXTSEngine engine = XTSFactory.getInstance().createXTSEngine();
m_version1ToNativeXSL = engine.createCompiledProcessor(xsl);
}
catch (XTSRException e)
{
String error = "Unable to fetch selected template from the repository:";
XTSLog.log(error);
XTSLog.log(e);
throw new XTSConfigurationException(error + e.toString());
}
catch (XTSRKeyNotFoundException knfe)
{
String error = "Selected template XTSRKey not found in repository:";
XTSLog.log(error);
XTSLog.log(knfe);
throw new XTSConfigurationException(error + knfe.toString());
}
catch (XTSXMLParseException pe)
{
String error = "Invalid XSL document in repository";
XTSLog.log(error);
XTSLog.log(pe);
throw new XTSConfigurationException(error + pe.toString());
}
}
return m_version1ToNativeXSL;
}
/** Reference to the XTSRepository */
private IXTSRepository m_repository = null;
/** Key for converting version 1 documents to native documents. */
private IXTSRKey m_version1ToNativeKey = null;
/** Compiled XSL Stylesheet for converting version 1 docs to
* native docs. */
private IXTSECompiledProcessor m_version1ToNativeXSL = null;
/** Field Value for the XTSRKey that indicates the document is an XSL doc */
private static final String DOC_TYPE_XSL = "XSL";
/** Element name that indicates the DOM is a Version 1 document */
private static final String JDE_INT_BPAPI = "intBPAPI";
/** Element name that indicates the DOM is a request and not a
* response or error. */
private static final String JDE_REQUEST = "jdeRequest";
/** Element name that indicates the DOM is a response */
private static final String RESPONSE = "jdeResponse";
/** Element name that specifies an XTSRKey to use in transforming
* the document. */
private static final String JDE_XTSR_KEY = "jdeXTSRKey";
/** The attribute of the <code>JDE_XTSR_KEY</code> element that
* stores the XTSRKey string value */
private static final String JDE_XTSR_KEY_ATTRIBUTE = "key";
/** XTSRKey field name that specifies the sub-type of the XML
* document. Normal values for the sub-type are defined by
* <code>SUBTYPE_REQUEST</code> and <code>SUBTYPE_RESPONSE</code> */
private static final String SUBTYPE_FIELD = "SUB_TYPE";
/** XTSRKey field name which specifies the type of the XML document.
* The normal value is defined by <code>DOC_TYPE_XSL</code> */
private static final String FIELD_TYPE = "TYPE";
/** XTSRKey field name which specifies the format (or owner) of the
* XML document. The normal value recognized by this selector is
* 'JDE' */
private static final String FIELD_FORMAT = "FORMAT";
/** XTSRKey field name that specifies the particular transformation
* that the XSL document will perform. This selector uses
* 'V1_NATIVE' for transformations between JD Edwards EnterpriseOne Version 1
* XML documents and JD Edwards EnterpriseOne native version documents. */
private static final String FIELD_ID = "ID";
/** The string representation of the XTSRKey for the XSL document to
* format JD Edwards EnterpriseOne version 1 request documents into
* JD Edwards EnterpriseOne native request documents. */
private static final String V1_TO_NATIVE_KEY = "XSL-JDE-V1_NATIVE-REQUEST";
/** XTSRKey field <code>SUBTYPE_FIELD</code> value that indicates
* the XSL document will transform jdeRequest documents. */
private static final String SUBTYPE_REQUEST = "REQUEST";
/** XTSRKey field <code>SUBTYPE_FIELD</code> value that indicates
* the XSL document will transform jdeResponse documents. */
private static final String SUBTYPE_RESPONSE = "RESPONSE";
/** Element name stored within the Transformation Path Information
* (TPI)that specifies the XTSRKey used to transform the document. */
private static final String JDE_TS_XTSR_KEY = "XTSJDETemplateKey";
private static class XTSMUnrecognizedElementException
extends IXTSMTemplateSelector.TemplateFetchException
{
public XTSMUnrecognizedElementException(String text)
{
super(text);
}
}
}