Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/optionswith #74

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,8 @@ public IConverter getConverter()
return Docx2PDFViaDocx4jConverter.getInstance();
}

public String getWith() {
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public String getVia()
{
return ConverterTypeVia.DOCX4J.name();
}

public String getWith() {
return null;
}

public IConverter getConverter()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import fr.opensagres.xdocreport.converter.ConverterTypeTo;
import fr.opensagres.xdocreport.converter.ConverterTypeVia;
import fr.opensagres.xdocreport.converter.ConverterTypeWith;
import fr.opensagres.xdocreport.converter.IConverter;
import fr.opensagres.xdocreport.converter.discovery.IConverterDiscovery;
import fr.opensagres.xdocreport.converter.docx.poi.itext.OpenXMLFormats2PDFViaITextConverter;
Expand Down Expand Up @@ -59,6 +60,10 @@ public String getVia()
{
return ConverterTypeVia.OpenXMLFormats.name();
}

public String getWith() {
return ConverterTypeWith.ITEXT2.name();
}

public IConverter getConverter()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import fr.opensagres.xdocreport.converter.ConverterTypeTo;
import fr.opensagres.xdocreport.converter.ConverterTypeVia;
import fr.opensagres.xdocreport.converter.ConverterTypeWith;
import fr.opensagres.xdocreport.converter.IConverter;
import fr.opensagres.xdocreport.converter.discovery.IConverterDiscovery;
import fr.opensagres.xdocreport.converter.docx.poi.itext.XWPF2PDFViaITextConverter;
Expand Down Expand Up @@ -59,6 +60,10 @@ public String getVia()
{
return ConverterTypeVia.XWPF.name();
}

public String getWith() {
return ConverterTypeWith.ITEXT2.name();
}

public IConverter getConverter()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public String getVia()
{
return ConverterTypeVia.XWPF.name();
}

public String getWith() {
return null;
}

public IConverter getConverter()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import fr.opensagres.xdocreport.converter.ConverterTypeTo;
import fr.opensagres.xdocreport.converter.ConverterTypeVia;
import fr.opensagres.xdocreport.converter.ConverterTypeWith;
import fr.opensagres.xdocreport.converter.IConverter;
import fr.opensagres.xdocreport.converter.discovery.IConverterDiscovery;
import fr.opensagres.xdocreport.converter.odt.odfdom.itext.ODF2PDFViaITextConverter;
Expand Down Expand Up @@ -60,6 +61,10 @@ public String getVia()
return ConverterTypeVia.ODFDOM.name();
}

public String getWith() {
return ConverterTypeWith.ITEXT2.name();
}

public IConverter getConverter()
{
return ODF2PDFViaITextConverter.getInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import fr.opensagres.xdocreport.converter.ConverterTypeTo;
import fr.opensagres.xdocreport.converter.ConverterTypeVia;
import fr.opensagres.xdocreport.converter.ConverterTypeWith;
import fr.opensagres.xdocreport.converter.IConverter;
import fr.opensagres.xdocreport.converter.discovery.IConverterDiscovery;
import fr.opensagres.xdocreport.converter.odt.odfdom.xhtml.ODF2XHTMLConverter;
Expand Down Expand Up @@ -60,6 +61,10 @@ public String getVia()
return ConverterTypeVia.ODFDOM.name();
}

public String getWith() {
return ConverterTypeWith.ITEXT2.name();
}

public IConverter getConverter()
{
return ODF2XHTMLConverter.getInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ public ConverterTo getConverterTo( String to )
return convertersTo.get( to );
}

public void addConverter( String to, String via, IConverter converter )
public void addConverter( String to, String via, String with, IConverter converter )
{
ConverterTo converterTo = getConverterTo( to );
if ( converterTo == null )
{
converterTo = new ConverterTo( to );
convertersTo.put( to, converterTo );
}
converterTo.addConverter( via, converter );
converterTo.addConverter( via, with, converter );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,25 @@ public static ConverterRegistry getRegistry()
public IConverter findConverter( Options options )
throws XDocConverterException
{
return findConverter( options.getFrom(), options.getTo(), options.getVia() );
return findConverter( options.getFrom(), options.getTo(), options.getVia(), options.getWith() );
}

public IConverter findConverter( String from, String to, String via )
public IConverter findConverter( String from, String to, String via, String with )
throws XDocConverterException
{
return internalFindConverter( from, to, via, true );
return internalFindConverter( from, to, via, with, true );
}

public IConverter getConverter( Options options )
{
return getConverter( options.getFrom(), options.getTo(), options.getVia() );
return getConverter( options.getFrom(), options.getTo(), options.getVia(), options.getWith() );
}

public IConverter getConverter( String from, String to, String via )
public IConverter getConverter( String from, String to, String via, String with )
{
try
{
return internalFindConverter( from, to, via, false );
return internalFindConverter( from, to, via, with, false );
}
catch ( XDocConverterException e )
{
Expand All @@ -107,7 +107,7 @@ public Set<String> getFroms()
return converters.keySet();
}

private IConverter internalFindConverter( String from, String to, String via, boolean throwError )
private IConverter internalFindConverter( String from, String to, String via, String with, boolean throwError )
throws XDocConverterException
{
initializeIfNeeded();
Expand Down Expand Up @@ -135,7 +135,7 @@ private IConverter internalFindConverter( String from, String to, String via, bo
}
if ( via == null )
{
IConverter converter = toConverters.getDefaultConverter();
IConverter converter = toConverters.getDefaultConverter(with);
if ( throwError && converter == null )
{
if ( throwError )
Expand All @@ -148,15 +148,42 @@ private IConverter internalFindConverter( String from, String to, String via, bo
return converter;
}

IConverter converter = toConverters.getConverter( via );
if ( converter == null )
ConverterVia converterVia = toConverters.getConverter( via );
if ( converterVia == null )
{
if ( throwError )
{
String msg = String.format( "Cannot find converters via %s for to=%s for from=%s", via, to, from );
LOGGER.severe( msg );
throw new XDocConverterException( msg );
}
return null;
}

if ( with == null )
{
IConverter converter = converterVia.getDefaultConverter();
if ( throwError && converter == null )
{
if ( throwError )
{
String msg = String.format( "Cannot find converters for to=%s for from=%s", to, from );
LOGGER.severe( msg );
throw new XDocConverterException( msg );
}
}
return converter;
}

IConverter converter = converterVia.getConverter(with);
if ( converter == null )
{
if ( throwError )
{
String msg = String.format( "Cannot find converters with %s for via=%s for to=%s for from=%s", with, via, to, from );
LOGGER.severe( msg );
throw new XDocConverterException( msg );
}
}
return converter;
}
Expand All @@ -173,14 +200,15 @@ protected boolean registerInstance( IConverterDiscovery discovery )
String from = discovery.getFrom();
String to = discovery.getTo();
String via = discovery.getVia();
String with = discovery.getWith();
IConverter converter = discovery.getConverter();
ConverterFrom converterFrom = converters.get( from );
if ( converterFrom == null )
{
converterFrom = new ConverterFrom( from );
converters.put( converterFrom.getFrom(), converterFrom );
}
converterFrom.addConverter( to, via, converter );
converterFrom.addConverter( to, via, with, converter );
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,39 +36,52 @@ public class ConverterTo

private final String to;

private final Map<String, IConverter> converters;
private final Map<String, ConverterVia> converters;

public ConverterTo( String to )
{
this.to = to;
this.converters = new HashMap<String, IConverter>();
this.converters = new HashMap<String, ConverterVia>();
}

public String getTo()
{
return to;
}

public Collection<IConverter> getConvertersTo()
public Collection<ConverterVia> getConvertersVia()
{
return converters.values();
}

public IConverter getConverter( String via )
public ConverterVia getConverter( String via )
{
return converters.get( via );
}

/**
* Returns the default converter or the first converter if none default converter.
*
* @param with
* @return
*/
public IConverter getDefaultConverter()
public IConverter getDefaultConverter( String with )
{
if ( with != null )
{
for ( ConverterVia converterVia : getConvertersVia() )
{
IConverter converter = converterVia.getConverter( with );
if ( converter != null )
{
return converter;
}
}
}
IConverter defaultConverter = null;
for ( IConverter converter : getConvertersTo() )
for ( ConverterVia converterVia : getConvertersVia() )
{
IConverter converter = converterVia.getDefaultConverter();
if ( converter.isDefault() )
{
return converter;
Expand All @@ -80,10 +93,22 @@ public IConverter getDefaultConverter()
}
return defaultConverter;
}

public void addConverter( String via, IConverter converter )
public ConverterVia getConverterVia( String via )
{
converters.put( via, converter );
return converters.get( via );
}

public void addConverter( String via, String with, IConverter converter )
{
ConverterVia converterVia = getConverterVia( via );
if ( converterVia == null )
{
converterVia = new ConverterVia( via );
converters.put( via, converterVia );
}
converterVia.addConverter( with, converter );
// converters.put( via, converter );
}

public Collection<String> getVias()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* The MIT License
*
* Copyright 2015 benoit.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package fr.opensagres.xdocreport.converter;

/**
*
* @author benoit
*/
public enum ConverterTypeWith {
ITEXT2, ITEXT5;
}
Loading