Skip to content

Commit

Permalink
Simplify creation of new content inliners #93 (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
soraksh authored and Andrey Subbotin committed Nov 14, 2018
1 parent 998fef3 commit d48155b
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import com.haulmont.yarg.exception.UnsupportedFormatException;
import com.haulmont.yarg.formatters.ReportFormatter;
import com.haulmont.yarg.formatters.factory.inline.DefaultInlinersProvider;
import com.haulmont.yarg.formatters.factory.inline.ReportInlinersProvider;
import com.haulmont.yarg.formatters.impl.*;
import com.haulmont.yarg.formatters.impl.doc.connector.OfficeIntegrationAPI;
import com.haulmont.yarg.formatters.impl.docx.HtmlImportProcessor;
Expand All @@ -27,6 +29,7 @@
import com.haulmont.yarg.structure.ReportTemplate;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -40,6 +43,8 @@ public class DefaultFormatterFactory implements ReportFormatterFactory {

protected Map<String, FormatterCreator> formattersMap = new HashMap<String, FormatterCreator>();

protected ReportInlinersProvider inlinersProvider;

public DefaultFormatterFactory() {
htmlImportProcessor = new HtmlImportProcessorImpl();
htmlToPdfConverterFactory = new HtmlToPdfConverterFactory();
Expand Down Expand Up @@ -90,6 +95,9 @@ public DefaultFormatterFactory() {
FormatterCreator jasperCreator = JasperFormatter::new;
formattersMap.put("jasper", jasperCreator);
formattersMap.put("jrxml", jasperCreator);

if (inlinersProvider == null)
setDefaultInlinersProvider();
}

public void setOfficeIntegration(OfficeIntegrationAPI officeIntegrationAPI) {
Expand All @@ -113,6 +121,10 @@ public void setFontsDirectory(String fontsDirectory) {
this.fontsDirectory = fontsDirectory;
}

public void setInlinersProvider(ReportInlinersProvider inlinersProvider) {
this.inlinersProvider = inlinersProvider;
}

public HtmlToPdfConverterFactory getHtmlToPdfConverterFactory() {
return htmlToPdfConverterFactory;
}
Expand All @@ -132,7 +144,16 @@ public ReportFormatter createFormatter(FormatterFactoryInput factoryInput) {
throw new UnsupportedFormatException(String.format("Unsupported template extension [%s]", templateExtension));
}

return formatterCreator.create(factoryInput);
ReportFormatter reportFormatter = formatterCreator.create(factoryInput);
if (reportFormatter instanceof AbstractFormatter) {
((AbstractFormatter) reportFormatter).setContentInliners(new ArrayList<>(inlinersProvider.getContentInliners()));
}

return reportFormatter;
}

protected void setDefaultInlinersProvider() {
inlinersProvider = new DefaultInlinersProvider();
}

protected static interface FormatterCreator {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2008-2018 Haulmont.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.haulmont.yarg.formatters.factory.inline;

import com.haulmont.yarg.formatters.impl.inline.BitmapContentInliner;
import com.haulmont.yarg.formatters.impl.inline.ContentInliner;
import com.haulmont.yarg.formatters.impl.inline.HtmlContentInliner;
import com.haulmont.yarg.formatters.impl.inline.ImageContentInliner;

import java.util.ArrayList;
import java.util.List;

public class DefaultInlinersProvider implements ReportInlinersProvider{
protected List<ContentInliner> contentInliners;

public DefaultInlinersProvider() {
this.contentInliners = new ArrayList<>();
addInliner(new BitmapContentInliner());
addInliner(new HtmlContentInliner());
addInliner(new ImageContentInliner());
}

public void addInliner(ContentInliner inliner){
this.contentInliners.add(inliner);
}

public void removeInliner(ContentInliner inliner){
this.contentInliners.remove(inliner);
}

public List<ContentInliner> getContentInliners() {
return contentInliners;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2008-2018 Haulmont.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.haulmont.yarg.formatters.factory.inline;

import com.haulmont.yarg.formatters.impl.inline.ContentInliner;

import java.util.List;

public interface ReportInlinersProvider {
List<ContentInliner> getContentInliners();

void addInliner(ContentInliner inliner);

void removeInliner(ContentInliner inliner);
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ protected AbstractFormatter(FormatterFactoryInput formatterFactoryInput) {
this.outputType = (formatterFactoryInput.getOutputType() != null)
? formatterFactoryInput.getOutputType() : reportTemplate.getOutputType();
this.outputStream = formatterFactoryInput.getOutputStream();

this.contentInliners.add(new BitmapContentInliner());
this.contentInliners.add(new HtmlContentInliner());
this.contentInliners.add(new ImageContentInliner());
}

@Override
Expand Down

0 comments on commit d48155b

Please sign in to comment.