コード例 #1
0
      public SiteMapFile(int count) throws IOException, SAXException {
        this.fileName = name + '_' + count + ".xml";
        writer = new FileWriter(new File(location, fileName));
        final StreamResult streamResult = new StreamResult(writer);

        try {
          transformerHandler = tf.newTransformerHandler();

          Transformer serializer = transformerHandler.getTransformer();

          serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //
          serializer.setOutputProperty(OutputKeys.METHOD, "xml");
          serializer.setOutputProperty(OutputKeys.INDENT, "yes");

          transformerHandler.setResult(streamResult);
          transformerHandler.startDocument();

          AttributesImpl schemaLocation = new AttributesImpl();

          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/sitemap.xsd");

          transformerHandler.startElement(NS, "", "urlset", schemaLocation);
        } catch (TransformerConfigurationException e) {
          throw new RuntimeException(e);
        }
      }
コード例 #2
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)");
  }
コード例 #3
0
 private void declarePrefixes(UnmarshallingContext context, String[] prefixes)
     throws SAXException {
   for (int i = prefixes.length - 1; i >= 0; i--) {
     String nsUri = context.getNamespaceURI(prefixes[i]);
     if (nsUri == null)
       throw new IllegalStateException("prefix \'" + prefixes[i] + "\' isn't bound");
     handler.startPrefixMapping(prefixes[i], nsUri);
   }
 }
コード例 #4
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);
      }
    }