-
-
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
16 changed files
with
417 additions
and
78 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
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
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,80 @@ | ||
package com.poiji.bind.mapping; | ||
|
||
import com.poiji.annotation.ExcelProperty; | ||
import com.poiji.util.ReflectUtil; | ||
import org.apache.poi.ooxml.POIXMLProperties; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.stream.Stream; | ||
|
||
public final class PropertyHandler { | ||
|
||
public static <T> T unmarshal(Class<T> type, POIXMLProperties poixmlProperties) { | ||
|
||
T unmarshalledObject = ReflectUtil.newInstanceOf(type); | ||
|
||
Stream.of(type.getDeclaredFields()) | ||
.filter(field -> field.getAnnotation(ExcelProperty.class) != null) | ||
.forEach(excelPropertyField -> { | ||
String propertyName = getPropertyName(excelPropertyField); | ||
|
||
setPropertyValueOnTarget(propertyName, poixmlProperties, excelPropertyField, unmarshalledObject); | ||
}); | ||
|
||
return unmarshalledObject; | ||
} | ||
|
||
private static String getPropertyName(Field excelPropertyField) { | ||
String propertyName = excelPropertyField.getAnnotation(ExcelProperty.class).propertyName(); | ||
|
||
if (propertyName.isEmpty()) { | ||
propertyName = excelPropertyField.getName(); | ||
} | ||
|
||
return propertyName; | ||
} | ||
|
||
private static void setPropertyValueOnTarget(String propertyName, POIXMLProperties poixmlProperties, Field targetField, Object targetObject) { | ||
switch (propertyName) { | ||
case "category": | ||
ReflectUtil.setFieldData(targetField, poixmlProperties.getCoreProperties().getCategory(), targetObject); | ||
break; | ||
case "contentStatus": | ||
ReflectUtil.setFieldData(targetField, poixmlProperties.getCoreProperties().getContentStatus(), targetObject); | ||
break; | ||
case "created": | ||
ReflectUtil.setFieldData(targetField, poixmlProperties.getCoreProperties().getCreated(), targetObject); | ||
break; | ||
case "creator": | ||
ReflectUtil.setFieldData(targetField, poixmlProperties.getCoreProperties().getCreator(), targetObject); | ||
break; | ||
case "description": | ||
ReflectUtil.setFieldData(targetField, poixmlProperties.getCoreProperties().getDescription(), targetObject); | ||
break; | ||
case "keywords": | ||
ReflectUtil.setFieldData(targetField, poixmlProperties.getCoreProperties().getKeywords(), targetObject); | ||
break; | ||
case "lastPrinted": | ||
ReflectUtil.setFieldData(targetField, poixmlProperties.getCoreProperties().getLastPrinted(), targetObject); | ||
break; | ||
case "modified": | ||
ReflectUtil.setFieldData(targetField, poixmlProperties.getCoreProperties().getModified(), targetObject); | ||
break; | ||
case "subject": | ||
ReflectUtil.setFieldData(targetField, poixmlProperties.getCoreProperties().getSubject(), targetObject); | ||
break; | ||
case "title": | ||
ReflectUtil.setFieldData(targetField, poixmlProperties.getCoreProperties().getTitle(), targetObject); | ||
break; | ||
case "revision": | ||
ReflectUtil.setFieldData(targetField, poixmlProperties.getCoreProperties().getRevision(), targetObject); | ||
break; | ||
default: | ||
if (poixmlProperties.getCustomProperties().getProperty(propertyName) != null) { | ||
ReflectUtil.setFieldData(targetField, poixmlProperties.getCustomProperties().getProperty(propertyName).getLpwstr(), targetObject); | ||
} | ||
|
||
break; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.