-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core): re-organize core code
- Loading branch information
Showing
7 changed files
with
489 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package dev.inaka.common; | ||
|
||
/** | ||
* PdfFormat is an enumeration of various PDF formats for document conversion. | ||
*/ | ||
public enum EmulatedMediaType { | ||
/** | ||
* screen media type | ||
*/ | ||
SCREEN("screen"), | ||
|
||
/** | ||
* print media type | ||
*/ | ||
PRINT("print"); | ||
|
||
private final String mediaType; | ||
|
||
/** | ||
* Constructs a EmulatedMediaType enum with the specified format string. | ||
* | ||
* @param mediaType The emulated media type string. | ||
*/ | ||
EmulatedMediaType(String mediaType) { | ||
this.mediaType = mediaType; | ||
} | ||
|
||
/** | ||
* Gets the emulated media type string for this EmulatedMediaType. | ||
* | ||
* @return The emulated media type string. | ||
*/ | ||
public String mediaType() { | ||
return this.mediaType; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/dev/inaka/common/exceptions/FooterFileNotFoundExceptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package dev.inaka.common.exceptions; | ||
|
||
import java.io.FileNotFoundException; | ||
|
||
/** | ||
* FooterFileNotFoundExceptions is an exception class that is thrown when a footer.html file is not found. | ||
*/ | ||
public class FooterFileNotFoundExceptions extends FileNotFoundException { | ||
/** | ||
* Constructs a FooterFileNotFoundExceptions with a default error message. | ||
*/ | ||
public FooterFileNotFoundExceptions() { | ||
super("No footer.html file found."); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/dev/inaka/common/exceptions/HeaderFileNotFoundExceptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package dev.inaka.common.exceptions; | ||
|
||
import java.io.FileNotFoundException; | ||
|
||
/** | ||
* HeaderFileNotFoundExceptions is an exception class that is thrown when a header.html file is not found. | ||
*/ | ||
public class HeaderFileNotFoundExceptions extends FileNotFoundException { | ||
/** | ||
* Constructs a HeaderFileNotFoundExceptions with a default error message. | ||
*/ | ||
public HeaderFileNotFoundExceptions() { | ||
super("No header.html file found."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,5 @@ public IndexFileNotFoundExceptions() { | |
super("No index.html file found."); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package dev.inaka.core; | ||
|
||
import dev.inaka.Jotenberg; | ||
import dev.inaka.chromium.ChromiumOptions; | ||
import dev.inaka.chromium.ChromiumPageProperties; | ||
import dev.inaka.common.AbstractOptions; | ||
import dev.inaka.libreoffice.LibreOfficeOptions; | ||
import dev.inaka.libreoffice.LibreOfficePageProperties; | ||
import dev.inaka.pdfengines.PDFEnginesMergeOptions; | ||
import dev.inaka.pdfengines.PDFEnginesOptions; | ||
import dev.inaka.screenshots.ImageProperties; | ||
|
||
import java.io.File; | ||
import java.lang.reflect.Field; | ||
|
||
/** | ||
* ConversionHelper is a class that provides helper methods for converting between different formats. | ||
*/ | ||
public class ConversionHelper { | ||
private final Jotenberg jotenberg; | ||
|
||
public ConversionHelper(Jotenberg jotenberg) { | ||
this.jotenberg = jotenberg; | ||
} | ||
|
||
/** | ||
* Builds Chromium page properties using reflection and adds them to the request entity. | ||
* | ||
* @param pageProperties Chromium page properties to add to the request entity. | ||
*/ | ||
void buildPageProperties(ChromiumPageProperties pageProperties) { | ||
Field[] fields = ChromiumPageProperties.class.getDeclaredFields(); | ||
try { | ||
for (Field field : fields) { | ||
field.setAccessible(true); | ||
Object value = field.get(pageProperties); | ||
if (value != null) { | ||
jotenberg.getBuilder().addTextBody(field.getName(), (String) value); | ||
} | ||
} | ||
} catch (IllegalAccessException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
/** | ||
* Builds Chromium options using reflection and adds them to the request entity. | ||
* | ||
* @param options Chromium options to add to the request entity. | ||
*/ | ||
void buildChromiumOptions(AbstractOptions options) { | ||
Field[] fields = ChromiumOptions.class.getDeclaredFields(); | ||
try { | ||
for (Field field : fields) { | ||
field.setAccessible(true); | ||
Object value = field.get(options); | ||
if (value != null) { | ||
if (field.getDeclaringClass().equals(File.class)) { | ||
jotenberg.getBuilder().addBinaryBody(field.getName(), (File) value); | ||
} else { | ||
jotenberg.getBuilder().addTextBody(field.getName(), (String) value); | ||
} | ||
} | ||
} | ||
} catch (IllegalAccessException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
/** | ||
* Builds LibreOffice page properties using reflection and adds them to the request entity. | ||
* | ||
* @param pageProperties LibreOffice page properties to add to the request entity. | ||
*/ | ||
void buildPageProperties(LibreOfficePageProperties pageProperties) { | ||
Field[] fields = LibreOfficePageProperties.class.getDeclaredFields(); | ||
try { | ||
for (Field field : fields) { | ||
field.setAccessible(true); | ||
Object value = field.get(pageProperties); | ||
if (value != null) { | ||
jotenberg.getBuilder().addTextBody(field.getName(), (String) value); | ||
} | ||
} | ||
} catch (IllegalAccessException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
/** | ||
* Builds LibreOffice options using reflection and adds them to the request entity. | ||
* | ||
* @param options LibreOffice options to add to the request entity. | ||
*/ | ||
void buildPageOptions(LibreOfficeOptions options) { | ||
Field[] fields = LibreOfficeOptions.class.getDeclaredFields(); | ||
try { | ||
for (Field field : fields) { | ||
field.setAccessible(true); | ||
Object value = field.get(options); | ||
if (value != null) { | ||
jotenberg.getBuilder().addTextBody(field.getName(), (String) value); | ||
} | ||
} | ||
} catch (IllegalAccessException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
/** | ||
* Builds image properties using reflection and adds them to the request entity. | ||
* | ||
* @param imageProperties image properties to add to the request entity. | ||
*/ | ||
void buildImageProperties(ImageProperties imageProperties) { | ||
Field[] fields = ImageProperties.class.getDeclaredFields(); | ||
try { | ||
for (Field field : fields) { | ||
field.setAccessible(true); | ||
Object value = field.get(imageProperties); | ||
if (value != null) { | ||
jotenberg.getBuilder().addTextBody(field.getName(), (String) value); | ||
} | ||
} | ||
} catch (IllegalAccessException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Builds PDF engines options using reflection and adds them to the request entity. | ||
* | ||
* @param options PDF engines options to add to the request entity. | ||
*/ | ||
public void buildPdfEngineOptions(PDFEnginesOptions options) { | ||
Field[] fields = PDFEnginesMergeOptions.class.getDeclaredFields(); | ||
try { | ||
for (Field field : fields) { | ||
field.setAccessible(true); | ||
Object value = field.get(options); | ||
if (value != null) { | ||
jotenberg.getBuilder().addTextBody(field.getName(), (String) value); | ||
} | ||
} | ||
} catch (IllegalAccessException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
Oops, something went wrong.