4,10 → 4,10 |
* Coded by: Group 5, software practice summer 2003 |
* University of Bielefeld, Germany |
* |
* @version $Revision: 1.44 $ |
* @version $Revision: 1.45 $ |
* |
* Last modification: $Date: 2003/07/18 13:04:17 $ |
* $Id: Validator.java,v 1.44 2003/07/18 13:04:17 smcsporr Exp $ |
* Last modification: $Date: 2003/07/18 13:55:25 $ |
* $Id: Validator.java,v 1.45 2003/07/18 13:55:25 smcsporr Exp $ |
*/ |
|
package src.parser; |
19,18 → 19,16 |
import java.io.StringReader; |
import java.io.StringWriter; |
|
import javax.xml.transform.OutputKeys; |
import javax.xml.transform.Transformer; |
import javax.xml.transform.TransformerFactory; |
import javax.xml.transform.TransformerConfigurationException; |
import javax.xml.transform.TransformerException; |
import javax.xml.transform.TransformerFactory; |
|
import javax.xml.transform.dom.DOMSource; |
|
import javax.xml.transform.stream.StreamResult; |
import javax.xml.transform.OutputKeys; |
|
//import org.apache.xml.serializer.OutputPropertiesFactory; |
//import org.apache.xml.serializer.Serializer; |
//import org.apache.xml.serializer.SerializerFactory; |
|
import org.w3c.dom.Document; |
|
import org.xml.sax.InputSource; |
50,7 → 48,7 |
* |
* @author S. McSporran |
* |
* @version $Revision: 1.44 $ Last modification: $Date: 2003/07/18 13:04:17 $ |
* @version $Revision: 1.45 $ Last modification: $Date: 2003/07/18 13:55:25 $ |
*/ |
public class Validator { |
|
79,16 → 77,19 |
* @param logI: A reference to a log panel implementation. |
* @param statI: A reference to a status panel implementation. |
* |
* @see src.gui.LogInterface |
* @see src.gui.StatusInterface |
* |
* author S. McSporran |
* |
* Last revision: 11-Jul-2003 |
*/ |
public Validator(LogInterface logI, StatusInterface statI) |
{ |
public Validator(LogInterface logI, StatusInterface statI) { |
logInterfaceReference = logI; |
statInterfaceReference = statI; |
try |
{ |
|
try { |
|
/* Try to instantiate a SAX parser using the Xerces implementation. */ |
activeParser = XMLReaderFactory.createXMLReader(defaultParser); |
|
104,16 → 105,15 |
/* Activate the needed parser features. */ |
activateNamespaceProcessing(); |
activateValidation(); |
//activateDynamicValidation(); |
activateSchemaSupport(); |
} |
|
/* Catch errors while creating the parser object. */ |
catch (SAXException saxe1) |
{ |
try |
{ |
// Use the system's standard SAX parser. |
catch (SAXException saxe1) { |
|
try { |
|
/* Use the system's standard SAX parser. */ |
activeParser = XMLReaderFactory.createXMLReader(); |
|
/* Register an instance of the ValidationErrorHandler to the parser. */ |
145,6 → 145,8 |
/** |
* This method creates an instance of a <code>Transformer</code> object. |
* |
* @see javax.xml.transform.Transformer |
* |
* author S. McSporran |
* |
* Last revision: 30-Jun-2003 |
156,6 → 158,7 |
transFactory = TransformerFactory.newInstance(); |
myTransformer = transFactory.newTransformer(); |
} |
|
/* Catch and log creation errors. */ |
catch (TransformerConfigurationException tce) { |
setStatIcon(StatusInterface.ICON_ERROR); |
167,12 → 170,14 |
* This method registers an instance of the <code>ValidationErrorHandler</code> class |
* with the parser. |
* |
* @see src.parser.ValidationErrorHandler |
* |
* author S. McSporran |
* |
* Last revision: 18-Jun-2003 |
*/ |
private void registerErrorHandler() |
{ |
private void registerErrorHandler() { |
|
/* Create a new custom error handler. */ |
eHandler = new ValidationErrorHandler(); |
|
183,18 → 188,18 |
/** |
* This method activates the validation feature of the parser. |
* |
* @see <a href="http://xml.apache.org/xerces2-j/features.html">xerces validation</a> |
* |
* author S. McSporran |
* |
* Last revision: 18-Jun-2003 |
*/ |
private void activateValidation() |
{ |
private void activateValidation() { |
|
try |
{ |
try { |
|
/* Turn validation on, if the feature is deactivated. */ |
if (! activeParser.getFeature("http://xml.org/sax/features/validation")) |
{ |
if (! activeParser.getFeature("http://xml.org/sax/features/validation")) { |
activeParser.setFeature("http://xml.org/sax/features/validation", true); |
} |
} |
202,14 → 207,12 |
/* Send a message to the logging area, if the parser doesn't know the |
* validation feature. |
*/ |
catch (SAXNotRecognizedException snre) |
{ |
catch (SAXNotRecognizedException snre) { |
log(LogInterface.TYPE_ERROR,activeParser + " does not support validation."); |
} |
|
/* Send a message to the logging area, if the feature cannot be activated. */ |
catch (SAXNotSupportedException snse) |
{ |
catch (SAXNotSupportedException snse) { |
log(LogInterface.TYPE_ERROR,"The validation feature cannot be activated now."); |
} |
} |
221,14 → 224,12 |
* |
* Last revision: 18-Jun-2003 |
*/ |
private void activateDynamicValidation() |
{ |
private void activateDynamicValidation() { |
|
try |
{ |
try { |
|
/* Turn dynamic validation on, if the feature is deactivated. */ |
if (! activeParser.getFeature("http://apache.org/xml/features/validation/dynamic")) |
{ |
if (! activeParser.getFeature("http://apache.org/xml/features/validation/dynamic")) { |
activeParser.setFeature("http://apache.org/xml/features/validation/dynamic", true); |
} |
} |
236,14 → 237,12 |
/* Send a message to the logging area, if the parser doesn't know the |
* dynamic validation feature. |
*/ |
catch (SAXNotRecognizedException snre) |
{ |
catch (SAXNotRecognizedException snre) { |
log(LogInterface.TYPE_ERROR,activeParser + " does not support dynamic validation."); |
} |
|
/* Send a message to the logging area, if the feature cannot be activated. */ |
catch (SAXNotSupportedException snse) |
{ |
catch (SAXNotSupportedException snse) { |
log(LogInterface.TYPE_ERROR,"The dynamic validation feature cannot be activated now."); |
} |
} |
255,14 → 254,12 |
* |
* Last revision: 18-Jun-2003 |
*/ |
private void activateSchemaSupport() |
{ |
private void activateSchemaSupport() { |
|
try |
{ |
try { |
|
/* Turn dynamic validation on, if the feature is deactivated. */ |
if (! activeParser.getFeature("http://apache.org/xml/features/validation/schema")) |
{ |
if (! activeParser.getFeature("http://apache.org/xml/features/validation/schema")) { |
activeParser.setFeature("http://apache.org/xml/features/validation/schema", true); |
} |
} |
270,14 → 267,12 |
/* Send a message to the logging area, if the parser doesn't support |
* XML schema validation. |
*/ |
catch (SAXNotRecognizedException snre) |
{ |
catch (SAXNotRecognizedException snre) { |
log(LogInterface.TYPE_ERROR,activeParser + " does not support schema validation."); |
} |
|
/* Send a message to the logging area, if the feature cannot be activated. */ |
catch (SAXNotSupportedException snse) |
{ |
catch (SAXNotSupportedException snse) { |
log(LogInterface.TYPE_ERROR,"The schema validation support cannot be activated now."); |
} |
} |
289,14 → 284,11 |
* |
* Last revision: 18-Jun-2003 |
*/ |
private void activateNamespaceProcessing() |
{ |
private void activateNamespaceProcessing() { |
|
try |
{ |
try { |
/* Turn namespace processing on. */ |
if (! activeParser.getFeature("http://xml.org/sax/features/namespaces")) |
{ |
if (! activeParser.getFeature("http://xml.org/sax/features/namespaces")) { |
activeParser.setFeature("http://xml.org/sax/features/namespaces", true); |
} |
} |
304,14 → 296,12 |
/* Send a message to the logging area, if the parser doesn't support |
* namespace processing. |
*/ |
catch (SAXNotRecognizedException snre) |
{ |
catch (SAXNotRecognizedException snre) { |
log(LogInterface.TYPE_ERROR,activeParser + " does not support namespace processing."); |
} |
|
/* Send a message to the logging area, if the feature cannot be activated. */ |
catch (SAXNotSupportedException snse) |
{ |
catch (SAXNotSupportedException snse) { |
log(LogInterface.TYPE_ERROR,"The namespace processing feature cannot be activated now."); |
} |
} |
323,14 → 313,11 |
* |
* Last revision: 18-Jun-2003 |
*/ |
private void activateFatalErrorIgnorance() |
{ |
private void activateFatalErrorIgnorance() { |
|
try |
{ |
try { |
/* Turn break on fatal errors off. */ |
if (! activeParser.getFeature("http://apache.org/xml/features/continue-after-fatal-error")) |
{ |
if (! activeParser.getFeature("http://apache.org/xml/features/continue-after-fatal-error")) { |
activeParser.setFeature("http://apache.org/xml/features/continue-after-fatal-error", true); |
} |
} |
338,14 → 325,12 |
/* Send a message to the logging area, if the parser doesn't support |
* break on fatal error skipping. |
*/ |
catch (SAXNotRecognizedException snre) |
{ |
catch (SAXNotRecognizedException snre) { |
log(LogInterface.TYPE_ERROR,activeParser + " does not support the continue-after-fatal-error feature."); |
} |
|
/* Send a message to the logging area, if the feature cannot be activated. */ |
catch (SAXNotSupportedException snse) |
{ |
catch (SAXNotSupportedException snse) { |
log(LogInterface.TYPE_ERROR,"The continue-after-fatal-error feature cannot be activated now."); |
} |
} |
406,6 → 391,7 |
/* Set valid status to false in case of parsing errors. */ |
eHandler.setValid(false); |
} |
|
catch (IOException ioe) { |
log(LogInterface.TYPE_ERROR, "Validation I/O-error: " + ioe.getMessage()); |
|
413,6 → 399,7 |
eHandler.setValid(false); |
} |
} |
|
/* Catch and log transformation errors. */ |
catch (TransformerException te) { |
log(LogInterface.TYPE_ERROR, "Transformer error: " + te.getMessage()); |
420,6 → 407,7 |
/* Set valid status to false in case of transformation errors. */ |
eHandler.setValid(false); |
} |
|
return eHandler.isValid(); |
} |
|
433,13 → 421,15 |
* |
* Last revision: 30-Jun-2003 |
*/ |
public boolean validateStream(InputStream inStream) |
{ |
public boolean validateStream(InputStream inStream) { |
|
/* Set valid status in the ValidationErrorHandler to true. */ |
eHandler.setValid(true); |
|
try { |
activeParser.parse(new InputSource(new InputStreamReader(inStream))); |
} |
|
/* Catch and log parsing errors. */ |
catch (SAXException se) { |
log(LogInterface.TYPE_ERROR, "Validation processing error: " + se.getMessage()); |
447,6 → 437,7 |
/* Set valid status to false in case of parsing errors. */ |
eHandler.setValid(false); |
} |
|
catch (IOException ioe) { |
log(LogInterface.TYPE_ERROR, "Validation I/O-error: " + ioe.getMessage()); |
|
453,6 → 444,7 |
/* Set valid status to false in case of I/O errors. */ |
eHandler.setValid(false); |
} |
|
return eHandler.isValid(); |
} |
|
480,8 → 472,7 |
* |
* Last Revision: 11-Jul-2003 |
*/ |
public void setLogInterface (LogInterface log) |
{ |
public void setLogInterface (LogInterface log) { |
logInterfaceReference = log; |
} |
} |