xmlreader.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 735 行 · 第 1/2 页
JAVA
735 行
/*
* 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 SoftwareFoundation, Inc.
* 59 Temple Place, Suite 330
* Boston, MA 02111-1307 USA
*
* @author Scott Ferguson
*/
package com.caucho.quercus.lib.xml;
import com.caucho.quercus.annotation.Optional;
import com.caucho.quercus.env.BooleanValue;
import com.caucho.quercus.env.Env;
import com.caucho.quercus.env.LongValue;
import com.caucho.quercus.env.NullValue;
import com.caucho.quercus.env.StringValue;
import com.caucho.quercus.env.Value;
import com.caucho.util.L10N;
import com.caucho.vfs.Path;
import javax.xml.stream.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
public class XmlReader
{
private static final Logger log
= Logger.getLogger(XmlReader.class.getName());
private static final L10N L = new L10N(XmlReader.class);
private int _depth;
private int _lastNodeType;
private int _currentNodeType;
private boolean _hasAttribute;
private XMLStreamReader _streamReader;
private static final HashMap<Integer, Integer> _constConvertMap
= new HashMap<Integer, Integer>();
private HashMap<String, Integer> _startElements;
public static final int NONE = 0;
public static final int ELEMENT = 1;
public static final int ATTRIBUTE = 2;
public static final int TEXT = 3;
public static final int CDATA = 4;
public static final int ENTITY_REF = 5;
public static final int ENTITY = 6;
public static final int PI = 7;
public static final int COMMENT = 8;
public static final int DOC = 9;
public static final int DOC_TYPE = 10;
public static final int DOC_FRAGMENT = 11;
public static final int NOTATION = 12;
public static final int WHITESPACE = 13;
public static final int SIGNIFICANT_WHITESPACE = 14;
public static final int END_ELEMENT = 15;
public static final int END_ENTITY = 16;
public static final int XML_DECLARATION = 17;
public static final int LOADDTD = 1;
public static final int DEFAULTATTRS = 2;
public static final int VALIDATE = 3;
public static final int SUBST_ENTITIES = 4;
/**
* Default constructor.
*
* XXX: Not completely sure what the passed in string(s) does.
*
* @param string not used
*/
public XmlReader(@Optional String[] string) {
_depth = 0;
_lastNodeType = -1;
_currentNodeType = XMLStreamConstants.START_DOCUMENT;
_streamReader = null;
_startElements = new HashMap<String, Integer>();
_hasAttribute = false;
}
/**
* Determines if the stream has been opened and produces a warning if not.
*
* @param env
* @param operation name of the operation being performed (i.e. read, etc.)
* @return true if the stream is open, false otherwise
*/
private boolean streamIsOpen(Env env, String operation) {
if (! streamIsOpen()) {
env.warning(L.l("Load Data before trying to " + operation));
return false;
}
return true;
}
/**
* Determines if the stream has been opened.
*
* @return true if the stream is open, false otherwise
*/
private boolean streamIsOpen() {
return _streamReader != null;
}
/**
* Returns the number of attributes of the current element.
*
* @return the count if it exists, otherwise null
*/
public Value getAttributeCount() {
if (! streamIsOpen())
return NullValue.NULL;
try {
if (_currentNodeType == XMLStreamConstants.CHARACTERS)
return LongValue.create(0);
return LongValue.create(_streamReader.getAttributeCount());
}
catch (IllegalStateException ex) {
log.log(Level.WARNING, ex.toString(), ex);
return NullValue.NULL;
}
}
/**
* Returns the base uniform resource locator of the current element.
*
* @return the URI, otherwise null
*/
public Value getBaseURI() {
if (! streamIsOpen())
return NullValue.NULL;
return StringValue.create(_streamReader.getLocation().getSystemId());
}
/**
* Returns the depth of the current element.
*
* @return the depth if it exists, otherwise null
*/
public Value getDepth() {
if (! streamIsOpen())
return NullValue.NULL;
return LongValue.create(_depth);
}
/**
* Determines whether this element has attributes.
*
* @return true if this element has attributes, false if not, otherwise null
*/
public Value getHasAttributes() {
if (! streamIsOpen())
return NullValue.NULL;
try {
if (_currentNodeType == XMLStreamConstants.CHARACTERS)
return BooleanValue.FALSE;
return BooleanValue.create(_hasAttribute || _streamReader.getAttributeCount() > 0);
}
catch (IllegalStateException ex) {
log.log(Level.WARNING, ex.toString(), ex);
return NullValue.NULL;
}
}
/**
* Determines whether this element has content.
*
* @return true if this element has content, false if not, otherwise null
*/
public Value getHasValue() {
if (! streamIsOpen())
return NullValue.NULL;
return BooleanValue.create(_streamReader.hasText());
}
/**
* Determines whether this element is default.
*
* @return true if this element is default, false if not, otherwise null
*/
public Value getIsDefault() {
if (! streamIsOpen())
return NullValue.NULL;
// XXX: StreamReaderImpl.isAttributeSpecified() only checks for
// attribute existence. This should be tested against the atttribute list
// but couldn't find anything like that in StreamReader.
return BooleanValue.FALSE;
}
/**
* Determines whether this element is empty.
*
* @return true if this element is empty, false if not, otherwise null
*/
public Value getIsEmptyElement() {
if (! streamIsOpen())
return NullValue.NULL;
// The only case I found for isEmptyElement was for something
// like <element/>. Even something like <element></element> was
// not considered empty.
if (_currentNodeType == XMLStreamConstants.START_ELEMENT
&& _streamReader.isEndElement())
return BooleanValue.TRUE;
return BooleanValue.FALSE;
}
/**
* Determines whether this element has attributes.
*
* @return true if this element has attributes, false if not, otherwise null
*/
public Value getLocalName() {
if (! streamIsOpen())
return NullValue.NULL;
String name = "";
if (_currentNodeType == XMLStreamConstants.CHARACTERS)
name = "#text";
else if (_currentNodeType == XMLStreamConstants.COMMENT)
name = "#comment";
else
name = _streamReader.getLocalName();
return StringValue.create(name);
}
/**
* Returns the name of the current element.
*
* @return the name, otherwise null
*/
public Value getName(Env env) {
if (! streamIsOpen())
return NullValue.NULL;
try {
String name = "";
// XXX: Next line should be "String prefix = _streamReader.getPrefix();"
// but there was a NullPointerException for XMLStreamReaderImpl._name.
// php/4618
String prefix = _streamReader.getPrefix();
if (_currentNodeType == XMLStreamConstants.CHARACTERS)
name = "#text";
else if (_currentNodeType == XMLStreamConstants.COMMENT)
name = "#comment";
else {
if (prefix == null)
name = _streamReader.getName().toString();
else
name = prefix + ":" + _streamReader.getLocalName().toString();
}
return StringValue.create(name);
}
catch (IllegalStateException ex) {
log.log(Level.WARNING, ex.toString(), ex);
return NullValue.NULL;
}
}
/**
* Returns the namespace uniform resource locator of the current element.
*
* @return the namespace URI, otherwise null
*/
public Value getNamespaceURI() {
if (! streamIsOpen())
return NullValue.NULL;
return StringValue.create(_streamReader.getNamespaceURI());
}
/**
* Returns the node type of the current element.
*
* @return the node type, otherwise null
*/
public Value getNodeType() {
if (! streamIsOpen())
return NullValue.NULL;
/*
Integer convertedInteger = _constConvertMap.get(_nextType);
int convertedInt = convertedInteger.intValue();
return LongValue.create(convertedInt);*/
int convertedInt = SIGNIFICANT_WHITESPACE;
if (! _streamReader.isWhiteSpace()) {
Integer convertedInteger =
_constConvertMap.get(_streamReader.getEventType());
convertedInt = convertedInteger.intValue();
}
return LongValue.create(convertedInt);
}
/**
* Returns the prefix of the current element.
*
* @return the prefix, otherwise null
*/
public Value getPrefix() {
if (! streamIsOpen())
return NullValue.NULL;
return StringValue.create(_streamReader.getPrefix());
}
/**
* Returns the value of the current element.
*
* @return the value, otherwise null
*/
public Value getValue() {
if (! streamIsOpen())
return NullValue.NULL;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?