Skip to content

Commit

Permalink
generalized code to pptx/docx/xlsx
Browse files Browse the repository at this point in the history
  • Loading branch information
nedlir committed Mar 1, 2022
1 parent 20a3ab6 commit 7cbdb3e
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 18 deletions.
81 changes: 70 additions & 11 deletions FileManipulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,57 @@

public class FileManipulator {

private String fileName; // file which will be extracted from
public final static String EXCEL_FILE = "xlsx";
public final static String POWER_POINT_FILE = "pptx";
public final static String WORD_FILE = "pptx";

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 fileName, String extractionPath) {
this.fileName = fileName;
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);

Expand All @@ -51,4 +74,40 @@ public void insertFile(String targetFile) throws Exception {
}
}

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;
}
}
21 changes: 14 additions & 7 deletions XMLParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@

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 {
Expand All @@ -36,14 +39,18 @@ public void removeElementOfSecurity() {
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);
Expand Down

0 comments on commit 7cbdb3e

Please sign in to comment.