Example #1
0
 public void parseStreaming(InputStream is, OutputStream os, Context context) {
   // HTML5 may violate XML correctness. It shouldn't, but hey, shit happens.
   HtmlParser hp = new HtmlParser(XmlViolationPolicy.ALLOW);
   // true streaming for max awesomeness
   hp.setStreamabilityViolationPolicy(XmlViolationPolicy.FATAL);
   handler.setOutput(os);
   handler.setContext(context);
   hp.setContentHandler(handler);
   try {
     hp.parse(new InputSource(is));
     handler.end();
   } catch (IOException | SAXException ex) {
     Logger.getLogger(getClass()).error("HTML parsing failed", ex);
   }
 }
Example #2
0
  public Map<String, String> getFragmentParts(InputStream is, Context context) {
    // HTML5 may violate XML correctness. It shouldn't, but hey, shit happens.
    HtmlParser hp = new HtmlParser(XmlViolationPolicy.FATAL);
    // true streaming for max awesomeness
    hp.setStreamabilityViolationPolicy(XmlViolationPolicy.FATAL);

    handler.setAction(Handler.Action.EXTRACT_INLINE_PARTS);
    handler.setContext(context);
    handler.doNotWriteDocumentStart();
    hp.setContentHandler(handler);
    try {
      hp.parse(new InputSource(is));
    } catch (IOException | SAXException ex) {
      Logger.getLogger(getClass()).error("HTML parsing failed", ex);
    }
    return handler.getFragmentParts();
  }
  private String dispatch(Document document, HandlerContext handlercontext)
      throws RequestFailureException, SAXException {
    StringBuilder results = new StringBuilder("<?xml version=\"1.0\"?><results>");

    NodeList nodes = document.getFirstChild().getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
      try {
        if ("audit".equals(nodes.item(i).getNodeName())) {
          continue;
        }
        if ("#text".equals(nodes.item(i).getNodeName())) {
          // Noeud ajoute lorsque le manager est execute dans le container.
          continue;
        }
        String id = XMLUtils.getNodeValue(nodes.item(i), "id");
        Handler handler = getHandler(id);
        handler.setContext(handlercontext);
        if (!handlercontext.isAllowedTo(id)) {
          throw new SecurityException(
              "Vous n'avez pas les droits pour effectuer ce traitement (" + id + ")");
        }

        fireHandlerStarted(handlercontext, handler);
        String result = handler.proceed(nodes.item(i));
        fireHandlerStopped(handlercontext, handler);

        results.append(result);
      } catch (HandlerException handlerException) {
        Exception cause = handlerException;
        if (handlerException.getCausedBy() != null && handlerException.getMessage() == null) {
          cause = handlerException.getCausedBy();
        }
        throw new RequestFailureException(
            XMLUtils.getAttribute(nodes.item(i), "request_id"), cause);
      } catch (Exception exception) {
        throw new RequestFailureException(
            XMLUtils.getAttribute(nodes.item(i), "request_id"), exception);
      }
    }
    return results.append("</results>").toString();
  }