en futuras entregas mejorare la herramienta para que pueda comparar archivos de office
y también hacer una super búsqueda en varios archivos de office
Código
package office; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.StringReader; import java.io.StringWriter; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.Enumeration; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.Set; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.Stack; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import javax.xml.XMLConstants; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import org.w3c.dom.Node; import org.w3c.dom.bootstrap.DOMImplementationRegistry; import org.w3c.dom.ls.DOMImplementationLS; import org.w3c.dom.ls.LSSerializer; import org.xml.sax.InputSource; import javafx.application.Application; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.event.ActionEvent; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Alert; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.Menu; import javafx.scene.control.MenuBar; import javafx.scene.control.MenuItem; import javafx.scene.control.TextArea; import javafx.scene.control.TextField; import javafx.scene.control.TreeItem; import javafx.scene.control.TreeView; import javafx.scene.control.Alert.AlertType; import javafx.scene.layout.Background; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.stage.FileChooser; import javafx.stage.FileChooser.ExtensionFilter; import javafx.stage.Stage; import java.util.Collection; import java.util.Collections; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.fxmisc.flowless.VirtualizedScrollPane; import org.fxmisc.richtext.CodeArea; import org.fxmisc.richtext.LineNumberFactory; import org.fxmisc.richtext.model.StyleSpans; import org.fxmisc.richtext.model.StyleSpansBuilder; public class TreeViewExample extends Application { Stage primaryStage; BorderPane root; Map<String, TreeItem> map; Map<String, String> inmemory; CodeArea textArea = new CodeArea(); private static final Pattern XML_TAG = Pattern.compile("(?<ELEMENT>(</?\\h*)(\\w+)([^<>]*)(\\h*/?>))" +"|(?<COMMENT><!--[^<>]+-->)"); private static final Pattern ATTRIBUTES = Pattern.compile("(\\w+\\h*)(=)(\\h*\"[^\"]+\")"); private static final int GROUP_OPEN_BRACKET = 2; private static final int GROUP_ELEMENT_NAME = 3; private static final int GROUP_ATTRIBUTES_SECTION = 4; private static final int GROUP_CLOSE_BRACKET = 5; private static final int GROUP_ATTRIBUTE_NAME = 1; private static final int GROUP_EQUAL_SYMBOL = 2; private static final int GROUP_ATTRIBUTE_VALUE = 3; Application.launch(args); } textArea.clear(); inmemory=new HashMap<String, String>(); FileChooser fileChooser = new FileChooser(); fileChooser.setTitle("selecione el excel"); fileChooser.getExtensionFilters().addAll(new ExtensionFilter("Excel", "*.xlsx"), new ExtensionFilter("Doc", "*.docx"), new ExtensionFilter("Ppt", "*..pptx")); if (file != null) { label.setText(file.getAbsolutePath()); Enumeration<?> enu = zipFile.entries(); Set<String> nodes = new HashSet<>(); while (enu.hasMoreElements()) { inmemory.put(name+"/", convertToString(inputStream)); inputStream.close(); node += current + "/"; nodes.add(node); } } List<String> listnodes = new ArrayList<String>(nodes); TreeItem rootItem = root(listnodes, file.getName()); TreeView treeView = new TreeView(); treeView.getSelectionModel().selectedItemProperty().addListener(this::selectedFile); treeView.setRoot(rootItem); treeView.setShowRoot(true); root.setLeft(treeView); Alert alert = new Alert(AlertType.ERROR); alert.setContentText(e.getMessage()); e.printStackTrace(); alert.showAndWait(); } } } Scanner scanner = new Scanner(in); scanner.useDelimiter("\\A"); boolean hasInput = scanner.hasNext(); if (hasInput) { return scanner.next(); } else { return null; } } TreeItem<String> selectedItem = (TreeItem<String>) newValue; if(map.get(current)==selectedItem) { if(text!=null) { textArea.replaceText(prettyFormat(text)); } } } } if (result == 0) result = o1.compareTo(o2); return result; } TreeItem rootItem = new TreeItem(name); rootItem.setExpanded(true); map = new LinkedHashMap<String, TreeItem>(); map.put(current, new TreeItem(split[split.length - 1])); } if (key.split("/", -1).length == 2) { rootItem.getChildren().add(map.get(key)); } else { TreeItem parent = map.get(result); parent.setExpanded(true); parent.getChildren().add(map.get(key)); } } return rootItem; } try { final Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src).getDocumentElement(); //May need this: System.setProperty(DOMImplementationRegistry.PROPERTY,"com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl"); final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); final LSSerializer writer = impl.createLSSerializer(); writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); // Set this to true if the output needs to be beautified. writer.getDomConfig().setParameter("xml-declaration", keepDeclaration); // Set this to true if the declaration is needed to be outputted. return writer.writeToString(document); } } @Override public void start(Stage primaryStage) { label.setEditable(false); label.setStyle("-fx-background-color:#ced1d6;"); textArea.setParagraphGraphicFactory(LineNumberFactory.get(textArea)); this.primaryStage = primaryStage; open.setOnAction(this::open); m.getItems().add(open); mb.getMenus().add(m); root = new BorderPane(); VBox head = new VBox(); head.getChildren().add(mb); head.getChildren().add(label); root.setTop(head); textArea.setEditable(false); textArea.textProperty().addListener((obs, oldText, newText) -> { textArea.setStyleSpans(0, computeHighlighting(newText)); }); root.setCenter(new VirtualizedScrollPane<>(textArea)); Scene scene = new Scene(root, 550, 250); scene.getStylesheets().add(TreeViewExample.class.getResource("xml-highlighting.css").toExternalForm()); primaryStage.setTitle("OpenXml Comparator"); primaryStage.setScene(scene); // primaryStage.setMaximized(true); primaryStage.show(); } Matcher matcher = XML_TAG.matcher(text); int lastKwEnd = 0; StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>(); while(matcher.find()) { if(matcher.group("COMMENT") != null) { } else { if(matcher.group("ELEMENT") != null) { spansBuilder.add(Collections.singleton("tagmark"), matcher.end(GROUP_OPEN_BRACKET) - matcher.start(GROUP_OPEN_BRACKET)); spansBuilder.add(Collections.singleton("anytag"), matcher.end(GROUP_ELEMENT_NAME) - matcher.end(GROUP_OPEN_BRACKET)); if(!attributesText.isEmpty()) { lastKwEnd = 0; Matcher amatcher = ATTRIBUTES.matcher(attributesText); while(amatcher.find()) { spansBuilder.add(Collections.singleton("attribute"), amatcher.end(GROUP_ATTRIBUTE_NAME) - amatcher.start(GROUP_ATTRIBUTE_NAME)); spansBuilder.add(Collections.singleton("tagmark"), amatcher.end(GROUP_EQUAL_SYMBOL) - amatcher.end(GROUP_ATTRIBUTE_NAME)); spansBuilder.add(Collections.singleton("avalue"), amatcher.end(GROUP_ATTRIBUTE_VALUE) - amatcher.end(GROUP_EQUAL_SYMBOL)); lastKwEnd = amatcher.end(); } if(attributesText.length() > lastKwEnd) } lastKwEnd = matcher.end(GROUP_ATTRIBUTES_SECTION); } } lastKwEnd = matcher.end(); } return spansBuilder.create(); } }
Código
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>tool</groupId> <artifactId>office</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.fxmisc.richtext</groupId> <artifactId>richtextfx</artifactId> <version>0.10.6 </version> </dependency> </dependencies> </project>
Código
.tagmark { -fx-fill: gray; } .anytag { -fx-fill: crimson; } .paren { -fx-fill: firebrick; -fx-font-weight: bold; } .attribute { -fx-fill: darkviolet; } .avalue { -fx-fill: black; } .comment { -fx-fill: teal; } .red-text { -fx-font-color: red; } .red-green { -fx-font-color: blue; } .styleClassName { -fx-background-color:gray; }