コード例 #1
0
  public GlosslistHandler(String inDir, String outDir) throws Exception {
    Directory in = new Directory(inDir);
    if (!in.exists()) throw new IOException(inDir);

    Collection<File> files = in.getFiles();

    con = new XPathContext();
    con.addNamespace("db", dbns);

    for (File file : files) {
      System.out.println("processing glossentries in " + file.getName());
      if (file.getCanonicalPath().endsWith(".xml")) {
        Builder builder = new Builder();
        Document doc = builder.build(file);
        try {
          doc = process(doc);
        } catch (Exception e) {
          System.err.println("Error processing " + file.getName());
          throw e;
        }
        serialize(doc, new File(outDir, file.getName()));
      }
    }
  }
コード例 #2
0
 /**
  * Scramble all XML docs within a directory, recursively.
  *
  * @param input
  * @throws XMLStreamException
  * @throws IOException
  */
 public XmlTextScrambler(Directory input) {
   Collection<?> files = input.getFiles(true);
   Iterator<?> iter = files.iterator();
   while (iter.hasNext()) {
     Peeker peeker = PeekerPool.getInstance().acquire();
     File f = (File) iter.next();
     try {
       peeker.peek(f);
     } catch (Exception e) {
       continue; // not an xml file
     }
     try {
       scramble(new EFile(f));
     } catch (Exception e) {
       System.err.println(
           "Exception while scrambling " + f.getAbsolutePath() + ": " + e.getMessage());
     }
     PeekerPool.getInstance().release(peeker);
   }
 }
コード例 #3
0
  protected boolean execute(Map<String, String> parameters) throws TransformerRunException {
    String inputXML = parameters.remove("xml");
    String factory = parameters.remove("factory");
    String out = parameters.remove("out");
    String copyReferring = parameters.remove("copyReferring");

    URL xsltURL = Stylesheets.get("dtbook2xhtml.xsl");
    File inFile = FilenameOrFileURI.toFile(inputXML);

    /*
     * Check if the doc is compound
     */
    try {
      NamespaceReporter nsr = new NamespaceReporter(inFile.toURI().toURL());
      if (nsr.getNamespaceURIs().contains(Namespaces.MATHML_NS_URI)) {
        this.sendMessage(
            i18n("CONTAINS_MATHML"), MessageEvent.Type.INFO, MessageEvent.Cause.SYSTEM);
        parameters.put("svg_mathml", "true");
      }

      if (nsr.getNamespaceURIs().contains(Namespaces.SVG_NS_URI)) {
        this.sendMessage(i18n("CONTAINS_SVG"), MessageEvent.Type.INFO, MessageEvent.Cause.SYSTEM);
        parameters.put("svg_mathml", "true");
      }
    } catch (Exception e) {
      this.sendMessage(
          i18n("ERROR", e.getMessage()), MessageEvent.Type.ERROR, MessageEvent.Cause.SYSTEM);
    }

    try {
      File outFile = FilenameOrFileURI.toFile(out);
      FileUtils.createDirectory(outFile.getParentFile());

      if ("true".equals(copyReferring)) {
        EFile eInFile = new EFile(inFile);
        String outFileName;
        Directory folder;
        if (outFile.isDirectory()) {
          folder = new Directory(outFile);
          outFileName = eInFile.getNameMinusExtension() + ".html";
        } else {
          folder = new Directory(outFile.getParentFile());
          outFileName = outFile.getName();
        }

        if (inFile.getParentFile().getCanonicalPath().equals(folder.getCanonicalPath())) {
          throw new TransformerRunException(i18n("INPUT_OUTPUT_SAME"));
        }
        Fileset fileset = this.buildFileSet(new File(inputXML));
        if (!parameters.containsKey("css_path")) {
          parameters.put("css_path", "default.css");
        }
        Map<String, Object> xslParams = new HashMap<String, Object>();
        xslParams.putAll(parameters);
        Stylesheet.apply(
            inputXML,
            xsltURL,
            new File(folder, outFileName).toString(),
            factory,
            xslParams,
            CatalogEntityResolver.getInstance());

        URL url2 = Css.get(Css.DocumentType.D202_XHTML);
        folder.writeToFile(parameters.get("css_path"), url2.openStream());

        for (Iterator<FilesetFile> it = fileset.getLocalMembers().iterator(); it.hasNext(); ) {
          FilesetFile fsf = it.next();
          if (fsf instanceof ImageFile) {
            FileUtils.copyChild(fsf.getFile(), folder, inFile.getParentFile());
          }
        }
      } else {
        Map<String, Object> xslParams = new HashMap<String, Object>();
        xslParams.putAll(parameters);
        Stylesheet.apply(
            inputXML, xsltURL, out, factory, xslParams, CatalogEntityResolver.getInstance());
      }

      if (parameters.containsKey("svg_mathml")) {
        // some post-xslt namespace cleanup.
        Map<String, Object> domConfigMap =
            LSParserPool.getInstance().getDefaultPropertyMap(Boolean.FALSE);
        domConfigMap.put("resource-resolver", CatalogEntityResolver.getInstance());
        LSParser parser = LSParserPool.getInstance().acquire(domConfigMap);
        DOMConfiguration domConfig = parser.getDomConfig();
        domConfig.setParameter("error-handler", this);
        Document doc = parser.parseURI(outFile.toURI().toString());

        SimpleNamespaceContext snc = new SimpleNamespaceContext();
        snc.declareNamespace("m", Namespaces.MATHML_NS_URI);
        NodeList math = XPathUtils.selectNodes(doc.getDocumentElement(), "//m:*", snc);
        for (int i = 0; i < math.getLength(); i++) {
          try {
            Node m = math.item(i);
            m.setPrefix("");
            if (m.getLocalName().equals("math")) {
              m.getAttributes().removeNamedItem("xmlns:dtbook");
              m.getAttributes().removeNamedItem("xmlns:m");
              Node c = m.getAttributes().getNamedItem("xmlns");
              c.setNodeValue(Namespaces.MATHML_NS_URI);
            }
          } catch (Exception e) {
            this.sendMessage(e.getMessage(), MessageEvent.Type.ERROR);
          }
        }

        Map<String, Object> props = new HashMap<String, Object>();
        props.put(
            "namespaces", Boolean.FALSE); // temp because of attributeNS bug(?) in Xerces DOM3LS				
        props.put("error-handler", this);
        // props.put("format-pretty-print", Boolean.TRUE);
        Serializer.serialize(doc, outFile, "utf-8", props);
      }

    } catch (XSLTException e) {
      throw new TransformerRunException(e.getMessage(), e);
    } catch (CatalogExceptionNotRecoverable e) {
      throw new TransformerRunException(e.getMessage(), e);
    } catch (FilesetFatalException e) {
      throw new TransformerRunException(e.getMessage(), e);
    } catch (IOException e) {
      throw new TransformerRunException(e.getMessage(), e);
    }

    return true;
  }