Exemple #1
0
 /**
  * Skips to the next element if the reader points the required element. Post: reader will be at
  * {@link XMLStreamConstants#START_ELEMENT} of the next element.
  *
  * @param reader
  * @param elementName
  * @throws XMLStreamException
  */
 public static void skipRequiredElement(XMLStreamReader reader, QName elementName)
     throws XMLStreamException {
   if (reader.isStartElement() && reader.getName().equals(elementName)) {
     nextElement(reader);
     if (reader.isEndElement() && reader.getName().equals(elementName)) {
       nextElement(reader);
     }
     return;
   }
   throw new XMLParsingException(
       reader, "Required element " + elementName + " was not found at given stream position.");
 }
Exemple #2
0
 /**
  * Post: reader will be unchanged or on success at {@link XMLStreamConstants #END_ELEMENT} of the
  * matching element or at {@link XMLStreamConstants #START_ELEMENT} of the next element if
  * requested.
  *
  * @param reader pointing to the current element.
  * @param elementName of the current element.
  * @param defaultValue to return if the current name was not the one given or the value could not
  *     be parsed as a integer.
  * @param nextElemOnSucces if true the reader will be moved to the next tag if the retrieval was
  *     successful.
  * @return the text of the current element (which should have element name) parsed as a integer.
  * @throws XMLStreamException from {@link XMLStreamReader#getElementText()}.
  */
 public static int getElementTextAsInteger(
     XMLStreamReader reader, QName elementName, int defaultValue, boolean nextElemOnSucces)
     throws XMLStreamException {
   int value = defaultValue;
   if (elementName.equals(reader.getName()) && reader.isStartElement()) {
     String s = reader.getElementText();
     if (s != null) {
       try {
         value = Integer.parseInt(s);
         if (nextElemOnSucces) {
           nextElement(reader);
         }
       } catch (NumberFormatException nfe) {
         LOG.debug(
             reader.getLocation()
                 + ") Value "
                 + s
                 + " in element: "
                 + elementName
                 + " was not a parsable integer, returning integer value: "
                 + defaultValue);
       }
     }
   }
   return value;
 }
Exemple #3
0
 /**
  * Forwards the given {@link XMLStreamReader} to the specified element or to the end of the
  * enclosing element/document if there is no such element.
  *
  * @param reader reader to forward, must not be <code>null</code>
  * @param elementName element to forward to, must not be <code>null</code>
  * @throws XMLStreamException
  */
 public static boolean skipToElementOnSameLevel(XMLStreamReader reader, QName elementName)
     throws XMLStreamException {
   while (reader.isStartElement() && !elementName.equals(reader.getName())) {
     skipElement(reader);
     nextElement(reader);
   }
   return reader.isStartElement() && elementName.equals(reader.getName());
 }
Exemple #4
0
 /**
  * Get the text of the element or if the reader does not match the given elementName the default
  * text will be returned. Post: reader will be unchanged or at {@link
  * XMLStreamConstants#END_ELEMENT} of the matching element or at {@link XMLStreamConstants
  * #START_ELEMENT} of the next element if requested.
  *
  * @param reader
  * @param elemName
  * @param defaultText
  * @param nextElemOnSucces if true the reader will be moved to the next tag if the retrieval was
  *     successful.
  * @return the text of the element or if the reader does not match the given elementName the
  *     default text will be returned.
  * @throws XMLStreamException
  */
 public static String getText(
     XMLStreamReader reader, QName elemName, String defaultText, boolean nextElemOnSucces)
     throws XMLStreamException {
   String value = defaultText;
   if (reader.isStartElement() && reader.getName().equals(elemName)) {
     value = reader.getElementText();
     if (nextElemOnSucces) {
       nextElement(reader);
     }
   }
   return value;
 }
