⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlvalidator.java

📁 jive 2.1.1 的源码
💻 JAVA
字号:
/**
 * $RCSfile: XMLValidator.java,v $
 * $Revision: 1.3 $
 * $Date: 2001/06/19 03:31:45 $
 *
 * Copyright (C) 1999-2001 CoolServlets Inc. All rights reserved.
 * ===================================================================
 * The Jive Software License, Version 1.0
 * (Derived from Apache Software License Version 1.1)
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by
 *        Jive Software (http://www.jivesoftware.com)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Jive", "Jive Software, and "CoolServlets" must not be used
 *    to endorse or promote products derived from this software without
 *    prior written permission. For written permission, please
 *    contact info@jivesoftware.com.
 *
 * 5. Products derived from this software may not be called "Jive",
 *    nor may "Jive" appear in their name, without prior written
 *    permission of CoolServlets Inc.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL COOLSERVLETS.COM OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ===================================================================
 */

package com.jivesoftware.forum.tool;

import java.io.*;
import java.text.*;

import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;


/**
 * A class to validate XML documents. The DTD must be available at the
 * location specified in the XML file for this to work.<p>
 *
 * Usage:<p>
 *
 * java -jar XMLValidator.jar [xml file]
 */
public class XMLValidator extends DefaultHandler {

    private Locator locator = null;
    private boolean validXML = true;
    private long validationTime = 0;
    private int errorCount = 0;

    public boolean isValidXML() {
        return validXML;
    }

    public void setValidXML(boolean validXML) {
        if (!validXML) {
            errorCount++;
        }
        this.validXML = validXML;
    }

    public long getValidationTime() {
        return validationTime;
    }

    public int getErrorCount() {
        return errorCount;
    }

    public void validate(InputStream in) {
        if (in == null) {
            System.out.println("No input source, can't validate");
            return;
        }
        super.setDocumentLocator(locator);

        // Initialize SAX parser
        SAXParser parser = null;
        try {
            SAXParserFactory parserFactory = SAXParserFactory.newInstance();
            parser = parserFactory.newSAXParser();
        }
        catch (ParserConfigurationException pce) {
            System.out.println("Can't load SAX parser.");
            return;
        }
        catch (SAXException e) {
            System.out.println("Can't load SAX parser.");
            return;
        }

        try {
            // set features of parser
            parser.setProperty("http://xml.org/sax/features/validation", new Boolean(true));
            // parse!
            validationTime = System.currentTimeMillis();
            parser.parse(new InputSource(in), this);
            validationTime = System.currentTimeMillis() - validationTime;
        }
        catch (IOException e) {
            System.out.println("Error reading input source");
            return;
        }
        catch (SAXException e) {
            System.out.println("Error parsing XML at line num "
                + locator.getLineNumber() + ":"
                + locator.getColumnNumber());
            e.printStackTrace();
        }
    }

    private void printError (SAXParseException e, String msg) {
        setValidXML(false);
        System.out.println(msg
                + e.getLineNumber() + ":"
                + e.getColumnNumber());
        System.out.println(e.getMessage());
        System.out.println("\n");
    }

    /* sax methods */
    public void warning(SAXParseException e) throws SAXException {
        printError(e,"SAX Warning at XML line num ");
    }

    public void error(SAXParseException e) throws SAXException {
        printError(e,"SAX Non-Fatal error at XML line num ");
    }

    public void fatalError(SAXParseException e) throws SAXException {
        printError(e,"SAX Fatal error at XML line num ");
    }

    public static void main(String args[]) {
        if (args.length < 1) {
            System.out.println("USAGE: java -jar XMLValidator.jar [xml file]");
            System.exit(0);
        }
        try {
            BufferedInputStream xmlIn = new BufferedInputStream(
                new FileInputStream(args[0]),8*1024*1024
            );
            File xmlFile = new File(args[0]);
            DecimalFormat decimalFormatter = new DecimalFormat("##.00");

            System.out.println("");
            System.out.println("Validating " + xmlFile.getAbsoluteFile()
                + " ("
                + decimalFormatter.format(((double)xmlFile.length()/1024.0))
                + "K)");
            System.out.println("");
            System.out.println("");

            XMLValidator validator = new XMLValidator();
            validator.validate(xmlIn);

            System.out.println("");
            System.out.print(validator.getErrorCount() + " error"
                + ((validator.getErrorCount()!=1)?"s":"") + " found, ");

            if (validator.isValidXML()) {
                System.out.print(xmlFile.getAbsoluteFile() + " is valid.");
            }
            else {
                System.out.print(xmlFile.getAbsoluteFile() + " is not valid.");
            }
            System.out.println("");
            System.out.println("Time: "
                + decimalFormatter.format((((double)validator.getValidationTime())/1000.0))
                + " seconds");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -