Java Code Examples for javax.xml.stream.XMLStreamReader#getLocation()

The following examples show how to use javax.xml.stream.XMLStreamReader#getLocation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DocumentStaxUtils.java    From gate-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: DocumentStaxUtils.java    From gate-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * A copy of the nextTag algorithm from the XMLStreamReader javadocs,
 * but which also skips over DTD events as well as whitespace,
 * comments and PIs.
 * 
 * @param xsr the reader to advance
 * @return {@link XMLStreamConstants#START_ELEMENT} or
 *         {@link XMLStreamConstants#END_ELEMENT} for the next tag.
 * @throws XMLStreamException
 */
private static int nextTagSkipDTD(XMLStreamReader xsr)
        throws XMLStreamException {
  int eventType = xsr.next();
  while((eventType == XMLStreamConstants.CHARACTERS && xsr.isWhiteSpace())
          || (eventType == XMLStreamConstants.CDATA && xsr.isWhiteSpace())
          || eventType == XMLStreamConstants.SPACE
          || eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
          || eventType == XMLStreamConstants.COMMENT
          || eventType == XMLStreamConstants.DTD) {
    eventType = xsr.next();
  }
  if(eventType != XMLStreamConstants.START_ELEMENT
          && eventType != XMLStreamConstants.END_ELEMENT) {
    throw new XMLStreamException("expected start or end tag", xsr
            .getLocation());
  }
  return eventType;
}
 
Example 3
Source File: AccessRuleXml.java    From outbackcdx with Apache License 2.0 5 votes vote down vote up
private static Date parseDate(XMLStreamReader xml) throws XMLStreamException {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss.S");
    try {
        return format.parse(xml.getElementText());
    } catch (ParseException e) {
        throw new XMLStreamException("unable to parse date", xml.getLocation(),  e);
    }
}
 
Example 4
Source File: XMLCatalogSchemaManager.java    From tajo with Apache License 2.0 5 votes vote down vote up
protected StoreObject loadCatalogStore(XMLStreamReader xmlReader) throws XMLStreamException {
  try {
    JAXBElement<StoreObject> elem = unmarshaller.unmarshal(xmlReader, StoreObject.class);
    return elem.getValue();
  } catch (JAXBException e) {
    throw new XMLStreamException(e.getMessage(), xmlReader.getLocation(), e);
  }
}
 
Example 5
Source File: WSDLParserExtensionFacade.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private Locator getLocator(XMLStreamReader reader) {
    Location location = reader.getLocation();
        LocatorImpl loc = new LocatorImpl();
        loc.setSystemId(location.getSystemId());
        loc.setLineNumber(location.getLineNumber());
    return loc;
}
 
Example 6
Source File: LocatableWebServiceException.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static Locator toLocation(XMLStreamReader xsr) {
    LocatorImpl loc = new LocatorImpl();
    Location in = xsr.getLocation();
    loc.setSystemId(in.getSystemId());
    loc.setPublicId(in.getPublicId());
    loc.setLineNumber(in.getLineNumber());
    loc.setColumnNumber(in.getColumnNumber());
    return loc;
}
 
Example 7
Source File: ParsingUtils.java    From galleon with Apache License 2.0 5 votes vote down vote up
public static String getNextElement(XMLStreamReader reader, String name, Map<String, String> attributes, boolean getElementText) throws XMLStreamException {
    if (!reader.hasNext()) {
        throw new XMLStreamException("Expected more elements", reader.getLocation());
    }
    int type = reader.next();
    while (reader.hasNext() && type != START_ELEMENT) {
        type = reader.next();
    }
    if (reader.getEventType() != START_ELEMENT) {
        throw new XMLStreamException("No <" + name + "> found");
    }
    if (!reader.getLocalName().equals("" + name + "")) {
        throw new XMLStreamException("<" + name + "> expected", reader.getLocation());
    }

    if (attributes != null) {
        for (int i = 0 ; i < reader.getAttributeCount() ; i++) {
            String attr = reader.getAttributeLocalName(i);
            if (!attributes.containsKey(attr)) {
                throw new XMLStreamException("Unexpected attribute " + attr, reader.getLocation());
            }
            attributes.put(attr, reader.getAttributeValue(i));
        }
    }

    return getElementText ? reader.getElementText() : null;
}
 
Example 8
Source File: WSDLParserExtensionFacade.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private Locator getLocator(XMLStreamReader reader) {
    Location location = reader.getLocation();
        LocatorImpl loc = new LocatorImpl();
        loc.setSystemId(location.getSystemId());
        loc.setLineNumber(location.getLineNumber());
    return loc;
}
 
Example 9
Source File: LocatableWebServiceException.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static Locator toLocation(XMLStreamReader xsr) {
    LocatorImpl loc = new LocatorImpl();
    Location in = xsr.getLocation();
    loc.setSystemId(in.getSystemId());
    loc.setPublicId(in.getPublicId());
    loc.setLineNumber(in.getLineNumber());
    loc.setColumnNumber(in.getColumnNumber());
    return loc;
}
 
Example 10
Source File: AbstractObjectImpl.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
AbstractObjectImpl(XMLStreamReader xsr) {
    Location loc = xsr.getLocation();
    this.lineNumber = loc.getLineNumber();
    this.systemId = loc.getSystemId();
}
 
Example 11
Source File: ParsingUtils.java    From galleon with Apache License 2.0 4 votes vote down vote up
public static XMLStreamException unexpectedContent(final XMLStreamReader reader) {
    final String kind;
    switch (reader.getEventType()) {
        case XMLStreamConstants.ATTRIBUTE:
            kind = "attribute";
            break;
        case XMLStreamConstants.CDATA:
            kind = "cdata";
            break;
        case XMLStreamConstants.CHARACTERS:
            kind = "characters";
            break;
        case XMLStreamConstants.COMMENT:
            kind = "comment";
            break;
        case XMLStreamConstants.DTD:
            kind = "dtd";
            break;
        case XMLStreamConstants.END_DOCUMENT:
            kind = "document end";
            break;
        case XMLStreamConstants.END_ELEMENT:
            kind = "element end";
            break;
        case XMLStreamConstants.ENTITY_DECLARATION:
            kind = "entity declaration";
            break;
        case XMLStreamConstants.ENTITY_REFERENCE:
            kind = "entity ref";
            break;
        case XMLStreamConstants.NAMESPACE:
            kind = "namespace";
            break;
        case XMLStreamConstants.NOTATION_DECLARATION:
            kind = "notation declaration";
            break;
        case XMLStreamConstants.PROCESSING_INSTRUCTION:
            kind = "processing instruction";
            break;
        case XMLStreamConstants.SPACE:
            kind = "whitespace";
            break;
        case XMLStreamConstants.START_DOCUMENT:
            kind = "document start";
            break;
        case XMLStreamConstants.START_ELEMENT:
            kind = "element start";
            break;
        default:
            kind = "unknown";
            break;
    }

    return new XMLStreamException("unexpected content: " + kind + (reader.hasName() ? reader.getName() : null) +
            (reader.hasText() ? reader.getText() : null), reader.getLocation());
}
 
Example 12
Source File: VaultConfig.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
private static void unexpectedVaultAttribute(String attribute, XMLStreamReader reader) throws XMLStreamException {
    throw new XMLStreamException("Attribute '" + attribute +
            "' is unknown for element '" +
            VAULT_OPTION + "' at " + reader.getLocation());

}
 
Example 13
Source File: DmnXMLUtil.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
public static void addXMLLocation(DmnElement element, XMLStreamReader xtr) {
    Location location = xtr.getLocation();
    //element.setXmlRowNumber(location.getLineNumber());
    //element.setXmlColumnNumber(location.getColumnNumber());
}
 
Example 14
Source File: AbstractObjectImpl.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
AbstractObjectImpl(XMLStreamReader xsr) {
    Location loc = xsr.getLocation();
    this.lineNumber = loc.getLineNumber();
    this.systemId = loc.getSystemId();
}
 
Example 15
Source File: AbstractObjectImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
AbstractObjectImpl(XMLStreamReader xsr) {
    Location loc = xsr.getLocation();
    this.lineNumber = loc.getLineNumber();
    this.systemId = loc.getSystemId();
}
 
Example 16
Source File: AbstractObjectImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
AbstractObjectImpl(XMLStreamReader xsr) {
    Location loc = xsr.getLocation();
    this.lineNumber = loc.getLineNumber();
    this.systemId = loc.getSystemId();
}
 
Example 17
Source File: StaEDISchemaFactory.java    From staedi with Apache License 2.0 4 votes vote down vote up
static StaEDISchemaReadException schemaException(String message, XMLStreamReader reader, Throwable cause) {
    return new StaEDISchemaReadException(message, reader.getLocation(), cause);
}
 
Example 18
Source File: AbstractObjectImpl.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
AbstractObjectImpl(XMLStreamReader xsr) {
    Location loc = xsr.getLocation();
    this.lineNumber = loc.getLineNumber();
    this.systemId = loc.getSystemId();
}
 
Example 19
Source File: DocumentStaxUtils.java    From gate-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * 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();
}
 
Example 20
Source File: AttributeParser.java    From wildfly-core with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * Creates and returns a {@link org.jboss.dmr.ModelNode} using the given {@code value} after first validating the node
 * against {@link org.jboss.as.controller.AttributeDefinition#getValidator() this object's validator}.
 * <p>
 * If {@code value} is {@code null} an {@link org.jboss.dmr.ModelType#UNDEFINED undefined} node will be returned.
 * </p>
 *
 * @param value  the value. Will be {@link String#trim() trimmed} before use if not {@code null}.
 * @param reader {@link XMLStreamReader} from which the {@link XMLStreamReader#getLocation() location} from which
 *               the attribute value was read can be obtained and used in any {@code XMLStreamException}, in case
 *               the given value is invalid.
 * @return {@code ModelNode} representing the parsed value
 * @throws javax.xml.stream.XMLStreamException if {@code value} is not valid
 * @see #parseAndSetParameter(org.jboss.as.controller.AttributeDefinition, String, ModelNode, XMLStreamReader)
 */
public ModelNode parse(final AttributeDefinition attribute, final String value, final XMLStreamReader reader) throws XMLStreamException {
    try {
        return parse(attribute, value);
    } catch (OperationFailedException e) {
        throw new XMLStreamException(e.getFailureDescription().toString(), reader.getLocation());
    }
}