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

📄 octopusvalidator.java

📁 数据仓库工具
💻 JAVA
字号:
/**
  OctopusValidator - Class used for validating XML files.

    Copyright (C) 2002-2003  Together

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

 OctopusValidator.java
 Date: 20.5.2003.
 @version 1.0.0
 @author: Zoran Milakovic zoran@prozone.co.yu
 */

package org.webdocwf.util.loader;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.xerces.parsers.DOMParser;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * Class used for validation of XML files.
 *
 * @author     Zoran Milakovic
 * @version    1.1
 */
public class OctopusValidator extends DefaultHandler {


  /**
   * Validate xml document which is passed in inStream parameter.
   * @param inStream xml document to validate
   * @throws SAXException
   * @throws SAXParseException
   * @throws ParserConfigurationException
   * @throws IOException
   **/
  public void validate(ByteArrayInputStream inStream) throws Exception
  {
  	
		final String JAXP_SCHEMA_LANGUAGE =
				"http://java.sun.com/xml/jaxp/properties/schemaLanguage";
		final String W3C_XML_SCHEMA =
			"http://www.w3.org/2001/XMLSchema";
  	
    DOMParser parser = new DOMParser();

		try {
			parser.setFeature("http://xml.org/sax/features/validation",true);
			parser.setFeature("http://apache.org/xml/features/validation/schema",true);
			parser.setErrorHandler(new OctopusErrorHandler());
			parser.setEntityResolver(new OctopusEntityResolver());
			
			parser.parse(new InputSource(inStream));
	
		} catch (Exception e) {
			throw e;
		}
  }

  /**
   *
   * @param ex SAXParseException
   * @return line and column where error occured in input stream created from loaderJob.xml and ALL
   * included .xml files.
   */
  private String getLocationString (SAXParseException ex) {
    StringBuffer str = new StringBuffer();
    String systemId = ex.getSystemId();
    if (systemId != null) {
      int index = systemId.lastIndexOf('/');
      if (index != -1)
        systemId = systemId.substring(index + 1);
      str.append(systemId);
    }
    str.append(':');
    str.append(ex.getLineNumber());
    str.append(':');
    str.append(ex.getColumnNumber());
    return  str.toString();
  }

  class OctopusErrorHandler implements ErrorHandler
  {

    // throw SAXException for fatal errors
    public void fatalError( SAXParseException exception ) throws SAXException
    {
      //System.err.println("[Validation : FatalError ] URI = " + getLocationString(exception) + ": " + exception.getMessage());
      LocationOfException.getLineNumber(exception.getLineNumber());
      System.err.println("[Validation : FatalError ] URI : "+LocationOfException.getFileName());
      System.err.println("[Validation : FatalError ] Line number : "+LocationOfException.getLineNumber(exception.getLineNumber()));
      throw new SAXException(exception);
    }

    public void error( SAXParseException errorException ) throws SAXException
    {
//      System.err.println("[Validation : Error ] URI = " + getLocationString(errorException) + ": " + errorException.getMessage());
      LocationOfException.getLineNumber(errorException.getLineNumber());
      System.err.println("[Validation : Error ] URI : "+LocationOfException.getFileName());
      System.err.println("[Validation : Error ] Line number : "+LocationOfException.getLineNumber(errorException.getLineNumber()));
      throw new SAXException(errorException);
    }

    // print any warnings
    public void warning( SAXParseException warningError ) throws SAXException
    {
      System.err.println("[Validation : Warning] URI : " + getLocationString(warningError) + ": " + warningError.getMessage());
    }
  }


}

⌨️ 快捷键说明

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