* Bug #13854 fixed - SciNotes did not initialize a new document at startup. 66/16466/3
Clément DAVID [Mon, 11 May 2015 08:20:43 +0000 (10:20 +0200)]
This commit introduce ScilabXPathFactory that enforce the use of the
OpenJDK provided XPathFactory. This ensure that loading saxon.jar or any
other XPathFactory provider did not interact poorly with Scilab usage.

Change-Id: Iffc5d7e79bd1dd1dc448c13f7536e04201d7b3d6

scilab/CHANGES_5.5.X
scilab/modules/commons/src/java/org/scilab/modules/commons/xml/ScilabXPathFactory.java [new file with mode: 0644]
scilab/modules/commons/src/java/org/scilab/modules/commons/xml/XConfiguration.java
scilab/modules/gui/src/java/org/scilab/modules/gui/utils/WindowsConfigurationManager.java
scilab/modules/jvm/src/java/org/scilab/modules/jvm/LoadClassPath.java
scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/SciNotesOptions.java
scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/ScilabContext.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/io/spec/XcosPackage.java

index a3e15bc..3193ae9 100644 (file)
@@ -6,6 +6,8 @@ Scilab Bug Fixes
 
 * Bug #13829 fixed - mean and sum functions returned wrong results for hypermatrices.
 
+* Bug #13854 fixed - On some operating systems, SciNotes did not initialize a new document at startup.
+
 
 
                      Changes between version 5.5.1 and 5.5.2
