OfficerBreaker/XMLParser.java

65 lines
2.5 KiB
Java
Raw Normal View History

2022-03-11 21:27:08 -03:00
package officerbreaker;
2022-02-26 17:22:07 -03:00
import java.io.File;
import java.io.FileInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class XMLParser {
2022-03-01 08:48:51 -03:00
private FileManipulator manipulator;
private String filePath;
2022-03-01 08:48:51 -03:00
private final String targetElement; // hashed password element
2022-02-26 17:22:07 -03:00
private Element securityElement;
private Document document;
2022-03-01 08:48:51 -03:00
public XMLParser(FileManipulator manipulator) throws Exception {
this.manipulator = manipulator;
this.filePath = manipulator.getXMLTargetFile();
this.targetElement = manipulator.getSecurityElement();
this.document = XMLFileToDocument();
2022-03-01 08:48:51 -03:00
// get the element targetElement (the hashed password element):
this.securityElement = (Element) document.getElementsByTagName(targetElement).item(0);
}
public Document XMLFileToDocument() throws Exception {
DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
return documentBuilder.parse(new FileInputStream(new File(filePath)));
}
public void removeElementOfSecurity() {
// remove the node:
2022-02-26 17:22:07 -03:00
if (this.securityElement != null) { // if element == null it means there is no hashed pass
this.securityElement.getParentNode().removeChild(this.securityElement);
2022-02-25 18:44:56 -03:00
}
2022-03-01 08:48:51 -03:00
/*
if excel file, remove fileSharing and element of security from each sheet
*/
}
2022-03-01 08:48:51 -03:00
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);
2022-03-01 08:48:51 -03:00
StreamResult streamResult = new StreamResult(new File(this.filePath));
2022-02-26 17:22:07 -03:00
// transform the XML source to file:
transformer.transform(dom, streamResult);
}
2022-02-26 17:22:07 -03:00
public boolean isExistSecurityElement() {
// if the element was returned as null during instantiation, it means it doesn't exist
return (this.securityElement != null);
}
}