Women in Technology

Hear us Roar



Article:
  XML Document Validation with an XML Schema
Subject:   sample code quality - bad
Date:   2006-04-26 03:26:17
From:   gninneh
Response to: sample code quality - bad

I mean the comments in the text here _not_ the downloadable files. It would be better to print the whole class in one piece and add comments either in correct source code format in between, Ior before or after the classes' code. Then people could just copy and paste that code from the browser.


As opposed to that, the downloadable code has no comments and is probably runnable, but it's absolutely unreadable because it has an indentation that seems to be made by some randomuzer function - even the eclipse source formatter isn't able anymore to make something nice out of it.
Did you ever look at this downloadable code?
How do you think does it look?

Full Threads Oldest First

Showing messages 1 through 4 of 4.

  • sample code quality - bad
    2006-04-26 08:02:02  Deepak Vohra | [View]

    Please add indentation with Ctrl+Shift+F in Eclipse.
    • sample code quality - bad
      2006-05-26 06:45:06  gninneh [View]

      Thanks for this very helpful hint. Did you try this yourself?

      I cite from my previous post:
      "Even the eclipse source formatter isn't able anymore to make something nice out of it."

      You indent randomly, write multiple lines of code in one, completely at random. This is so bad that even eclipse can't make it better automatically.

      Do you really think code should look like this?
      • sample code quality - bad
        2006-05-27 16:06:51  Deepak Vohra | [View]

        I have formatted the code.
        When posted, the formatting does not get displayed.

      • sample code quality - bad
        2006-05-27 16:01:56  Deepak Vohra | [View]

        Formatted code:

        JAXPValidator.java:

        import org.xml.sax.SAXException;
        import org.xml.sax.SAXParseException;
        import org.xml.sax.helpers.DefaultHandler;

        import javax.xml.parsers.DocumentBuilder;
        import javax.xml.parsers.DocumentBuilderFactory;
        import javax.xml.parsers.ParserConfigurationException;


        public class JAXPValidator {
        public void validateSchema(String SchemaUrl, String XmlDocumentUrl) {
        try {
        System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
        "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setValidating(true);
        factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
        "http://www.w3.org/2001/XMLSchema");
        factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource",
        SchemaUrl);

        DocumentBuilder builder = factory.newDocumentBuilder();
        Validator handler = new Validator();
        builder.setErrorHandler(handler);
        builder.parse(XmlDocumentUrl);

        if (handler.validationError == true) {
        System.out.println("XML Document has Error:" +
        handler.validationError + " " +
        handler.saxParseException.getMessage());
        } else {
        System.out.println("XML Document is valid");
        }
        } catch (java.io.IOException ioe) {
        System.out.println("IOException " + ioe.getMessage());
        } catch (SAXException e) {
        System.out.println("SAXException" + e.getMessage());
        } catch (ParserConfigurationException e) {
        System.out.println(
        "ParserConfigurationException " +
        e.getMessage());
        }
        }

        public static void main(String[] argv) {
        String SchemaUrl = argv[0];
        String XmlDocumentUrl = argv[1];
        JAXPValidator validator = new JAXPValidator();
        validator.validateSchema(SchemaUrl, XmlDocumentUrl);
        }

        private class Validator extends DefaultHandler {
        public boolean validationError = false;
        public SAXParseException saxParseException = null;

        public void error(SAXParseException exception)
        throws SAXException {
        validationError = true;
        saxParseException = exception;
        }

        public void fatalError(SAXParseException exception)
        throws SAXException {
        validationError = true;
        saxParseException = exception;
        }

        public void warning(SAXParseException exception)
        throws SAXException {
        }
        }
        }



        SchemaValidator.java:


        import org.apache.xerces.parsers.SAXParser;

        import org.xml.sax.SAXException;
        import org.xml.sax.SAXParseException;
        import org.xml.sax.helpers.DefaultHandler;

        import java.io.IOException;


        public class SchemaValidator {
        public void validateSchema(String SchemaUrl, String XmlDocumentUrl) {
        SAXParser parser = new SAXParser();

        try {
        parser.setFeature("http://xml.org/sax/features/validation", true);
        parser.setFeature("http://apache.org/xml/features/validation/schema",
        true);
        parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking",
        true);
        parser.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
        SchemaUrl);

        Validator handler = new Validator();
        parser.setErrorHandler(handler);
        parser.parse(XmlDocumentUrl);

        if (handler.validationError == true) {
        System.out.println("XML Document has Error:" +
        handler.validationError + "" +
        handler.saxParseException.getMessage());
        } else {
        System.out.println("XML Document is valid");
        }
        } catch (java.io.IOException ioe) {
        System.out.println("IOException" + ioe.getMessage());
        } catch (SAXException e) {
        System.out.println("SAXException" + e.getMessage());
        }
        }

        public static void main(String[] argv) {
        String SchemaUrl = argv[0];
        String XmlDocumentUrl = argv[1];
        SchemaValidator validator = new SchemaValidator();
        validator.validateSchema(SchemaUrl, XmlDocumentUrl);
        }

        private class Validator extends DefaultHandler {
        public boolean validationError = false;
        public SAXParseException saxParseException = null;

        public void error(SAXParseException exception)
        throws SAXException {
        validationError = true;
        saxParseException = exception;
        }

        public void fatalError(SAXParseException exception)
        throws SAXException {
        validationError = true;
        saxParseException = exception;
        }

        public void warning(SAXParseException exception)
        throws SAXException {
        }
        }
        }