-
-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
732 additions
and
130 deletions.
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
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,24 @@ | ||
package com.poiji.annotation; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Indicates, that a field contains an excel property (e.g. author, title, custom properties, ...) | ||
* | ||
* @author breucode | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.FIELD) | ||
@Documented | ||
public @interface ExcelProperty { | ||
/** | ||
* If this value is set, the property will be read from property field with this name instead of your field name. | ||
* | ||
* @return the name of the property field to read from | ||
*/ | ||
String propertyName() default ""; | ||
} |
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
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,13 @@ | ||
package com.poiji.bind; | ||
|
||
/** | ||
* Created by hakan on 25.05.2020 | ||
*/ | ||
public interface PropertyUnmarshaller { | ||
|
||
<T> T unmarshal(Class<T> type); | ||
|
||
<T> T returnFromExcelFile(Class<T> type); | ||
|
||
<T> T returnFromEncryptedFile(Class<T> type); | ||
} |
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
66 changes: 66 additions & 0 deletions
66
src/main/java/com/poiji/bind/mapping/HSSFPropertyFile.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,66 @@ | ||
package com.poiji.bind.mapping; | ||
|
||
import com.poiji.bind.PropertyUnmarshaller; | ||
import com.poiji.exception.PoijiException; | ||
import com.poiji.option.PoijiOptions; | ||
import org.apache.poi.openxml4j.exceptions.OpenXML4JException; | ||
import org.apache.poi.openxml4j.opc.OPCPackage; | ||
import org.apache.poi.openxml4j.opc.PackageAccess; | ||
import org.apache.poi.poifs.filesystem.DocumentFactoryHelper; | ||
import org.apache.poi.poifs.filesystem.POIFSFileSystem; | ||
import org.apache.poi.util.IOUtils; | ||
import org.apache.poi.xssf.usermodel.XSSFWorkbook; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* Created by hakan on 24.05.2020 | ||
*/ | ||
public final class HSSFPropertyFile implements PropertyUnmarshaller { | ||
|
||
private File file; | ||
private PoijiOptions options; | ||
|
||
HSSFPropertyFile(File file, PoijiOptions options) { | ||
this.file = file; | ||
this.options = options; | ||
} | ||
|
||
@Override | ||
public <T> T unmarshal(Class<T> type) { | ||
if (options.getPassword() != null) { | ||
return returnFromEncryptedFile(type); | ||
} | ||
return returnFromExcelFile(type); | ||
} | ||
|
||
@Override | ||
public <T> T returnFromExcelFile(Class<T> type) { | ||
try (OPCPackage open = OPCPackage.open(file, PackageAccess.READ)) { | ||
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(open); | ||
PropertyHandler propertyHandler = new PropertyHandler(); | ||
return propertyHandler.unmarshal(type, xssfWorkbook.getProperties()); | ||
} catch (IOException | OpenXML4JException e) { | ||
throw new PoijiException("Problem occurred while reading data", e); | ||
} | ||
} | ||
|
||
@Override | ||
public <T> T returnFromEncryptedFile(Class<T> type) { | ||
try (POIFSFileSystem fs = new POIFSFileSystem(file, true)) { | ||
InputStream stream = DocumentFactoryHelper.getDecryptedStream(fs, options.getPassword()); | ||
try (OPCPackage open = OPCPackage.open(stream)) { | ||
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(open); | ||
PropertyHandler propertyHandler = new PropertyHandler(); | ||
return propertyHandler.unmarshal(type, xssfWorkbook.getProperties()); | ||
} catch (IOException | OpenXML4JException e) { | ||
IOUtils.closeQuietly(fs); | ||
throw new PoijiException("Problem occurred while reading data", e); | ||
} | ||
} catch (IOException e) { | ||
throw new PoijiException("Problem occurred while reading data", e); | ||
} | ||
} | ||
} |
Oops, something went wrong.