Package de.ddb.conversion

Examples of de.ddb.conversion.Converter


    final static Format B = new Format("B");

    public void testGetConverter() throws ConverterException {
        ConverterRegistry registry = new ConverterRegistry();

        Converter anyEncConverter = new GenericConverter(){
         
          // A, B, null, null
          {
            getConverterContext().setSourceFormat(A);
            getConverterContext().setTargetFormat(B);
            getConverterContext().setSourceEncoding(null);
            getConverterContext().setTargetEncoding(null);
          }

      public void convert(InputStream in, OutputStream out,
          ConversionParameters params) throws ConverterException,
          IOException
      {
        // TODO Auto-generated method stub
       
      }

      public void convert(Reader in, Writer out,
          ConversionParameters params) throws ConverterException,
          IOException
      {
        // TODO Auto-generated method stub
       
      }
         
        };
       
        Converter anySourceEncConverter = new GenericConverter(){
         
//          A, B, null, "UTF-8"
          {
            getConverterContext().setSourceFormat(A);
            getConverterContext().setTargetFormat(B);
            getConverterContext().setSourceEncoding(null);
            getConverterContext().setTargetEncoding("UTF-8");
          }

      public void convert(InputStream in, OutputStream out,
          ConversionParameters params) throws ConverterException,
          IOException
      {
        // TODO Auto-generated method stub
       
      }

      public void convert(Reader in, Writer out,
          ConversionParameters params) throws ConverterException,
          IOException
      {
        // TODO Auto-generated method stub
       
      }
         
        };
        Converter anyTargetEncConverter = new GenericConverter(){
         
//          A, B, "UTF-8", null
          {
            getConverterContext().setSourceFormat(A);
            getConverterContext().setTargetFormat(B);
            getConverterContext().setSourceEncoding("UTF-8");
            getConverterContext().setTargetEncoding(null);
          }

      public void convert(InputStream in, OutputStream out,
          ConversionParameters params) throws ConverterException,
          IOException
      {
        // TODO Auto-generated method stub
       
      }

      public void convert(Reader in, Writer out,
          ConversionParameters params) throws ConverterException,
          IOException
      {
        // TODO Auto-generated method stub
       
      }
         
        };
        Converter specifiedEncConverter = new GenericConverter(){
         
//          A, B, "UTF-8", "UTF-8"
          {
            getConverterContext().setSourceFormat(A);
            getConverterContext().setTargetFormat(B);
            getConverterContext().setSourceEncoding("UTF-8");
            getConverterContext().setTargetEncoding("UTF-8");
          }

      public void convert(InputStream in, OutputStream out,
          ConversionParameters params) throws ConverterException,
          IOException
      {
        // TODO Auto-generated method stub
       
      }

      public void convert(Reader in, Writer out,
          ConversionParameters params) throws ConverterException,
          IOException
      {
        // TODO Auto-generated method stub
       
      }
         
        };

        registry.addConverter(anySourceEncConverter);
        registry.addConverter(anyTargetEncConverter);
        registry.addConverter(specifiedEncConverter);

        Converter returnedConverter;

        returnedConverter = registry.getConverter(A.getName(), B.getName(),
                "egal", "UTF-8");
        assertTrue(anySourceEncConverter == returnedConverter);
View Full Code Here


  @Test
  public void testConvert() throws ConverterException, IOException
  {
    String file = "1015404219_pica.mrc";
    Converter converter = new DumpConverter();
    InputStream in = new FileInputStream("test" + File.separator + "input" + File.separator + file);
    OutputStream out = new FileOutputStream("test" + File.separator + "output" + File.separator + file + ".out");
    ConversionParameters params = new ConversionParameters();
    params.setSourceCharset("x-PICA");
    params.setTargetCharset("UTF-8");
    converter.convert(in, out, params);
  }
View Full Code Here

   * @throws ConverterException
   */
  public Converter getConverter(final String sourceFormatName,
      final String targetFormatName, final String sourceEnc,
      final String targetEnc) throws ConverterException {
    Converter c = getRegistry().get(
        sourceFormatName + "|" + targetFormatName + "|" + sourceEnc
            + "|" + targetEnc);
    if (c != null) {
      LOGGER.info("Found converter [" + c.getClass().getSimpleName()
          + "], [" + sourceFormatName + "|" + targetFormatName + "|"
          + sourceEnc + "|" + targetEnc + "]");
    }
    if (c == null) {
      c = getRegistry().get(
          sourceFormatName + "|" + targetFormatName + "|null|"
              + targetEnc);
      if (c != null) {
        LOGGER.info("Found converter [" + c.getClass().getSimpleName()
            + "], [" + sourceFormatName + "|" + targetFormatName
            + "|null|" + targetEnc + "]");
      }
    }
    if (c == null) {
      c = getRegistry().get(
          sourceFormatName + "|" + targetFormatName + "|" + sourceEnc
              + "|null");
      if (c != null) {
        LOGGER.info("Found converter [" + c.getClass().getSimpleName()
            + "], [" + sourceFormatName + "|" + targetFormatName
            + "|" + sourceEnc + "|null]");
      }
    }
    if (c == null) {
      c = getRegistry().get(
          sourceFormatName + "|" + targetFormatName + "|null|null");
      if (c != null) {
        LOGGER.info("Found converter [" + c.getClass().getSimpleName()
            + "], [" + sourceFormatName + "|" + targetFormatName
            + "|null|null]");
      }
    }
    if (c == null) {
View Full Code Here

     * @return a converter that converts between source format and target format.
     * @throws ConverterException
     */
    public Converter getConverter(String sourceFormatName,
            String targetFormatName, String sourceEnc, String targetEnc) throws ConverterException {
        Converter c = this.registry.get(sourceFormatName + "|" + targetFormatName
                + "|" + sourceEnc + "|" + targetEnc);
        if(c != null)
        {
          LOGGER.info("Found converter ["+c.getClass().getSimpleName()+"], ["+sourceFormatName+"|"+targetFormatName+"|"+sourceEnc+"|"+targetEnc+"]");
        }
        if (c == null) {
            c = this.registry.get(sourceFormatName + "|" + targetFormatName
                    + "|null|" + targetEnc);
            if(c != null)
            {
              LOGGER.info("Found converter ["+c.getClass().getSimpleName()+"], ["+sourceFormatName+"|"+targetFormatName+"|null|"+targetEnc+"]");
            }
        }
        if (c == null) {
            c = this.registry.get(sourceFormatName + "|" + targetFormatName
                    + "|" + sourceEnc + "|null");
            if(c != null)
            {
              LOGGER.info("Found converter ["+c.getClass().getSimpleName()+"], ["+sourceFormatName+"|"+targetFormatName+"|"+sourceEnc+"|null]");
            }
        }
        if (c == null) {
            c = this.registry.get(sourceFormatName + "|" + targetFormatName
                    + "|null|null");
            if(c != null)
            {
              LOGGER.info("Found converter ["+c.getClass().getSimpleName()+"], ["+sourceFormatName+"|"+targetFormatName+"|null|null]");
            }
        }
        if(c == null)
        {
          throw new ConverterException("No converter registered for conversions from '"
View Full Code Here

       * convert input
       */
      final ConversionContext context = ConversionContextFactory
          .newDefaultEnvironment();

      final Converter c = new MabxmlToMabConverter();
      c.getConverterContext()
          .setSourceEncoding(MabxmlToMab.inputEncoding);
      c.getConverterContext().setTargetEncoding(
          MabxmlToMab.outputEncoding);
      c.getConverterContext()
          .setSourceFormat(
              context.getFormat(ConversionContextFactory.DOCFORMATNAME_MABXML1));
      c.getConverterContext()
          .setTargetFormat(
              context.getFormat(ConversionContextFactory.DOCFORMATNAME_MAB2));
      context.addConverter(c);
      final ConversionParameters params = new ConversionParameters();
      params.setSourceCharset(MabxmlToMab.inputEncoding);
      params.setTargetCharset(MabxmlToMab.outputEncoding);
      final ByteArrayOutputStream bout = new ByteArrayOutputStream();
      c.convert(MabxmlToMab.in, bout, params);
      if (MabxmlToMab.outputEncoding.equals("x-MAB")
          || MabxmlToMab.outputEncoding.equals("x-PICA")) {
        /*
         * No normalization for x-PICA and x-MAB, since they
         * NFD-normalize their input anyway
View Full Code Here

            /*
             * convert input
             */
            ConversionContext context = ConversionContextFactory.newDefaultEnvironment();
           
            Converter c = new MabxmlToMabConverter();
            c.getConverterContext().setSourceEncoding(inputEncoding);
            c.getConverterContext().setTargetEncoding(outputEncoding);
            c.getConverterContext().setSourceFormat(context.getFormat(ConversionContextFactory.DOCFORMATNAME_MABXML1));
            c.getConverterContext().setTargetFormat(context.getFormat(ConversionContextFactory.DOCFORMATNAME_MAB2));
            context.addConverter(c);
            ConversionParameters params = new ConversionParameters();
            InputStream convertedIn = c.convert(in, params);
            if (outputEncoding.equals("x-MAB")
                    || outputEncoding.equals("x-PICA")) {
                /*
                 * No normalization for x-PICA and x-MAB, since they
                 * NFD-normalize their input anyway
View Full Code Here

      params.setAddDoctype(false);
      params.setTargetCharset(MabToMabxml.outputEncoding);
      params.setSourceCharset(MabToMabxml.inputEncoding);
      final ConversionContext context = ConversionContextFactory
          .newDefaultEnvironment();
      final Converter c = new MabToMabxmlConverter();
      c.getConverterContext()
          .setSourceEncoding(MabToMabxml.inputEncoding);
      c.getConverterContext().setTargetEncoding(
          MabToMabxml.outputEncoding);
      c.getConverterContext()
          .setSourceFormat(
              context.getFormat(ConversionContextFactory.DOCFORMATNAME_MAB2));
      c.getConverterContext()
          .setTargetFormat(
              context.getFormat(ConversionContextFactory.DOCFORMATNAME_MABXML1));
      context.addConverter(c);
      c.convert(MabToMabxml.in, MabToMabxml.out, params);
      MabToMabxml.LOGGER.info("Done.");
    } catch (final ClassNotFoundException e) {
      System.out.println("Error: " + e);
      MabToMabxml.LOGGER.error("main(String[])", e);
    } finally {
View Full Code Here

        List<de.ddb.conversion.context.xml.JavaConverter> javaConverters = contextDocument
                .getConversionContext().getConverters().getJavaConverterList();
        for (de.ddb.conversion.context.xml.JavaConverter jc : javaConverters) {
          try
          {
            Converter converter = (Converter) Class.forName(
                        jc.getClassName()).newInstance();
           
            Properties converterProps = null;
                if(jc.isSetProperties())
                {
                  List<Property> props = jc.getProperties().getPropertyList();
                  if(!props.isEmpty())
                  {
                    converterProps = new Properties();
                    for(Property prop : props)
                      {
                      if(LOGGER.isDebugEnabled())
                      {                       
                        LOGGER.debug("Found property [" + prop.getKey() + "] = " + prop.getStringValue());
                      }
                        converterProps.setProperty(prop.getKey(), prop.getStringValue());
                      }
                  }
                }
           
                for (String sf : jc.getSourceFormatList()) {
                    for (String tf : jc.getTargetFormatList()) {
                        context.addConverter(converter, sf,
                                        tf,
                                        jc.getSourceEncoding(),
                                        jc.getTargetEncoding(),
                                        converterProps);
                    }
                }
          }
          catch(ClassNotFoundException e)
          {
            LOGGER.error("Could not find class: " + jc.getClassName());
            if(LOGGER.isDebugEnabled())
            {             
              LOGGER.debug(e, e);
            }
          }
          catch (InstantiationException e) {
            LOGGER.error("Could not instantiate converter: " + jc.getClassName());
            if(LOGGER.isDebugEnabled())
            {             
              LOGGER.debug(e, e);
            }
      }
      catch (IllegalAccessException e)
      {
        LOGGER.error("Could not access converter: " + jc.getClassName());
        if(LOGGER.isDebugEnabled())
            {             
              LOGGER.debug(e, e);
            }
      }
          catch (Exception e)
      {
        LOGGER.error("Unknown exception: " + jc.getClassName() + " : " + e.getMessage());
        if(LOGGER.isDebugEnabled())
            {             
              LOGGER.debug(e, e);
            }
      }
           
        }

        /*
         * set piped converters
         */
        List<de.ddb.conversion.context.xml.PipedConverter> pipedConverters = contextDocument
                .getConversionContext().getConverters()
                .getPipedConverterList();
        for (de.ddb.conversion.context.xml.PipedConverter pc : pipedConverters) {
          try
          {
            Properties converterProps = null;
                if(pc.isSetProperties())
                {
                  List<Property> props = pc.getProperties().getPropertyList();
                  if(!props.isEmpty())
                  {
                    converterProps = new Properties();
                    for(Property prop : props)
                      {
                        converterProps.setProperty(prop.getKey(), prop.xmlText());
                      }
                  }
                }
            if(pc.getSourceFormatList().isEmpty())
            {
              throw new ConverterInitializationException("Missing source format to initialize piped converter.");
            }
            if(pc.getTargetFormatList().isEmpty())
            {
              throw new ConverterInitializationException("Missing target format to initialize piped converter.");
            }
            Converter c1 = context.getConverter(pc
                        .getSourceFormatList().get(0), pc
                        .getIntermediateFormat(), pc
                        .getSourceEncoding(), pc
                        .getIntermediateEncoding());
                Converter c2 = context.getConverter(pc
                        .getIntermediateFormat(), pc
                        .getTargetFormatList().get(0), pc
                        .getIntermediateEncoding(), pc
                        .getTargetEncoding());
                PipedConverter c = new PipedConverter(c1, c2);
View Full Code Here

            // outputNormalizer.convert(convertedIn, out);
            // }
            ConversionParameters params = new ConversionParameters();
            params.setAddDoctype(false);
            ConversionContext context = ConversionContextFactory.newDefaultEnvironment();
            Converter c = new MabToMabxmlConverter();
            c.getConverterContext().setSourceEncoding(inputEncoding);
            c.getConverterContext().setTargetEncoding(outputEncoding);
            c.getConverterContext().setSourceFormat(context.getFormat(ConversionContextFactory.DOCFORMATNAME_MAB2));
            c.getConverterContext().setTargetFormat(context.getFormat(ConversionContextFactory.DOCFORMATNAME_MABXML1));
            context.addConverter(c);
            c.convert(in, out, params);

        }
    catch (ClassNotFoundException e)
    {
      System.out.println("Error: " + e);
View Full Code Here

        ConversionContext context = ConversionContextFactory
                .newContextFromStream(new FileInputStream(configFilePath));
        if (logger.isDebugEnabled()) {
            logger.debug("main() - converting");
        }
        Converter converter = context.getConverter(sourceFormat,
                targetFormat);
        converter.convert(System.in, System.out, sourceCharset, targetCharset);
    }
View Full Code Here

TOP

Related Classes of de.ddb.conversion.Converter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.