diff --git a/scilab/modules/commons/src/java/org/scilab/modules/commons/xml/ScilabXPathFactory.java b/scilab/modules/commons/src/java/org/scilab/modules/commons/xml/ScilabXPathFactory.java
new file mode 100644 (file)
index 0000000..dbfc89d
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+ * Copyright (C) 2015 - Scilab Enteprises - Clement DAVID
+ *
+ * This file must be used under the terms of the CeCILL.
+ * This source file is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution.  The terms
+ * are also available at
+ * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
+ *
+ */
+
+package org.scilab.modules.commons.xml;
+
+import javax.xml.xpath.XPathFactory;
+
+/**
+ * Class to provide a way to be sure that the default PathFactory provided by Java framework will be used.
+ */
+public class ScilabXPathFactory {
+
+    private static final String XPATHFACTORYPROPERTY = "javax.xml.xpath.XPathFactory";
+    private static final String XPATHFACTORYIMPL = "com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl";
+
+    /**
+     * Use default internal XPathFactoryImpl
+     * @return the key corresponding to the currently used implementation
+     */
+    public static String useDefaultTransformerFactoryImpl() {
+        String factory = System.getProperty(XPATHFACTORYPROPERTY);
+        System.setProperty(XPATHFACTORYPROPERTY, XPATHFACTORYIMPL);
+
+        return factory;
+    }
+
+    /**
+     * Restore the previous factory
+     * @param factory the key as returned by useDefaultPathFactoryImpl
+     */
+    public static void restoreTransformerFactoryImpl(String factory) {
+        if (factory == null) {
+            System.clearProperty(XPATHFACTORYPROPERTY);
+        } else {
+            System.setProperty(XPATHFACTORYPROPERTY, factory);
+        }
+    }
+
+    /**
+     * @return a new instance using the default XPathFactory implementation
+     */
+    public static XPathFactory newInstance() {
+        String factory = useDefaultTransformerFactoryImpl();
+        XPathFactory xpf = XPathFactory.newInstance();
+        restoreTransformerFactoryImpl(factory);
+
+        return xpf;
+    }
+}
index 1c6c8f0..41dfc62 100644 (file)
@@ -79,7 +79,7 @@ public class XConfiguration {
     private static final String SEVERE_ERROR = Messages.gettext("A severe error occurred: cannot load the preferences file.");
     private static final String PARSING_ERROR = Messages.gettext("An error occurred when loading the preferences file, try to reload the default one.");
 
-    private static final XPathFactory xpathFactory = XPathFactory.newInstance();
+    private static final XPathFactory xpathFactory = ScilabXPathFactory.newInstance();
     private static final Map < Class<?>, StringParser > conv = new HashMap < Class<?>, StringParser > ();
 
     private static final EventListenerList listenerList = new EventListenerList();
index 1657d7a..babdebd 100644 (file)
@@ -18,7 +18,6 @@ import java.awt.GraphicsEnvironment;
 import java.awt.Point;
 import java.awt.Rectangle;
 import java.io.File;
-import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -32,7 +31,6 @@ import java.util.UUID;
 import javax.swing.SwingUtilities;
 import javax.xml.xpath.XPath;
 import javax.xml.xpath.XPathConstants;
-import javax.xml.xpath.XPathFactory;
 
 import org.flexdock.docking.Dockable;
 import org.flexdock.docking.DockingManager;
@@ -42,11 +40,14 @@ import org.flexdock.perspective.persist.xml.LayoutNodeSerializer;
 import org.flexdock.perspective.persist.xml.PersistenceConstants;
 import org.scilab.modules.commons.ScilabCommonsUtils;
 import org.scilab.modules.commons.ScilabConstants;
+import org.scilab.modules.commons.xml.ScilabXPathFactory;
 import org.scilab.modules.commons.xml.ScilabXMLUtilities;
 import org.scilab.modules.commons.xml.XConfiguration;
 import org.scilab.modules.commons.xml.XConfigurationEvent;
 import org.scilab.modules.commons.xml.XConfigurationListener;
+
 import static org.scilab.modules.commons.xml.XConfiguration.XConfAttribute;
+
 import org.scilab.modules.gui.bridge.tab.SwingScilabDockablePanel;
 import org.scilab.modules.gui.bridge.window.SwingScilabWindow;
 import org.scilab.modules.gui.console.ScilabConsole;
@@ -167,7 +168,7 @@ public class WindowsConfigurationManager implements XConfigurationListener {
         try {
             Document doc = XConfiguration.getXConfigurationDocument();
             if (doc != null) {
-                XPath xp = XPathFactory.newInstance().newXPath();
+                XPath xp = ScilabXPathFactory.newInstance().newXPath();
                 NodeList nodes = (NodeList) xp.compile(LAYOUT_PATH + "/layout[@id=../@id]/@path").evaluate(doc, XPathConstants.NODESET);
                 if (nodes != null && nodes.getLength() > 0) {
                     return nodes.item(0).getNodeValue().replace("$SCI", System.getenv(SCI));
@@ -869,7 +870,7 @@ public class WindowsConfigurationManager implements XConfigurationListener {
     private static final void validateWindows() {
         // We remove all the blanks and carriage return
         try {
-            XPath xp = XPathFactory.newInstance().newXPath();
+            XPath xp = ScilabXPathFactory.newInstance().newXPath();
             NodeList nodes = (NodeList) xp.compile("//text()").evaluate(doc, XPathConstants.NODESET);
             for (int i = 0; i < nodes.getLength(); i++) {
                 nodes.item(i).getParentNode().removeChild(nodes.item(i));
index 5649ef6..404218c 100644 (file)
@@ -30,8 +30,9 @@ import org.w3c.dom.Document;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 import org.xml.sax.SAXException;
-
 import org.scilab.modules.commons.xml.ScilabDocumentBuilderFactory;
+import org.scilab.modules.commons.xml.ScilabXPathFactory;
+import org.scilab.modules.commons.xml.ScilabXMLUtilities;
 
 /**
  * Utility class to ease the jar loading management.
@@ -63,7 +64,7 @@ public final class LoadClassPath {
         String xpathExpression = XPATH_EXPRS + "[@on='" + module + "']";
 
         // Initialize xpath
-        XPathFactory factory = XPathFactory.newInstance();
+        XPathFactory factory = ScilabXPathFactory.newInstance();
         XPath xpath = factory.newXPath();
 
         // initialize document factory
index 1b04857..ac572c1 100644 (file)
@@ -22,9 +22,11 @@ import javax.xml.xpath.XPathExpressionException;
 import javax.xml.xpath.XPathFactory;
 
 import org.w3c.dom.Document;
-
 import org.scilab.modules.commons.OS;
+
 import static org.scilab.modules.commons.xml.XConfiguration.XConfAttribute;
+
+import org.scilab.modules.commons.xml.ScilabXPathFactory;
 import org.scilab.modules.commons.xml.XConfiguration;
 
 /**
@@ -71,12 +73,12 @@ public class SciNotesOptions {
 
             if (eol.equals("")) {
                 switch (OS.get()) {
-                case WINDOWS:
-                    this.eol = ScilabDocument.EOLWIN;
-                    break;
-                default:
-                    this.eol = ScilabDocument.EOLUNIX;
-                    break;
+                    case WINDOWS:
+                        this.eol = ScilabDocument.EOLWIN;
+                        break;
+                    default:
+                        this.eol = ScilabDocument.EOLUNIX;
+                        break;
                 }
             } else if (eol.startsWith("Windows")) {
                 this.eol = ScilabDocument.EOLWIN;
@@ -212,7 +214,7 @@ public class SciNotesOptions {
         private void set(boolean enable) {
             this.enable = enable;
             if (enable) {
-                XPathFactory xpathFactory = XPathFactory.newInstance();
+                XPathFactory xpathFactory = ScilabXPathFactory.newInstance();
                 XPath xp = xpathFactory.newXPath();
                 try {
                     header = (String) xp.compile("string(" + HEADERPATH + ")").evaluate(doc, XPathConstants.STRING);
index 641993a..0fca867 100644 (file)
@@ -16,10 +16,7 @@ package org.scilab.modules.scinotes;
 import java.awt.Color;
 import java.awt.Font;
 import java.util.ArrayList;
-import java.util.Iterator;
 import java.util.List;
-import java.util.Map;
-import java.util.Set;
 
 import javax.swing.text.ViewFactory;
 import javax.swing.text.View;
@@ -34,6 +31,7 @@ import org.w3c.dom.NodeList;
 
 import static org.scilab.modules.commons.xml.XConfiguration.XConfAttribute;
 
+import org.scilab.modules.commons.xml.ScilabXPathFactory;
 import org.scilab.modules.commons.xml.XConfiguration;
 import org.scilab.modules.gui.utils.ScilabFontUtils;
 
@@ -92,7 +90,7 @@ public class ScilabContext implements ViewFactory {
 
 
     public static void saveFont(Font font) {
-        XPathFactory xpathFactory = XPathFactory.newInstance();
+        XPathFactory xpathFactory = ScilabXPathFactory.newInstance();
         XPath xp = xpathFactory.newXPath();
         NodeList nodes;
         Document doc = XConfiguration.getXConfigurationDocument();
index a1c4600..14b54e7 100644 (file)
@@ -43,6 +43,7 @@ import javax.xml.xpath.XPathFactory;
 
 import org.scilab.modules.commons.xml.ScilabDocumentBuilderFactory;
 import org.scilab.modules.commons.xml.ScilabTransformerFactory;
+import org.scilab.modules.commons.xml.ScilabXPathFactory;
 import org.scilab.modules.types.ScilabList;
 import org.scilab.modules.xcos.graph.XcosDiagram;
 import org.w3c.dom.Document;
@@ -397,7 +398,7 @@ public class XcosPackage {
     public String getPackageVersion() {
         // cache the xpath expression
         if (XPATH_VERSION == null) {
-            final XPathFactory factory = XPathFactory.newInstance();
+            final XPathFactory factory = ScilabXPathFactory.newInstance();
             final XPath xpath = factory.newXPath();
 
             try {