Beispiel #1
0
  public void testNextTagInvalid() throws XMLStreamException {
    String XML = "<root>   non-empty<leaf /></root>";
    String XML2 = "<root><leaf></leaf>text   </root>";

    for (int i = 0; i < 4; ++i) {
      boolean ns = (i & 1) != 0;
      boolean coal = (i & 2) != 0;
      XMLEventReader er = getReader(XML, ns, coal);
      assertTokenType(START_DOCUMENT, er.nextEvent().getEventType());
      assertTokenType(START_ELEMENT, er.nextEvent().getEventType());
      String msg = null;
      try {
        XMLEvent evt = er.nextTag();
        msg =
            "Expected a XMLStreamException when trying to call XMLEventReader.nextTag() on non-empty CHARACTERS";
      } catch (XMLStreamException sex) {
        // fine!
      } catch (Throwable t) {
        msg =
            "Expected a XMLStreamException when trying to call XMLEventReader.nextTag() on non-empty CHARACTERS; got ("
                + t.getClass()
                + "): "
                + t;
      }
      if (msg != null) {
        fail(msg);
      }
      er.close();

      /* any other easily failing cases? Maybe if we are on top of
       * END_ELEMENT, and will hit another one?
       */
      er = getReader(XML2, ns, coal);
      assertTokenType(START_DOCUMENT, er.nextEvent().getEventType());
      assertTokenType(START_ELEMENT, er.nextEvent().getEventType());
      assertTokenType(START_ELEMENT, er.nextEvent().getEventType());
      assertTokenType(END_ELEMENT, er.nextEvent().getEventType());

      try {
        XMLEvent evt = er.nextTag();
        msg =
            "Expected a XMLStreamException when trying to call XMLEventReader.nextTag() on END_ELEMENT and hitting non-ws text; got event "
                + tokenTypeDesc(evt);
      } catch (XMLStreamException sex) {
        msg = null; // fine!
      } catch (Throwable t) {
        msg =
            "Expected a XMLStreamException when trying to call XMLEventReader.nextTag() on END_ELEMENT and hitting non-ws text; got: "
                + t;
      }
      if (msg != null) {
        fail(msg);
      }
      er.close();
    }
  }
Beispiel #2
0
  public void testSimpleValid() throws XMLStreamException {
    /* Whether prolog/epilog white space is reported is not defined
     * by StAX specs, thus, let's not add any
     */
    String XML =
        "<?xml version='1.0' ?>"
            + "<!DOCTYPE root [  ]>"
            + "<root attr='123'><!-- comment -->\n"
            + "</root>";

    for (int i = 0; i < 4; ++i) {
      boolean ns = (i & 1) != 0;
      boolean coal = (i & 2) != 0;
      XMLEventReader er = getReader(XML, ns, coal);

      assertTokenType(START_DOCUMENT, er.nextEvent().getEventType());
      assertTokenType(DTD, er.nextEvent().getEventType());
      assertTokenType(START_ELEMENT, er.nextEvent().getEventType());
      assertTokenType(COMMENT, er.nextEvent().getEventType());
      // for fun, let's just use next() instead of nextEvent()
      XMLEvent evt = (XMLEvent) er.next();
      assertTokenType(CHARACTERS, evt.getEventType());
      assertTokenType(END_ELEMENT, er.nextEvent().getEventType());
      assertTokenType(END_DOCUMENT, er.nextEvent().getEventType());
      assertFalse(er.hasNext());
      er.close();
    }
  }
Beispiel #3
0
  public void doFilter(
      ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
      throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    RequestURIParts parts = parseParts(request);
    if (parts != null) {
      String realBaseAddress = parts.getBaseAddress();
      String idlPath = parts.getFilePath();
      InputStream idl = this.servletContext.getResourceAsStream(idlPath);
      if (idl != null) {
        servletResponse.setContentType("text/xml");
        String assumedBaseAddress = null;
        try {
          XMLEventReader eventReader = this.inputFactory.createXMLEventReader(idl);
          XMLEventWriter eventWriter =
              this.outputFactory.createXMLEventWriter(servletResponse.getWriter());
          while (eventReader.hasNext()) {
            XMLEvent event = eventReader.nextEvent();
            if (event.isProcessingInstruction()) {
              String target = ((ProcessingInstruction) event).getTarget();
              if ("enunciate-assumed-base-uri".equals(target)) {
                assumedBaseAddress = ((ProcessingInstruction) event).getData();
                if (assumedBaseAddress.endsWith("/")) {
                  assumedBaseAddress =
                      assumedBaseAddress.substring(0, assumedBaseAddress.length() - 1);
                }
              }
              continue;
            } else if (event.getEventType() == XMLStreamConstants.CDATA
                || event.getEventType() == XMLStreamConstants.CHARACTERS) {
              String data = ((Characters) event).getData();
              if (assumedBaseAddress != null && data.contains(assumedBaseAddress)) {
                data = data.replace(assumedBaseAddress, realBaseAddress);
                event = new DelegatingCharacters(((Characters) event), data);
              }
            } else if (event.getEventType() == XMLStreamConstants.START_ELEMENT) {
              List<Attribute> attributes = new ArrayList<Attribute>();
              Iterator attributesIt = ((StartElement) event).getAttributes();
              while (attributesIt.hasNext()) {
                Attribute attribute = (Attribute) attributesIt.next();
                String value = attribute.getValue();
                if (assumedBaseAddress != null && value.contains(assumedBaseAddress)) {
                  value = value.replace(assumedBaseAddress, realBaseAddress);
                  attribute = new DelegatingAttribute(attribute, value);
                  event = new DelegatingStartElement(((StartElement) event), attributes);
                }
                attributes.add(attribute);
              }
            }

            eventWriter.add(event);
          }

          eventReader.close();
          eventWriter.flush();
          eventWriter.close();
          return;
        } catch (XMLStreamException e) {
          throw new ServletException(e);
        }
      }
    }

    chain.doFilter(servletRequest, servletResponse);
  }