本文整理汇总了Java中javax.xml.stream.XMLStreamReader.require方法的典型用法代码示例。如果您正苦于以下问题:Java XMLStreamReader.require方法的具体用法?Java XMLStreamReader.require怎么用?Java XMLStreamReader.require使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.stream.XMLStreamReader
的用法示例。
在下文中一共展示了XMLStreamReader.require方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readFeatureMap
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
/**
* Processes a GateDocumentFeatures or Annotation element to build a
* feature map. The element is expected to contain Feature children,
* each with a Name and Value. The reader will be returned positioned
* on the closing GateDocumentFeatures or Annotation tag.
*
* @throws XMLStreamException
*/
public static FeatureMap readFeatureMap(XMLStreamReader xsr)
throws XMLStreamException {
FeatureMap fm = Factory.newFeatureMap();
while(xsr.nextTag() == XMLStreamConstants.START_ELEMENT) {
xsr.require(XMLStreamConstants.START_ELEMENT, null, "Feature");
Object featureName = null;
Object featureValue = null;
while(xsr.nextTag() == XMLStreamConstants.START_ELEMENT) {
if("Name".equals(xsr.getLocalName())) {
featureName = readFeatureNameOrValue(xsr);
}
else if("Value".equals(xsr.getLocalName())) {
featureValue = readFeatureNameOrValue(xsr);
}
else {
throw new XMLStreamException("Feature element should contain "
+ "only Name and Value children", xsr.getLocation());
}
}
fm.put(featureName, featureValue);
}
return fm;
}
示例2: readXcesFeatureMap
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
/**
* Processes a struct element to build a feature map. The element is
* expected to contain feat children, each with name and value
* attributes. The reader will be returned positioned on the closing
* struct tag.
*
* @throws XMLStreamException
*/
public static FeatureMap readXcesFeatureMap(XMLStreamReader xsr)
throws XMLStreamException {
FeatureMap fm = Factory.newFeatureMap();
while(xsr.nextTag() == XMLStreamConstants.START_ELEMENT) {
xsr.require(XMLStreamConstants.START_ELEMENT, XCES_NAMESPACE, "feat");
String featureName = xsr.getAttributeValue(null, "name");
Object featureValue = xsr.getAttributeValue(null, "value");
fm.put(featureName, featureValue);
// read the (possibly virtual) closing tag of the feat element
xsr.nextTag();
xsr.require(XMLStreamConstants.END_ELEMENT, XCES_NAMESPACE, "feat");
}
return fm;
}
示例3: testInconsistentGetPrefixBehaviorWhenNoPrefix
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
@Test
public void testInconsistentGetPrefixBehaviorWhenNoPrefix() throws Exception {
String xml = "<root><child xmlns='foo'/><anotherchild/></root>";
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader r = factory.createXMLStreamReader(new StringReader(xml));
r.require(XMLStreamReader.START_DOCUMENT, null, null);
r.next();
r.require(XMLStreamReader.START_ELEMENT, null, "root");
Assert.assertEquals(r.getPrefix(), "", "prefix should be empty string");
r.next();
r.require(XMLStreamReader.START_ELEMENT, null, "child");
r.next();
r.next();
r.require(XMLStreamReader.START_ELEMENT, null, "anotherchild");
Assert.assertEquals(r.getPrefix(), "", "prefix should be empty string");
}
示例4: readRelationSet
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
public static void readRelationSet(XMLStreamReader xsr,
RelationSet relations, Set<Integer> allAnnotIds)
throws XMLStreamException {
while(xsr.nextTag() == XMLStreamConstants.START_ELEMENT) {
xsr.require(XMLStreamConstants.START_ELEMENT, null, "Relation");
String type = xsr.getAttributeValue(null, "Type");
String idString = xsr.getAttributeValue(null, "Id");
String memberString = xsr.getAttributeValue(null, "Members");
if(memberString == null)
throw new XMLStreamException("A relation must have members");
if (type == null)
throw new XMLStreamException("A relation must have a type");
if (idString == null)
throw new XMLStreamException("A relation must have an id");
String[] memberStrings = memberString.split(";");
int[] members = new int[memberStrings.length];
for(int i = 0; i < members.length; ++i) {
members[i] = Integer.parseInt(memberStrings[i]);
}
xsr.nextTag();
xsr.require(XMLStreamConstants.START_ELEMENT, null, "UserData");
// get the string representation of the user data
StringBuilder stringRep = new StringBuilder(1024);
int eventType;
while((eventType = xsr.next()) != XMLStreamConstants.END_ELEMENT) {
switch(eventType) {
case XMLStreamConstants.CHARACTERS:
case XMLStreamConstants.CDATA:
stringRep.append(xsr.getTextCharacters(), xsr.getTextStart(),
xsr.getTextLength());
break;
case XMLStreamConstants.START_ELEMENT:
throw new XMLStreamException("Elements not allowed within "
+ "user data.", xsr.getLocation());
default:
// do nothing - ignore comments, PIs, etc.
}
}
xsr.require(XMLStreamConstants.END_ELEMENT, null, "UserData");
FeatureMap features = readFeatureMap(xsr);
Relation r = new SimpleRelation(Integer.parseInt(idString), type, members);
r.setFeatures(features);
if(stringRep.length() > 0) {
ObjectWrapper wrapper = new ObjectWrapper(stringRep.toString());
r.setUserData(wrapper.getValue());
}
relations.add(r);
}
}
示例5: readTextWithNodes
import javax.xml.stream.XMLStreamReader; //导入方法依赖的package包/类
/**
* Processes the TextWithNodes element from this XMLStreamReader,
* returning the text content of the document. The supplied map is
* updated with the offset of each Node element encountered. The
* reader must be positioned on the starting TextWithNodes tag and
* will be returned positioned on the corresponding closing tag.
*
* @param xsr
* @param nodeIdToOffsetMap
* @return the text content of the document
*/
public static String readTextWithNodes(XMLStreamReader xsr,
Map<Integer, Long> nodeIdToOffsetMap) throws XMLStreamException {
StringBuffer textBuf = new StringBuffer(20480);
int eventType;
while((eventType = xsr.next()) != XMLStreamConstants.END_ELEMENT) {
switch(eventType) {
case XMLStreamConstants.CHARACTERS:
case XMLStreamConstants.CDATA:
textBuf.append(xsr.getTextCharacters(), xsr.getTextStart(), xsr
.getTextLength());
break;
case XMLStreamConstants.START_ELEMENT:
// only Node elements allowed
xsr.require(XMLStreamConstants.START_ELEMENT, null, "Node");
String idString = xsr.getAttributeValue(null, "id");
if(idString == null) {
throw new XMLStreamException("Node element has no id", xsr
.getLocation());
}
try {
Integer id = Integer.valueOf(idString);
Long offset = Long.valueOf(textBuf.length());
nodeIdToOffsetMap.put(id, offset);
}
catch(NumberFormatException nfe) {
throw new XMLStreamException("Node element must have "
+ "integer id", xsr.getLocation());
}
// Node element must be empty
if(xsr.next() != XMLStreamConstants.END_ELEMENT) {
throw new XMLStreamException("Node element within TextWithNodes "
+ "must be empty.", xsr.getLocation());
}
break;
default:
// do nothing - ignore comments, PIs...
}
}
return textBuf.toString();
}