Exemplo n.º 1
0
  public void executedAtExit(Store store, int solutionsNo) {

    try {

      hdTree.endElement("", "", "tree");
      hdTree.endDocument();

      hdVis.endElement("", "", "visualization");
      hdVis.endDocument();

    } catch (SAXException e) {
      e.printStackTrace();
    }
  }
  @Override
  protected void marshalToOutputStream(
      Marshaller ms, Object obj, OutputStream os, Annotation[] anns, MediaType mt)
      throws Exception {

    Templates t = createTemplates(getOutTemplates(anns, mt), outParamsMap, outProperties);
    if (t == null && supportJaxbOnly) {
      super.marshalToOutputStream(ms, obj, os, anns, mt);
      return;
    }
    TransformerHandler th = null;
    try {
      th = factory.newTransformerHandler(t);
    } catch (TransformerConfigurationException ex) {
      TemplatesImpl ti = (TemplatesImpl) t;
      th = factory.newTransformerHandler(ti.getTemplates());
      this.trySettingProperties(th, ti);
    }
    Result result = new StreamResult(os);
    if (systemId != null) {
      result.setSystemId(systemId);
    }
    th.setResult(result);

    if (getContext() == null) {
      th.startDocument();
    }
    ms.marshal(obj, th);
    if (getContext() == null) {
      th.endDocument();
    }
  }
Exemplo n.º 3
0
  public void write(HttpServletResponse response) {
    StreamResult streamResult;
    SAXTransformerFactory tf;
    TransformerHandler hd;
    Transformer serializer;

    try {
      try {
        streamResult = new StreamResult(response.getWriter());
        tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
        hd = tf.newTransformerHandler();
        serializer = hd.getTransformer();

        serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        serializer.setOutputProperty(
            OutputKeys.DOCTYPE_SYSTEM, "http://labs.omniti.com/resmon/trunk/resources/resmon.dtd");
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");

        hd.setResult(streamResult);
        hd.startDocument();
        AttributesImpl atts = new AttributesImpl();
        hd.startElement("", "", "ResmonResults", atts);
        for (ResmonResult r : s) {
          r.write(hd);
        }
        hd.endElement("", "", "ResmonResults");
        hd.endDocument();
      } catch (TransformerConfigurationException tce) {
        response.getWriter().println(tce.getMessage());
      } catch (SAXException se) {
        response.getWriter().println(se.getMessage());
      }
    } catch (IOException ioe) {
    }
  }
Exemplo n.º 4
0
  /**
   * Writes the root end tag to the result.
   *
   * @throws SAXException
   */
  protected void writeEndDocument() {
    try {
      if (indent) handler.ignorableWhitespace("\n".toCharArray(), 0, 1);

      handler.endElement(Constants.MARCXML_NS_URI, COLLECTION, COLLECTION);
      handler.endPrefixMapping("");
      handler.endDocument();
    } catch (SAXException e) {
      throw new MarcException("SAX error occured while writing end document", e);
    }
  }
Exemplo n.º 5
0
 public void finish() throws IOException {
   if (!finished) {
     try {
       transformerHandler.endElement(NS, "", "urlset");
       transformerHandler.endDocument();
       this.writer.close();
       finished = true;
     } catch (SAXException e) {
       throw new RuntimeException(e);
     }
   }
 }
Exemplo n.º 6
0
  @Override
  public String serialize(List<Book> books) throws Exception {
    SAXTransformerFactory factory =
        (SAXTransformerFactory) TransformerFactory.newInstance(); // 取得SAXTransformerFactory实例
    TransformerHandler handler = factory.newTransformerHandler(); // 从factory获取TransformerHandler实例
    Transformer transformer = handler.getTransformer(); // 从handler获取Transformer实例
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // 设置输出采用的编码方式
    transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // 是否自动添加额外的空白
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); // 是否忽略XML声明

    StringWriter writer = new StringWriter();
    Result result = new StreamResult(writer);
    handler.setResult(result);

    String uri = ""; // 代表命名空间的URI 当URI无值时 须置为空字符串
    String localName = ""; // 命名空间的本地名称(不包含前缀) 当没有进行命名空间处理时 须置为空字符串

    handler.startDocument();
    handler.startElement(uri, localName, "books", null);

    AttributesImpl attrs = new AttributesImpl(); // 负责存放元素的属性信息
    char[] ch = null;
    for (Book book : books) {
      attrs.clear(); // 清空属性列表
      attrs.addAttribute(
          uri,
          localName,
          "id",
          "string",
          String.valueOf(book.getId())); // 添加一个名为id的属性(type影响不大,这里设为string)
      handler.startElement(uri, localName, "book", attrs); // 开始一个book元素
      // 关联上面设定的id属性

      handler.startElement(uri, localName, "name", null); // 开始一个name元素
      // 没有属性
      ch = String.valueOf(book.getName()).toCharArray();
      handler.characters(ch, 0, ch.length); // 设置name元素的文本节点
      handler.endElement(uri, localName, "name");

      handler.startElement(uri, localName, "price", null); // 开始一个price元素
      // 没有属性
      ch = String.valueOf(book.getPrice()).toCharArray();
      handler.characters(ch, 0, ch.length); // 设置price元素的文本节点
      handler.endElement(uri, localName, "price");

      handler.endElement(uri, localName, "book");
    }
    handler.endElement(uri, localName, "books");
    handler.endDocument();

    return writer.toString();
  }
