コード例 #1
0
  private void scramble(EFile input) throws XMLStreamException, IOException {
    File output =
        new File(
            input.getParentFile(),
            input.getNameMinusExtension() + ".scrambled." + input.getExtension());

    Map<String, Object> xifProperties = null;
    Map<String, Object> xofProperties = null;
    XMLInputFactory xif = null;
    XMLOutputFactory xof = null;
    XMLEventWriter writer = null;
    XMLEventFactory xef = null;
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
      xifProperties = StAXInputFactoryPool.getInstance().getDefaultPropertyMap(false);
      xofProperties = StAXOutputFactoryPool.getInstance().getDefaultPropertyMap();
      xif = StAXInputFactoryPool.getInstance().acquire(xifProperties);
      xif.setXMLResolver(new StaxEntityResolver(CatalogEntityResolver.getInstance()));
      xof = StAXOutputFactoryPool.getInstance().acquire(xofProperties);
      xef = StAXEventFactoryPool.getInstance().acquire();
      fis = new FileInputStream(input);
      fos = new FileOutputStream(output);

      XMLEventReader baseReader = xif.createXMLEventReader(fis);
      writer = xof.createXMLEventWriter(fos);
      BookmarkedXMLEventReader reader = new BookmarkedXMLEventReader(baseReader);
      ContextStack context = new ContextStack(true);

      boolean skipElemTextScrambling = false;
      int c = 0;
      while (reader.hasNext()) {
        XMLEvent xe = reader.nextEvent();
        context.addEvent(xe);
        if (xe.isStartElement()) {
          skipElemTextScrambling = shouldSkip(xe.asStartElement());
          if (isMetaElement(xe.asStartElement())) {
            xe = handleMetaElement(xe.asStartElement(), xef);
          } else if (isImageElement(xe.asStartElement())) {
            xe = handleImageElement(xe.asStartElement(), xef);
          } else if (isAcronymElement(xe.asStartElement())) {
            xe = handleAcronymElement(xe.asStartElement(), xef);
          } else if (isLinkElement(xe.asStartElement())) {
            xe = handleLinkElement(xe.asStartElement(), xef);
          } else if (isAnchorElement(xe.asStartElement())) {
            xe = handleAnchorElement(xe.asStartElement(), xef);
          }
        } else if (xe.isCharacters()
            && !skipElemTextScrambling
            && !CharUtils.isXMLWhiteSpace(xe.asCharacters().getData())) {
          xe =
              xef.createCharacters(
                  "["
                      + Integer.toString(++c)
                      + "] "
                      + context.getContextXPath(
                          ContextStack.XPATH_SELECT_ELEMENTS_ONLY,
                          ContextStack.XPATH_PREDICATES_NONE));
        } else if (xe.getEventType() == XMLEvent.PROCESSING_INSTRUCTION) {
          xe = handleProcessingInstruction((ProcessingInstruction) xe, xef);
        } else if (xe.isEndElement()) {
          skipElemTextScrambling = false;
        }
        if (xe != null) writer.add(xe);
      }

    } catch (CatalogExceptionNotRecoverable e) {
      e.printStackTrace();
    } finally {
      fis.close();
      fos.close();
      writer.flush();
      writer.close();
      StAXInputFactoryPool.getInstance().release(xif, xifProperties);
      StAXOutputFactoryPool.getInstance().release(xof, xofProperties);
      StAXEventFactoryPool.getInstance().release(xef);
    }
  }
コード例 #2
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;
  }