Ejemplo n.º 1
0
  protected void check(String layoutName, String lang) throws Exception {
    LayoutConversionContext ctx = new LayoutConversionContext(lang, null);
    LayoutDefinition layoutDef =
        service.getLayoutDefinition(WebLayoutManager.JSF_CATEGORY, layoutName);
    Layout layout =
        jsfService.getLayout(
            null, ctx, TEST_CATEGORY, layoutDef, BuiltinModes.VIEW, "currentDocument", null, false);
    String langFilePath = lang;
    if (langFilePath == null) {
      langFilePath = "nolang";
    }
    File file = Framework.createTempFile("layout-instance-export-" + langFilePath, ".json");
    FileOutputStream out = new FileOutputStream(file);
    JSONObject res = JSONLayoutExporter.exportToJson(layout);
    out.write(res.toString(2).getBytes(JSONLayoutExporter.ENCODED_VALUES_ENCODING));
    out.close();

    InputStream written = new FileInputStream(file);
    InputStream expected =
        new FileInputStream(
            FileUtils.getResourcePathFromContext(
                "layout-instance-export-" + langFilePath + ".json"));

    String expectedString = IOUtils.toString(expected, Charsets.UTF_8);
    String writtenString = IOUtils.toString(written, Charsets.UTF_8);
    // order of select options may depend on directory database => do not
    // check order of element by using the NON_EXTENSIBLE mode
    JSONAssert.assertEquals(expectedString, writtenString, JSONCompareMode.NON_EXTENSIBLE);
  }
Ejemplo n.º 2
0
  protected void check(LayoutDefinition layoutDef, String lang) throws Exception {
    LayoutConversionContext ctx = new LayoutConversionContext(lang, null);
    List<LayoutDefinitionConverter> layoutConverters = service.getLayoutConverters(TEST_CATEGORY);
    for (LayoutDefinitionConverter conv : layoutConverters) {
      layoutDef = conv.getLayoutDefinition(layoutDef, ctx);
    }
    List<WidgetDefinitionConverter> widgetConverters = service.getWidgetConverters(TEST_CATEGORY);

    String langFilePath = lang;
    if (langFilePath == null) {
      langFilePath = "nolang";
    }
    File file = Framework.createTempFile("layout-export-" + langFilePath, ".json");
    FileOutputStream out = new FileOutputStream(file);
    JSONLayoutExporter.export(WebLayoutManager.JSF_CATEGORY, layoutDef, ctx, widgetConverters, out);
    out.close();

    InputStream written = new FileInputStream(file);
    InputStream expected =
        new FileInputStream(
            FileUtils.getResourcePathFromContext("layout-export-" + langFilePath + ".json"));

    String expectedString = IOUtils.toString(expected, Charsets.UTF_8);
    String writtenString = IOUtils.toString(written, Charsets.UTF_8);
    // order of select options may depend on directory database => do not
    // check order of element by using the NON_EXTENSIBLE mode
    JSONAssert.assertEquals(expectedString, writtenString, JSONCompareMode.NON_EXTENSIBLE);
  }
Ejemplo n.º 3
0
  @Override
  public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters)
      throws ConversionException {
    blobHolder = new UTF8CharsetConverter().convert(blobHolder, parameters);
    Blob inputBlob = blobHolder.getBlob();
    String blobPath = blobHolder.getFilePath();
    if (inputBlob == null) {
      return null;
    }

    OfficeDocumentConverter documentConverter = newDocumentConverter();
    // This plugin do deal only with one input source.
    String sourceMimetype = inputBlob.getMimeType();

    boolean pdfa1 = parameters != null && Boolean.TRUE.equals(parameters.get(PDFA1_PARAM));

    File sourceFile = null;
    File outFile = null;
    File[] files = null;
    try {

      // If the input blob has the HTML mime type, make sure the
      // charset meta is present, add it if not
      if ("text/html".equals(sourceMimetype)) {
        inputBlob = checkCharsetMeta(inputBlob);
      }

      // Get original file extension
      String ext = inputBlob.getFilename();
      int dotPosition = ext.lastIndexOf('.');
      if (dotPosition == -1) {
        ext = ".bin";
      } else {
        ext = ext.substring(dotPosition);
      }
      // Copy in a file to be able to read it several time
      sourceFile = Framework.createTempFile("NXJOOoConverterDocumentIn", ext);
      InputStream stream = inputBlob.getStream();
      FileUtils.copyToFile(stream, sourceFile);
      stream.close();

      DocumentFormat sourceFormat = null;
      if (sourceMimetype != null) {
        // Try to fetch it from the registry.
        sourceFormat = getSourceFormat(documentConverter, sourceMimetype);
      }
      // If not found in the registry or not given as a parameter.
      // Try to sniff ! What does that smell ? :)
      if (sourceFormat == null) {
        sourceFormat = getSourceFormat(documentConverter, sourceFile);
      }

      // From plugin settings because we know the destination
      // mimetype.
      DocumentFormat destinationFormat =
          getDestinationFormat(documentConverter, sourceFormat, pdfa1);

      // allow HTML2PDF filtering

      List<Blob> blobs = new ArrayList<>();

      if (descriptor.getDestinationMimeType().equals("text/html")) {
        String tmpDirPath = getTmpDirectory();
        File myTmpDir = new File(tmpDirPath + "/JODConv_" + System.currentTimeMillis());
        boolean created = myTmpDir.mkdir();
        if (!created) {
          throw new IOException("Unable to create temp dir");
        }

        outFile =
            new File(
                myTmpDir.getAbsolutePath()
                    + "/"
                    + "NXJOOoConverterDocumentOut."
                    + destinationFormat.getExtension());

        created = outFile.createNewFile();
        if (!created) {
          throw new IOException("Unable to create temp file");
        }

        log.debug("Input File = " + outFile.getAbsolutePath());
        // Perform the actual conversion.
        documentConverter.convert(sourceFile, outFile, destinationFormat);

        files = myTmpDir.listFiles();
        for (File file : files) {
          // copy the files to a new tmp location, as we'll delete them
          Blob blob;
          try (FileInputStream in = new FileInputStream(file)) {
            blob = Blobs.createBlob(in);
          }
          blob.setFilename(file.getName());
          blobs.add(blob);
          // add a blob for the index
          if (file.getName().equals(outFile.getName())) {
            Blob indexBlob;
            try (FileInputStream in = new FileInputStream(file)) {
              indexBlob = Blobs.createBlob(in);
            }
            indexBlob.setFilename("index.html");
            blobs.add(0, indexBlob);
          }
        }

      } else {
        outFile =
            Framework.createTempFile(
                "NXJOOoConverterDocumentOut", '.' + destinationFormat.getExtension());

        // Perform the actual conversion.
        documentConverter.convert(sourceFile, outFile, destinationFormat, parameters);

        Blob blob;
        try (FileInputStream in = new FileInputStream(outFile)) {
          blob = Blobs.createBlob(in, getDestinationMimeType());
        }
        blobs.add(blob);
      }
      return new SimpleCachableBlobHolder(blobs);
    } catch (IOException e) {
      String msg =
          String.format(
              "An error occurred trying to convert file %s to from %s to %s",
              blobPath, sourceMimetype, getDestinationMimeType());
      throw new ConversionException(msg, e);
    } finally {
      if (sourceFile != null) {
        sourceFile.delete();
      }
      if (outFile != null) {
        outFile.delete();
      }

      if (files != null) {
        for (File file : files) {
          if (file.exists()) {
            file.delete();
          }
        }
      }
    }
  }