Exemple #5
0
 /**
  * Post: reader will be unchanged or at {@link XMLStreamConstants#END_ELEMENT} of the matching
  * element or at {@link XMLStreamConstants #START_ELEMENT} of the next element if requested.
  *
  * @param reader
  * @param elementName
  * @param defaultValue
  * @param nextElemOnSucces if true the reader will be moved to the next tag if the retrieval was
  *     successful.
  * @return the element text as boolean
  * @throws XMLStreamException
  */
 public static boolean getElementTextAsBoolean(
     XMLStreamReader reader, QName elementName, boolean defaultValue, boolean nextElemOnSucces)
     throws XMLStreamException {
   boolean res = defaultValue;
   if (elementName.equals(reader.getName())) {
     res = parseAsBoolean(reader, reader.getElementText());
     if (nextElemOnSucces) {
       nextElement(reader);
     }
   }
   return res;
 }
Exemple #6
0
 /**
  * Get the text of the given element which must be an element with given name. Post: reader will
  * be at {@link XMLStreamConstants#END_ELEMENT} of matching element or at {@link
  * XMLStreamConstants #START_ELEMENT} of the next element if requested.
  *
  * @param reader
  * @param elementName
  * @param nextElemOnSucces if true the reader will be moved to the next tag if the retrieval was
  *     successful.
  * @return the text of the current 'required' element.
  * @throws XMLStreamException
  */
 public static String getRequiredText(
     XMLStreamReader reader, QName elementName, boolean nextElemOnSucces)
     throws XMLStreamException {
   if (reader.isStartElement() && reader.getName().equals(elementName)) {
     String val = reader.getElementText();
     if (nextElemOnSucces) {
       nextElement(reader);
     }
     return val;
   }
   throw new XMLParsingException(
       reader, "Required element " + elementName + " was not found at given stream position.");
 }
Exemple #7
0
 /**
  * The reader must be on a StartElement, any attributes will be skipped. Post: reader will be
  * unchanged or at {@link XMLStreamConstants#START_ELEMENT } of the first element after the last
  * matching element
  *
  * @param reader
  * @param name of the elements
  * @return an array of strings, denoting the elements with the given name.
  * @throws XMLStreamException
  */
 public static String[] getSimpleUnboundedAsStrings(XMLStreamReader reader, QName name)
     throws XMLStreamException {
   List<String> values = new LinkedList<String>();
   if (reader.isStartElement()) {
     while (name.equals(reader.getName())) {
       String text = reader.getElementText();
       if (text != null) {
         values.add(text);
       }
       // move beyond the end tag.
       nextElement(reader);
     }
   }
   return values.toArray(new String[values.size()]);
 }
Exemple #8
0
  /**
   * Move the reader to the first element which matches one of the given name(s). The reader will be
   * positioned on the {@link XMLStreamConstants#START_ELEMENT} event or after the {@link
   * XMLStreamConstants#END_DOCUMENT} which ever comes first.
   *
   * @param reader to position
   * @param alowedElements name of the element to move forward to.
   * @return true if the reader is on the given element, false otherwise.
   * @throws XMLStreamException
   */
  public static boolean moveReaderToFirstMatch(
      XMLStreamReader reader, Collection<QName> alowedElements) throws XMLStreamException {
    boolean hasMoreElements = true;
    do {
      if (reader.isStartElement() && alowedElements.contains(reader.getName())) {
        return true;
      }
      try {
        if (LOG.isDebugEnabled()) {
          if (reader.isStartElement() || reader.isEndElement()) {
            LOG.debug("Skipping element: " + reader.getName());
          }
        }
        nextElement(reader);
      } catch (NoSuchElementException e) {
        // end of file
        hasMoreElements = false;
      }

    } while (hasMoreElements);
    return false;
  }
 /**
  * Returns the current state of the execution.
  *
  * @return state of the execution, or <code>null</code> if the execution has not been started yet
  * @throws OWSExceptionReport if the server replied with an exception
  * @throws IOException if a communication/network problem occured
  * @throws XMLStreamException
  */
 public ExecutionState getState() throws OWSExceptionReport, IOException, XMLStreamException {
   if (lastResponse == null) {
     return null;
   }
   if (lastResponse.getStatus().getState() != ExecutionState.SUCCEEDED
       && lastResponse.getStatus().getState() != ExecutionState.FAILED) {
     URL statusLocation = lastResponse.getStatusLocation();
     if (statusLocation == null) {
       throw new RuntimeException("Cannot update status. No statusLocation provided.");
     }
     LOG.debug("Polling response document from status location: " + statusLocation);
     XMLInputFactory inFactory = XMLInputFactory.newInstance();
     InputStream is = statusLocation.openStream();
     XMLStreamReader xmlReader = inFactory.createXMLStreamReader(is);
     XMLStreamUtils.nextElement(xmlReader);
     if (OWSExceptionReader.isExceptionReport(xmlReader.getName())) {
       throw OWSExceptionReader.parseExceptionReport(xmlReader);
     }
     ExecuteResponse100Reader reader = new ExecuteResponse100Reader(xmlReader);
     lastResponse = reader.parse100();
   }
   return lastResponse.getStatus().getState();
 }
