verifierhandlerimpl.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 820 行 · 第 1/2 页

JAVA
820
字号
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source 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, or any warranty * of NON-INFRINGEMENT.  See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * *   Free Software Foundation, Inc. *   59 Temple Place, Suite 330 *   Boston, MA 02111-1307  USA * * @author Scott Ferguson */package com.caucho.relaxng;import com.caucho.relaxng.program.EmptyItem;import com.caucho.relaxng.program.Item;import com.caucho.util.CharBuffer;import com.caucho.util.L10N;import com.caucho.util.LruCache;import com.caucho.vfs.Path;import com.caucho.vfs.ReadStream;import com.caucho.vfs.Vfs;import com.caucho.xml.QName;import org.xml.sax.Attributes;import org.xml.sax.ErrorHandler;import org.xml.sax.Locator;import org.xml.sax.SAXException;import org.xml.sax.SAXParseException;import org.xml.sax.helpers.DefaultHandler;import java.io.IOException;import java.util.ArrayList;import java.util.Collections;import java.util.HashSet;import java.util.LinkedHashSet;import java.util.logging.Level;import java.util.logging.Logger;/** * JARV verifier implementation */public class VerifierHandlerImpl extends DefaultHandler  implements VerifierHandler{  private static final L10N L = new L10N(VerifierHandlerImpl.class);  protected static final Logger log    = Logger.getLogger(VerifierHandlerImpl.class.getName());  // very verbose logging  private static final boolean _isDebug = false;  private SchemaImpl _schema;  private VerifierImpl _verifier;  private boolean _hasProgram;  private boolean _isValid = true;  private LruCache<Object,Item> _programCache;  private QName _name;  private ArrayList<QName> _nameStack = new ArrayList<QName>();  private String _eltLocation;  private ArrayList<String> _eltLocationStack = new ArrayList<String>();  private Item _item;  private ArrayList<Item> _itemStack = new ArrayList<Item>();  private Locator _locator;    private boolean _isLogFinest;  private CharBuffer _text = new CharBuffer();  private boolean _hasText;    private StartKey _startKey = new StartKey();  private EndElementKey _endElementKey = new EndElementKey();  /**   * Creates the Verifier Handler.   */  public VerifierHandlerImpl(SchemaImpl schema, VerifierImpl verifier)  {    _schema = schema;    _programCache = _schema.getProgramCache();    _verifier = verifier;  }  /**   * Sets the locator.   */  public void setDocumentLocator(Locator locator)  {    _locator = locator;  }    /**   * Sets the error handler   */  public void setErrorHandler(ErrorHandler errorHandler)    throws SAXException  {    _verifier.setErrorHandler(errorHandler);  }  private String getFileName()  {    if (_locator != null)      return _locator.getSystemId();    else      return null;  }  private int getLine()  {    if (_locator != null)      return _locator.getLineNumber();    else      return -1;  }  /**   * Called when the document starts.   */  public void startDocument()    throws SAXException  {    try {      _nameStack.clear();      _itemStack.clear();      _eltLocationStack.clear();      _name = new QName("#top", "");      _item = _schema.getStartItem();      _itemStack.add(_item);      _eltLocation = getLocation();      _isLogFinest = _isDebug && log.isLoggable(Level.FINEST);      _hasText = false;      _text.clear();    } catch (Exception e) {      error(e);    }  }  /**   * Called when an element starts.   */  public void startElement(String uri, String localName,                           String qName, Attributes attrs)    throws SAXException  {    if (! _isValid)      return;    if (_hasText)      sendText();        if (_isLogFinest)      log.finest("element start: " + qName);    try {      QName parent = _name;      _nameStack.add(parent);      String parentLocation = _eltLocation;      _eltLocationStack.add(parentLocation);            QName name = new QName(qName, uri);      _name = name;      _eltLocation = getLocation();      Item newItem = getStartElement(_item, name);      if (newItem == null) {	Item parentItem = _itemStack.get(_itemStack.size() - 1);	        if (parent.getName().equals("#top"))          throw new RelaxException(L.l("<{0}> is an unexpected top-level tag.{1}",                                       errorNodeName(name, _item, parentItem),				       errorMessageDetail(_item, parentItem, null, name)));        else          throw new RelaxException(L.l("<{0}> is an unexpected tag (parent <{1}> starts at {2}).{3}",                                       errorNodeName(name, _item, parentItem),				       parent.getName(), parentLocation,                                       errorMessageDetail(_item, parentItem, parent.getName(), name)));      }      _item = newItem;      _itemStack.add(newItem);      Item parentItem = newItem;      int len = attrs.getLength();      for (int i = 0; i < len; i++) {        String attrUri = attrs.getURI(i);        String attrQName = attrs.getQName(i);        String value = attrs.getValue(i);                if (_isLogFinest)          log.finest("attribute: " + attrQName + "=\"" + value + "\"");                name = new QName(attrQName, attrUri);        if (attrQName.startsWith("xml:")) {        }        else if (! _item.allowAttribute(name, value)) {          throw new RelaxException(L.l("{0}=\"{1}\" is an unexpected attribute in <{2}>.{3}",                                       attrQName, value, qName,				       attributeMessageDetail(_item,							      parentItem,							      qName, null)));	}        else          _item = _item.setAttribute(name, value);        if (_item == null)          _item = EmptyItem.create();      }      newItem = _item.attributeEnd();      if (newItem == null) {        throw new RelaxException(L.l("<{0}> expects more attributes.{1}",                                     qName, 				     attributeMessageDetail(_item,							    parentItem,							    qName, null)));      }            _item = newItem;    } catch (Exception e) {      error(e);    }  }  private Item getStartElement(Item item, QName name)    throws RelaxException  {    _startKey.init(item, name);    Item newItem = null;//_programCache.get(_startKey);    if (newItem != null) {      return newItem;    }        newItem = _item.startElement(name);    /*    if (newItem != null)      _programCache.put(new StartKey(item, name), newItem);    */    return newItem;  }    public void characters(char ch[], int start, int length)    throws SAXException  {    _hasText = true;    _text.append(ch, start, length);  }  public void sendText()    throws SAXException  {    if (! _hasText)      return;    _hasText = false;    try {      Item newItem = _item.text(_text);            if (newItem == null) {        String string = _text.toString();	Item parentItem = _itemStack.get(_itemStack.size() - 1);	        throw new RelaxException(L.l("The following text is not allowed in this context.\n{0}\n{1}", string,				     errorMessageDetail(_item, parentItem,							_name.getName(), null)));      }      _text.clear();      _item = newItem;                      } catch (Exception e) {      _text.clear();      error(e);    }  }  /**   * Called when an element ends.   */  public void endElement(String uri, String localName, String qName)    throws SAXException  {    if (_hasText)      sendText();        if (! _isValid)      return;    if (_isLogFinest)      log.finest("element end: " + qName);    QName name = _name;    QName parent = _nameStack.remove(_nameStack.size() - 1);    _name = parent;    Item parentItem = _itemStack.remove(_itemStack.size() - 1);    String eltOpen = _eltLocation;    _eltLocation = _eltLocationStack.remove(_eltLocationStack.size() - 1);        try {      Item nextItem = getEndElement(_item);            if (nextItem == null)        throw new RelaxException(L.l("<{0}> closed while expecting more elements (open at {1}).{2}",                                     qName, eltOpen,				     requiredMessageDetail(_item, parentItem,							qName, null)));            _item = nextItem;    } catch (Exception e) {      error(e);    }  }  private Item getEndElement(Item item)    throws RelaxException  {    _endElementKey.init(item);    Item newItem = null;//_programCache.get(_endElementKey);    if (newItem != null) {      return newItem;    }        newItem = _item.endElement();    /*    if (newItem != null)      _programCache.put(new EndElementKey(item), newItem);    */    return newItem;  }  /**   * Called for errors.   */  private void error(SAXException e)    throws SAXException  {    _isValid = false;    _verifier.error(new SAXParseException(e.getMessage(), _locator));  }  /**   * Called for errors.   */  private void error(Exception e)    throws SAXException  {    if (e instanceof RuntimeException)      throw (RuntimeException) e;    else if (e instanceof SAXException)      error((SAXException) e);    else      error(new SAXException(e.getMessage(), e));  }  /**   * Returns a string containing the allowed values.   */  private String errorNodeName(QName name, Item item, Item parentItem)  {    Item currentItem = item;        if (currentItem == null)      currentItem = parentItem;    if (currentItem == null)      return name.toString();    HashSet<QName> values = new LinkedHashSet<QName>();

⌨️ 快捷键说明

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