generalized code to pptx/docx/xlsx
This commit is contained in:
parent
20a3ab6d73
commit
7cbdb3ecff
|
|
@ -15,34 +15,57 @@ import java.util.zip.ZipFile;
|
|||
|
||||
public class FileManipulator {
|
||||
|
||||
private String fileName; // file which will be extracted from
|
||||
private String extractionPath; // temporary path where the file will be extracted to
|
||||
public final static String EXCEL_FILE = "xlsx";
|
||||
public final static String POWER_POINT_FILE = "pptx";
|
||||
public final static String WORD_FILE = "pptx";
|
||||
|
||||
public FileManipulator(String fileName, String extractionPath) {
|
||||
this.fileName = fileName;
|
||||
public final static String EXCEL_XML = "workbook.xml";
|
||||
public final static String POWER_POINT_XML = "presentation.xml";
|
||||
public final static String WORD_XML = "settings.xml";
|
||||
|
||||
public final static String EXCEL_PATH = "xl/";
|
||||
public final static String POWER_POINT_PATH = "ppt/";
|
||||
public final static String WORD_PATH = "word/";
|
||||
|
||||
public final static String EXCEL_SECURITY_NODE = "workbookProtection";
|
||||
public final static String POWER_POINT_SECURITY_NODE = "p:modifyVerifier";
|
||||
public final static String WORD_SECURITY_NODE = "w:documentProtection";
|
||||
|
||||
private String filePath; // path of file which will be manipulated
|
||||
private String extractionPath; // temporary path where the file will be extracted to
|
||||
private String fileType; // file type (extension name)
|
||||
private String XMLTargetFile; // XML file to be extracted from file
|
||||
private String targetPath; // path to XML inside the file
|
||||
private String securityElement; // hashed password element
|
||||
|
||||
public FileManipulator(String filePath, String extractionPath) throws Exception {
|
||||
this.filePath = filePath;
|
||||
this.extractionPath = extractionPath;
|
||||
this.fileType = determineFileType();
|
||||
locateTargetFileAndPath();
|
||||
|
||||
}
|
||||
|
||||
public void extractFile(String targetFile) throws Exception {
|
||||
File outputLocation = new File(extractionPath, targetFile);
|
||||
public void extractFile() throws Exception {
|
||||
File outputLocation = new File(this.extractionPath, this.XMLTargetFile);
|
||||
|
||||
// path to the file the file will be extracted from
|
||||
Path zipFile = Paths.get(fileName);
|
||||
Path zipFile = Paths.get(this.filePath);
|
||||
|
||||
// load zip file as filesystem
|
||||
FileSystem fileSystem = FileSystems.newFileSystem(zipFile, null);
|
||||
|
||||
Path source = fileSystem.getPath("ppt/" + targetFile); // location of targetFile inside the zip file
|
||||
Path source = fileSystem.getPath(this.targetPath + this.XMLTargetFile); // location of XMLTargetFile inside the zip file
|
||||
|
||||
Files.copy(source, outputLocation.toPath());
|
||||
}
|
||||
|
||||
public void insertFile(String targetFile) throws Exception {
|
||||
Path myFilePath = Paths.get(targetFile);
|
||||
public void insertFile() throws Exception {
|
||||
Path myFilePath = Paths.get(this.XMLTargetFile);
|
||||
|
||||
Path zipFilePath = Paths.get(this.fileName);
|
||||
Path zipFilePath = Paths.get(this.filePath);
|
||||
try (FileSystem fs = FileSystems.newFileSystem(zipFilePath, null)) {
|
||||
Path fileInsideZipPath = fs.getPath("/ppt/" + targetFile);
|
||||
Path fileInsideZipPath = fs.getPath(this.targetPath + this.XMLTargetFile);
|
||||
Files.delete(fileInsideZipPath);
|
||||
Files.copy(myFilePath, fileInsideZipPath);
|
||||
|
||||
|
|
@ -51,4 +74,40 @@ public class FileManipulator {
|
|||
}
|
||||
}
|
||||
|
||||
public String determineFileType() throws Exception {
|
||||
int indexOfLastDot = this.filePath.lastIndexOf(".");
|
||||
if (indexOfLastDot == -1) // dot not found, invalid file
|
||||
{
|
||||
throw new Exception("invalid input, file extension not found.");
|
||||
} else {
|
||||
return filePath.substring(indexOfLastDot + 1);
|
||||
}
|
||||
}
|
||||
|
||||
public void locateTargetFileAndPath() throws Exception {
|
||||
if (fileType.equals(POWER_POINT_FILE)) {
|
||||
this.XMLTargetFile = POWER_POINT_XML;
|
||||
this.targetPath = POWER_POINT_PATH;
|
||||
this.securityElement = POWER_POINT_SECURITY_NODE;
|
||||
} else if (fileType.equals(WORD_FILE)) {
|
||||
this.XMLTargetFile = WORD_XML;
|
||||
this.targetPath = WORD_PATH;
|
||||
this.securityElement = WORD_SECURITY_NODE;
|
||||
} else if (fileType.equals(EXCEL_FILE)) {
|
||||
this.XMLTargetFile = EXCEL_XML;
|
||||
this.targetPath = EXCEL_PATH;
|
||||
this.securityElement = EXCEL_SECURITY_NODE;
|
||||
} else // file not recognized
|
||||
{
|
||||
throw new Exception("invalid input, file type not supported.");
|
||||
}
|
||||
}
|
||||
|
||||
public String getXMLTargetFile() {
|
||||
return this.XMLTargetFile;
|
||||
}
|
||||
|
||||
public String getSecurityElement() {
|
||||
return this.securityElement;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,17 +12,20 @@ import org.w3c.dom.Element;
|
|||
|
||||
public class XMLParser {
|
||||
|
||||
private final String TARGET_ELEMENT = "p:modifyVerifier"; // hashed password element
|
||||
private FileManipulator manipulator;
|
||||
private String filePath;
|
||||
private final String targetElement; // hashed password element
|
||||
private Element securityElement;
|
||||
|
||||
private Document document;
|
||||
|
||||
public XMLParser(String filePath) throws Exception {
|
||||
this.filePath = filePath;
|
||||
public XMLParser(FileManipulator manipulator) throws Exception {
|
||||
this.manipulator = manipulator;
|
||||
this.filePath = manipulator.getXMLTargetFile();
|
||||
this.targetElement = manipulator.getSecurityElement();
|
||||
this.document = XMLFileToDocument();
|
||||
// get the element TARGET_ELEMENT (the hashed password element):
|
||||
this.securityElement = (Element) document.getElementsByTagName(TARGET_ELEMENT).item(0);
|
||||
// get the element targetElement (the hashed password element):
|
||||
this.securityElement = (Element) document.getElementsByTagName(targetElement).item(0);
|
||||
}
|
||||
|
||||
public Document XMLFileToDocument() throws Exception {
|
||||
|
|
@ -36,14 +39,18 @@ public class XMLParser {
|
|||
if (this.securityElement != null) { // if element == null it means there is no hashed pass
|
||||
this.securityElement.getParentNode().removeChild(this.securityElement);
|
||||
}
|
||||
|
||||
/*
|
||||
if excel file, remove fileSharing and element of security from each sheet
|
||||
*/
|
||||
}
|
||||
|
||||
public void writeToXMLFile(String newFilePath) throws Exception {
|
||||
public void writeToXMLFile() throws Exception {
|
||||
// create transformer from document to xml file:
|
||||
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
||||
Transformer transformer = transformerFactory.newTransformer();
|
||||
DOMSource dom = new DOMSource(this.document);
|
||||
StreamResult streamResult = new StreamResult(new File(newFilePath));
|
||||
StreamResult streamResult = new StreamResult(new File(this.filePath));
|
||||
|
||||
// transform the XML source to file:
|
||||
transformer.transform(dom, streamResult);
|
||||
|
|
|
|||
Loading…
Reference in New Issue