Exemple #10
0
 /**
  * Skips the current event, if it is a START_DOCUMENT event, so that the <code>XMLStreamReader
  * </code> cursor points at the first <code>START_ELEMENT</code> event. If the current event is
  * not a START_DOCUMENT event, nothing happens.
  *
  * @param xmlStream may not be <code>null</code>
  * @throws XMLStreamException
  */
 public static void skipStartDocument(XMLStreamReader xmlStream) throws XMLStreamException {
   if (xmlStream.getEventType() == START_DOCUMENT) {
     nextElement(xmlStream);
   }
 }
Exemple #11
0
  private ExecutionResponse sendExecute(boolean async)
      throws OWSExceptionReport, XMLStreamException, IOException {

    responseFormat = new ResponseFormat(false, async, false, async, outputDefs);

    // TODO what if server only supports Get?
    URL url = client.getExecuteURL(true);

    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);
    conn.setUseCaches(false);
    // TODO does this need configurability?
    conn.setRequestProperty("Content-Type", "application/xml");

    XMLOutputFactory outFactory = XMLOutputFactory.newInstance();

    OutputStream os = conn.getOutputStream();
    XMLInputFactory inFactory = XMLInputFactory.newInstance();

    if (LOG.isDebugEnabled()) {
      File logFile = File.createTempFile("wpsclient", "request.xml");
      XMLStreamWriter logWriter =
          outFactory.createXMLStreamWriter(new FileOutputStream(logFile), "UTF-8");
      ExecuteRequest100Writer executer = new ExecuteRequest100Writer(logWriter);
      executer.write100(process.getId(), inputs, responseFormat);
      logWriter.close();
      LOG.debug("WPS request can be found at " + logFile);

      InputStream is = new FileInputStream(logFile);
      byte[] buffer = new byte[1024];
      int read = 0;
      while ((read = is.read(buffer)) != -1) {
        os.write(buffer, 0, read);
      }
      is.close();
      os.close();
    } else {
      XMLStreamWriter writer = outFactory.createXMLStreamWriter(os, "UTF-8");
      ExecuteRequest100Writer executer = new ExecuteRequest100Writer(writer);
      executer.write100(process.getId(), inputs, responseFormat);
      writer.close();
    }

    InputStream responseStream = conn.getInputStream();

    if (LOG.isDebugEnabled()) {
      File logFile = File.createTempFile("wpsclient", "response");
      OutputStream logStream = new FileOutputStream(logFile);

      byte[] buffer = new byte[1024];
      int read = 0;
      while ((read = responseStream.read(buffer)) != -1) {
        logStream.write(buffer, 0, read);
      }
      logStream.close();

      responseStream = new FileInputStream(logFile);
      LOG.debug("WPS response can be found at " + logFile);
    }

    // String outputContent = conn.getContentType();
    // TODO determine XML reader encoding based on mime type
    XMLStreamReader reader = inFactory.createXMLStreamReader(responseStream);
    XMLStreamUtils.nextElement(reader);
    if (OWSExceptionReader.isExceptionReport(reader.getName())) {
      throw OWSExceptionReader.parseExceptionReport(reader);
    }
    if (new QName(WPSConstants.WPS_100_NS, "ExecuteResponse").equals(reader.getName())) {
      ExecuteResponse100Reader responseReader = new ExecuteResponse100Reader(reader);
      lastResponse = responseReader.parse100();
      reader.close();

    } else {
      throw new RuntimeException(
          "Unexpected Execute response: root element is '" + reader.getName() + "'");
    }
    return lastResponse;
  }