Exemplo n.º 7
0
  /**
   * Generate the sitemap files.
   *
   * @throws IOException if the files could not be created.
   * @throws SAXException if a xml error occurs.
   */
  public void generate() throws IOException, SAXException {

    int totalCount = 0;

    AttributesImpl schemaLocation = new AttributesImpl();

    transformerHandler.startDocument();
    transformerHandler.startPrefixMapping("xsd", XMLConstants.W3C_XML_SCHEMA_NS_URI);
    transformerHandler.startPrefixMapping("xsi", XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);

    schemaLocation.addAttribute(
        XMLConstants.W3C_XML_SCHEMA_NS_URI,
        "schemaLocation",
        "xsi:schemaLocation",
        "CDATA",
        "http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd");

    transformerHandler.startElement(NS, "", "sitemapindex", schemaLocation);

    for (final PageProvider provider : providers) {

      LOG.info("Processing " + provider.getName());

      final SiteMap group = new SiteMap(provider.getName());

      try {
        for (final Page page : provider) {
          if (null != page) {
            group.addPage(page);
          }
        }
      } finally {
        group.finish();
        LOG.info(group.getCount() + " entries processed for " + provider.getName());
        totalCount += group.getCount();
      }

      for (final SiteMap.SiteMapFile map : group.getSiteMaps()) {
        transformerHandler.startElement("", "", "sitemap", new AttributesImpl());
        addElement("loc", uri.resolve(map.getFileName()).toString());
        addElement("lastmod", formatDateW3c((new Date())));
        transformerHandler.endElement("", "", "sitemap");
      }
    }

    transformerHandler.endElement(NS, "", "sitemapindex");
    transformerHandler.endDocument();
    writer.close();

    LOG.info("All done (" + totalCount + " entries)");
  }
Exemplo n.º 8
0
    @Override
    public void run() {
      try {
        handler.startDocument();
        handler.startPrefixMapping(JSON_PREFIX, JSON_URI);
        handler.startPrefixMapping(XSD_PREFIX, XSD_URI);

        while (jp.nextToken() != null && !exitThread) {
          outputItem(jp, handler);
        }

        handler.endPrefixMapping(XSD_PREFIX);
        handler.endPrefixMapping(JSON_PREFIX);
        handler.endDocument();
        out.close();
      } catch (Exception ex) {
        LOG.error("Error processing JSON input stream. Reason: " + ex.getMessage(), ex);
      }
    }
  /**
   * Serializes the given entries to the given {@link java.io.OutputStream}.
   *
   * <p>After the serialization is finished the provided {@link java.io.OutputStream} remains open.
   *
   * @param out stream to serialize to
   * @param entries entries to serialize
   * @param casesensitive indicates if the written dictionary should be case sensitive or case
   *     insensitive.
   * @throws java.io.IOException If an I/O error occurs
   */
  public static void serialize(OutputStream out, Iterator<Entry> entries, boolean casesensitive)
      throws IOException {
    StreamResult streamResult = new StreamResult(out);
    SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();

    TransformerHandler hd;
    try {
      hd = tf.newTransformerHandler();
    } catch (TransformerConfigurationException e) {
      throw new AssertionError("The Transformer configuration must be valid!");
    }

    Transformer serializer = hd.getTransformer();
    serializer.setOutputProperty(OutputKeys.ENCODING, CHARSET);
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");

    hd.setResult(streamResult);

    try {
      hd.startDocument();

      AttributesImpl dictionaryAttributes = new AttributesImpl();

      dictionaryAttributes.addAttribute(
          "", "", ATTRIBUTE_CASE_SENSITIVE, "", String.valueOf(casesensitive));
      hd.startElement("", "", DICTIONARY_ELEMENT, dictionaryAttributes);

      while (entries.hasNext()) {
        Entry entry = entries.next();

        serializeEntry(hd, entry);
      }

      hd.endElement("", "", DICTIONARY_ELEMENT);

      hd.endDocument();
    } catch (SAXException e) {
      // TODO update after Java6 upgrade
      throw (IOException)
          new IOException("Error during serialization: " + e.getMessage()).initCause(e);
    }
  }