Beispiel #1
0
  public void configure(Runner runner, Action parent, Element config) throws EXistException {
    super.configure(runner, parent, config);

    if (config.hasAttribute("count")) {
      try {
        count = Integer.parseInt(config.getAttribute("count"));
      } catch (NumberFormatException e) {
        throw new EXistException(
            "invalid value for attribute 'count': " + config.getAttribute("count"), e);
      }
    }

    if (!config.hasAttribute("todir"))
      throw new EXistException("generate requires an attribute 'todir'");
    directory = new File(config.getAttribute("todir"));
    if (!(directory.exists() && directory.isDirectory()))
      throw new EXistException(
          directory.getAbsolutePath() + " does not exist or is not a directory");

    if (config.hasAttribute("prefix")) prefix = config.getAttribute("prefix");

    NodeList children = config.getChildNodes();
    Element root = null;
    for (int i = 0; i < children.getLength(); i++) {
      Node node = children.item(i);
      if (node.getNodeType() == Node.ELEMENT_NODE) {
        root = (Element) node;
        break;
      }
    }
    if (root == null) throw new EXistException("no content element found for generate");
    StringWriter writer = new StringWriter();
    DOMSerializer serializer = new DOMSerializer(writer, new Properties());
    try {
      serializer.serialize(root);
    } catch (TransformerException e) {
      throw new EXistException(
          "exception while serializing generate content: " + e.getMessage(), e);
    }
    xqueryContent = writer.toString();
  }
Beispiel #2
0
  public void testDocumentBuilder() {
    try {
      DocumentBuilderReceiver receiver = new DocumentBuilderReceiver();
      SAXParserFactory factory = SAXParserFactory.newInstance();
      factory.setNamespaceAware(true);
      XMLReader reader = factory.newSAXParser().getXMLReader();
      reader.setContentHandler(receiver);
      reader.parse(new InputSource(new StringReader(XML)));

      Document doc = receiver.getDocument();
      Node node = doc.getFirstChild();
      assertNotNull(node);

      StringWriter writer = new StringWriter();
      DOMSerializer serializer = new DOMSerializer(writer, null);
      serializer.serialize(node);
      System.out.println(writer.toString());
    } catch (Exception e) {
      fail(e.getMessage());
    }
  }