In setupFileDragAndDrop
you could probably make it so that you dont have to re-use the id's like 'aceInputXml'
again ( DRY ). Also you should consider putting stopEvent out of addFileDragAndDropEventListeners, since you will now have 2 stopEvent functions ?
setupFileInput
contains 50% copy pasted code, not DRY.
setupTransformButton
contains a lot of declarations and assignments that could be merged.
Also since you don't keep the button value around you could simply replace
function setupTransformButton(Saxon) {
var button;
button = document.getElementById('btnTransform');
button.addEventListener('click', function (e) {
var inputXmlStr, inputXsltStr, outputXmlStr,
inputXmlDoc, inputXsltDoc, outputXmlDoc, processor;
// Get the input XML and XSLT strings from the ACE editors.
inputXmlStr = aceInputXml.getSession().getValue();
inputXsltStr = aceInputXslt.getSession().getValue();
// Transform the inputs into Saxon XML documents.
inputXmlDoc = Saxon.parseXML(inputXmlStr);
inputXsltDoc = Saxon.parseXML(inputXsltStr);
// Get an XSLT20Processor object that will apply the transformation.
processor = Saxon.newXSLT20Processor(inputXsltDoc);
// Apply the transformation.
outputXmlDoc = processor.transformToDocument(inputXmlDoc);
outputXmlStr = Saxon.serializeXML(outputXmlDoc);
// Put the results of the transformation in the output ACE editor.
aceOutputXml.getSession().setValue(outputXmlStr);
});
}
with
function setupTransformButton(Saxon)
{
document.getElementById('btnTransform').addEventListener('click', function (e)
{
/* Get the input XML and XSLT strings from the ACE editors. */
var inputXmlStr = aceInputXml.getSession().getValue(),
inputXsltStr = aceInputXslt.getSession().getValue();
/* Transform the inputs into Saxon XML documents. */
var inputXmlDoc = Saxon.parseXML(inputXmlStr),
inputXsltDoc = Saxon.parseXML(inputXsltStr);
/* Get an XSLT20Processor object that will apply the transformation */
var processor = Saxon.newXSLT20Processor(inputXsltDoc);
/* Apply the transformation */
var outputXmlDoc = processor.transformToDocument(inputXmlDoc),
outputXmlStr = Saxon.serializeXML(outputXmlDoc);
/* Put the results of the transformation in the output ACE editor. */
aceOutputXml.getSession().setValue(outputXmlStr);
});
}
After writing this, I guess it really is a matter of taste, something to consider rather than a hard rule.
In setupFileDragAndDrop
you could probably make it so that you dont have to re-use the id's like 'aceInputXml'
again ( DRY ). Also you should consider putting stopEvent out of addFileDragAndDropEventListeners, since you will now have 2 stopEvent functions ?
setupFileInput
contains 50% copy pasted code, not DRY.
setupTransformButton
contains a lot of declarations and assignments that could be merged.
Also since you don't keep the button value around you could simply replace
function setupTransformButton(Saxon) {
var button;
button = document.getElementById('btnTransform');
button.addEventListener('click', function (e) {
var inputXmlStr, inputXsltStr, outputXmlStr,
inputXmlDoc, inputXsltDoc, outputXmlDoc, processor;
// Get the input XML and XSLT strings from the ACE editors.
inputXmlStr = aceInputXml.getSession().getValue();
inputXsltStr = aceInputXslt.getSession().getValue();
// Transform the inputs into Saxon XML documents.
inputXmlDoc = Saxon.parseXML(inputXmlStr);
inputXsltDoc = Saxon.parseXML(inputXsltStr);
// Get an XSLT20Processor object that will apply the transformation.
processor = Saxon.newXSLT20Processor(inputXsltDoc);
// Apply the transformation.
outputXmlDoc = processor.transformToDocument(inputXmlDoc);
outputXmlStr = Saxon.serializeXML(outputXmlDoc);
// Put the results of the transformation in the output ACE editor.
aceOutputXml.getSession().setValue(outputXmlStr);
});
}
with
function setupTransformButton(Saxon)
{
document.getElementById('btnTransform').addEventListener('click', function (e)
{
/* Get the input XML and XSLT strings from the ACE editors. */
var inputXmlStr = aceInputXml.getSession().getValue(),
inputXsltStr = aceInputXslt.getSession().getValue();
/* Transform the inputs into Saxon XML documents. */
var inputXmlDoc = Saxon.parseXML(inputXmlStr),
inputXsltDoc = Saxon.parseXML(inputXsltStr);
/* Get an XSLT20Processor object that will apply the transformation */
var processor = Saxon.newXSLT20Processor(inputXsltDoc);
/* Apply the transformation */
var outputXmlDoc = processor.transformToDocument(inputXmlDoc),
outputXmlStr = Saxon.serializeXML(outputXmlDoc);
/* Put the results of the transformation in the output ACE editor. */
aceOutputXml.getSession().setValue(outputXmlStr);
});
}
After writing this, I guess it really is a matter of taste, something to consider rather than a hard rule.
Your code looks fine to me,
I would probably have aceInputXml
, aceInputXslt
and aceOutputXml
be properties of 'editors'.
Then you could slightly rewrite setupAce as
function setupAce()
{
var key;
editors =
{
InputXml : ace.edit('aceInputXml'),
InputXslt : ace.edit('aceInputXslt'),
OutputXml : ace.edit('aceOutputXml')
}
for (key in editors)
{
editors[key].setTheme('ace/theme/monokai');
editors[key].getSession().setMode('ace/mode/xml');
}
}