本文整理匯總了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();
}