poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kiwiwi...@apache.org
Subject svn commit: r1881669 [2/2] - in /xmlbeans/trunk/src/main/java/org/apache/xmlbeans: ./ impl/tool/ impl/util/ impl/values/
Date Sat, 12 Sep 2020 23:25:46 GMT
Modified: xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/tool/XSTCTester.java
URL: http://svn.apache.org/viewvc/xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/tool/XSTCTester.java?rev=1881669&r1=1881668&r2=1881669&view=diff
==============================================================================
--- xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/tool/XSTCTester.java (original)
+++ xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/tool/XSTCTester.java Sat Sep 12
23:25:46 2020
@@ -15,38 +15,26 @@
 
 package org.apache.xmlbeans.impl.tool;
 
-import org.apache.xmlbeans.SystemProperties;
-import org.apache.xmlbeans.impl.xb.ltgfmt.TestsDocument;
+import org.apache.xmlbeans.*;
 import org.apache.xmlbeans.impl.xb.ltgfmt.FileDesc;
-import org.apache.xmlbeans.XmlOptions;
-import org.apache.xmlbeans.XmlException;
-import org.apache.xmlbeans.XmlError;
-import org.apache.xmlbeans.XmlCalendar;
+import org.apache.xmlbeans.impl.xb.ltgfmt.TestsDocument;
 
 import java.io.File;
-import java.io.PrintWriter;
-import java.io.FileWriter;
 import java.io.IOException;
-import java.util.Collection;
-import java.util.ArrayList;
-import java.util.Set;
-import java.util.HashSet;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.Date;
-import java.util.regex.Pattern;
+import java.io.PrintWriter;
 import java.net.URI;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.util.*;
+import java.util.regex.Pattern;
 
-public class XSTCTester
-{
-    public static void printUsage()
-    {
+public class XSTCTester {
+    public static void printUsage() {
         System.out.println("Usage: xstc [-showpass] [-errcode] foo_LTGfmt.xml ...");
     }
 
-    public static void main(String[] args) throws IOException
-    {
-        Set flags = new HashSet();
+    public static void main(String[] args) throws IOException {
+        Set<String> flags = new HashSet<>();
         flags.add("h");
         flags.add("help");
         flags.add("usage");
@@ -55,27 +43,25 @@ public class XSTCTester
         flags.add("errcode");
 
         long start = System.currentTimeMillis();
-        
+
         CommandLine cl = new CommandLine(args, flags, Collections.EMPTY_SET);
-        if (cl.getOpt("h") != null || cl.getOpt("help") != null || cl.getOpt("usage") !=
null)
-        {
+        if (cl.getOpt("h") != null || cl.getOpt("help") != null || cl.getOpt("usage") !=
null) {
             printUsage();
             System.exit(0);
             return;
         }
 
-        if (cl.getOpt("version") != null)
-        {
+        if (cl.getOpt("version") != null) {
             CommandLine.printVersion();
             System.exit(0);
             return;
         }
 
         String[] badopts = cl.getBadOpts();
-        if (badopts.length > 0)
-        {
-            for (int i = 0; i < badopts.length; i++)
-                System.out.println("Unrecognized option: " + badopts[i]);
+        if (badopts.length > 0) {
+            for (String badopt : badopts) {
+                System.out.println("Unrecognized option: " + badopt);
+            }
             printUsage();
             System.exit(0);
             return;
@@ -90,103 +76,101 @@ public class XSTCTester
         boolean errcode = (cl.getOpt("errcode") != null);
 
         File[] allFiles = cl.getFiles();
-        Collection ltgFiles = new ArrayList();
+        Collection<File> ltgFiles = new ArrayList<>();
         Harness harness = new XMLBeanXSTCHarness();
-        
-        for (int i = 0; i < allFiles.length; i++)
-        {
-            if (allFiles[i].getName().indexOf("LTG") >= 0)
-                ltgFiles.add(allFiles[i]);
+
+        for (File allFile : allFiles) {
+            if (allFile.getName().contains("LTG")) {
+                ltgFiles.add(allFile);
+            }
         }
-        
+
         File resultsFile = new File("out.html");
-        PrintWriter writer = new PrintWriter(new FileWriter(resultsFile));
-        writer.println("<html>");
-        writer.println("<style>td {border-bottom: 1px solid black} xmp {white-space:
normal; word-wrap: break-word; word-break: break-all} </style>");
-        writer.println("<body>");
-
-        writer.println("<script language='JavaScript' type='text/javascript'>");
-        writer.println("var w;");
-        writer.println("function openWindow(schema, instance) {");
-        writer.println("  if (w == null) {");
-        writer.println("    w = window.open('about:blank', 'xstc');");
-        writer.println("  }");
-        writer.println("  if (w.closed) {");
-        writer.println("    w = window.open('about:blank', 'xstc');");
-        writer.println("  }");
-        writer.println("  w.document.open();");
-        writer.println("  w.document.write(\"<frameset rows=*,*><frame src='\" +
schema + \"'><frame src='\" + instance + \"'></frameset>\");");
-        writer.println("  w.document.close();");
-        writer.println("  w.focus();");
-        writer.println("}");
-        writer.println("</script>");
-
-        writer.println("<h1>XML Schema Test Collection Results</h1>");
-        writer.println("<p>Run on " + (new XmlCalendar(new Date())) + "</p>");
-        writer.println("<p>Values in schema or instance valid columns are results from
compiling or validating respectively.");
-        writer.println("Red or orange background mean the test failed.</p>");
-        writer.println("<table style='border: 1px solid black' cellpadding=0 cellspacing=0>");
-        writer.println("<tr><td witdh=10%>id</td><td width=70%>Description</td><td
width=10%>sch v</td><td width=10%>ins v</td></tr>");
-        int failures = 0;
-        int cases = 0;
-        for (Iterator i = ltgFiles.iterator(); i.hasNext(); )
-        {
-            File ltgFile = (File)i.next();
-            System.out.println("Processing test cases in " + ltgFile);
-            Collection ltgErrors = new ArrayList();
-            TestCase[] testCases = parseLTGFile(ltgFile, ltgErrors);
-            Collection results = new ArrayList();
-            if (testCases != null) for (int j = 0; j < testCases.length; j++)
-            {
-                TestCaseResult result = new TestCaseResult();
-                result.testCase = testCases[j];
-                harness.runTestCase(result);
-                cases += 1;
-                if (!result.succeeded(errcode))
-                    failures += 1;
-                else if (!showpass)
-                    continue;
-                results.add(result);
-            }
-            writer.println("<tr><td colspan=4 bgcolor=skyblue>" + ltgFile + "</td></tr>");
-            if (!ltgErrors.isEmpty())
-            {
-                writer.println("<tr><td>Errors within the LTG file:");
-                writer.println("<xmp>");
-                for (Iterator j = ltgErrors.iterator(); j.hasNext(); )
-                    writer.println(j.next());
-                writer.println("</xmp>");
-                writer.println("</td></tr>");
-            }
-            else
-            {
-                if (results.size() == 0)
-                    writer.println("<tr><td colspan=4 bgcolor=green>Nothing to
report</td></tr>");
-            }
-            if (results == null)
-                continue;
-            for (Iterator j = results.iterator(); j.hasNext() ;)
-            {
-                summarizeResultAsHTMLTableRows((TestCaseResult)j.next(), writer, errcode);
-            }
-        }
-        writer.println("<tr><td colspan=4>Summary: " + failures + " failures
out of " + cases + " cases run.</td></tr>");
-        writer.println("</table>");
-        writer.close();
-        
+        try (PrintWriter writer = new PrintWriter(Files.newBufferedWriter(resultsFile.toPath(),
StandardCharsets.ISO_8859_1))) {
+            writer.println("<html>");
+            writer.println("<style>td {border-bottom: 1px solid black} xmp {white-space:
normal; word-wrap: break-word; word-break: break-all} </style>");
+            writer.println("<body>");
+
+            writer.println("<script language='JavaScript' type='text/javascript'>");
+            writer.println("var w;");
+            writer.println("function openWindow(schema, instance) {");
+            writer.println("  if (w == null) {");
+            writer.println("    w = window.open('about:blank', 'xstc');");
+            writer.println("  }");
+            writer.println("  if (w.closed) {");
+            writer.println("    w = window.open('about:blank', 'xstc');");
+            writer.println("  }");
+            writer.println("  w.document.open();");
+            writer.println("  w.document.write(\"<frameset rows=*,*><frame src='\"
+ schema + \"'><frame src='\" + instance + \"'></frameset>\");");
+            writer.println("  w.document.close();");
+            writer.println("  w.focus();");
+            writer.println("}");
+            writer.println("</script>");
+
+            writer.println("<h1>XML Schema Test Collection Results</h1>");
+            writer.println("<p>Run on " + (new XmlCalendar(new Date())) + "</p>");
+            writer.println("<p>Values in schema or instance valid columns are results
from compiling or validating respectively.");
+            writer.println("Red or orange background mean the test failed.</p>");
+            writer.println("<table style='border: 1px solid black' cellpadding=0 cellspacing=0>");
+            writer.println("<tr><td witdh=10%>id</td><td width=70%>Description</td><td
width=10%>sch v</td><td width=10%>ins v</td></tr>");
+            int failures = 0;
+            int cases = 0;
+            for (File ltgFile : ltgFiles) {
+                System.out.println("Processing test cases in " + ltgFile);
+                Collection<String> ltgErrors = new ArrayList<>();
+                TestCase[] testCases = parseLTGFile(ltgFile, ltgErrors);
+                final Collection<TestCaseResult> results = new ArrayList<>();
+                if (testCases != null) {
+                    for (TestCase testCase : testCases) {
+                        TestCaseResult result = new TestCaseResult();
+                        result.testCase = testCase;
+                        harness.runTestCase(result);
+                        cases += 1;
+                        if (!result.succeeded(errcode)) {
+                            failures += 1;
+                        } else if (!showpass) {
+                            continue;
+                        }
+                        results.add(result);
+                    }
+                }
+                writer.println("<tr><td colspan=4 bgcolor=skyblue>" + ltgFile
+ "</td></tr>");
+                if (!ltgErrors.isEmpty()) {
+                    writer.println("<tr><td>Errors within the LTG file:");
+                    writer.println("<xmp>");
+                    for (String ltgError : ltgErrors) {
+                        writer.println(ltgError);
+                    }
+                    writer.println("</xmp>");
+                    writer.println("</td></tr>");
+                } else {
+                    if (results.size() == 0) {
+                        writer.println("<tr><td colspan=4 bgcolor=green>Nothing
to report</td></tr>");
+                    }
+                }
+                for (TestCaseResult result : results) {
+                    summarizeResultAsHTMLTableRows(result, writer, errcode);
+                }
+            }
+            writer.println("<tr><td colspan=4>Summary: " + failures + " failures
out of " + cases + " cases run.</td></tr>");
+            writer.println("</table>");
+        }
+
         long finish = System.currentTimeMillis();
-        System.out.println("Time run tests: " + ((double)(finish - start) / 1000.0) + " seconds"
);
-        
+        System.out.println("Time run tests: " + ((double) (finish - start) / 1000.0) + "
seconds");
+
         // Launch results
         System.out.println("Results output to " + resultsFile);
-        if (SystemProperties.getProperty("os.name").toLowerCase().indexOf("windows") >=
0)
+        String osName = SystemProperties.getProperty("os.name");
+        assert (osName != null);
+        if (osName.toLowerCase(Locale.ROOT).contains("windows")) {
             Runtime.getRuntime().exec("cmd /c start iexplore \"" + resultsFile.getAbsolutePath()
+ "\"");
-        else
+        } else {
             Runtime.getRuntime().exec("mozilla file://" + resultsFile.getAbsolutePath());
+        }
     }
-    
-    public static class TestCase
-    {
+
+    public static class TestCase {
         private File ltgFile;
         private String id;
         private String origin;
@@ -199,176 +183,152 @@ public class XSTCTester
         private boolean rvExpected;
         private String errorCode;
 
-        public File getLtgFile()
-        {
+        public File getLtgFile() {
             return ltgFile;
         }
 
-        public String getId()
-        {
+        public String getId() {
             return id;
         }
 
-        public String getOrigin()
-        {
+        public String getOrigin() {
             return origin;
         }
 
-        public String getDescription()
-        {
+        public String getDescription() {
             return description;
         }
 
-        public File getSchemaFile()
-        {
+        public File getSchemaFile() {
             return schemaFile;
         }
 
-        public File getInstanceFile()
-        {
+        public File getInstanceFile() {
             return instanceFile;
         }
 
-        public File getResourceFile()
-        {
+        public File getResourceFile() {
             return resourceFile;
         }
 
-        public boolean isSvExpected()
-        {
+        public boolean isSvExpected() {
             return svExpected;
         }
 
-        public boolean isIvExpected()
-        {
+        public boolean isIvExpected() {
             return ivExpected;
         }
 
-        public boolean isRvExpected()
-        {
+        public boolean isRvExpected() {
             return rvExpected;
         }
 
-        public String getErrorCode()
-        {
+        public String getErrorCode() {
             return errorCode;
         }
 
     }
-    
-    public static class TestCaseResult
-    {
+
+    public static class TestCaseResult {
         private TestCase testCase;
         private boolean svActual;
-        private Collection svMessages = new ArrayList();
+        private final Collection<XmlError> svMessages = new ArrayList<>();
         private boolean ivActual;
-        private Collection ivMessages = new ArrayList();
+        private final Collection<XmlError> ivMessages = new ArrayList<>();
         private boolean crash;
 
-        public TestCase getTestCase()
-        {
+        public TestCase getTestCase() {
             return testCase;
         }
 
-        public boolean isSvActual()
-        {
+        public boolean isSvActual() {
             return svActual;
         }
 
-        public void setSvActual(boolean svActual)
-        {
+        public void setSvActual(boolean svActual) {
             this.svActual = svActual;
         }
 
-        public boolean isIvActual()
-        {
+        public boolean isIvActual() {
             return ivActual;
         }
 
-        public void setIvActual(boolean ivActual)
-        {
+        public void setIvActual(boolean ivActual) {
             this.ivActual = ivActual;
         }
 
-        public Collection getSvMessages()
-        {
+        public Collection<XmlError> getSvMessages() {
             return Collections.unmodifiableCollection(svMessages);
         }
 
-        public void addSvMessages(Collection svMessages)
-        {
+        public void addSvMessages(Collection<XmlError> svMessages) {
             this.svMessages.addAll(svMessages);
         }
 
-        public Collection getIvMessages()
-        {
+        public Collection<XmlError> getIvMessages() {
             return Collections.unmodifiableCollection(ivMessages);
         }
 
-        public void addIvMessages(Collection ivMessages)
-        {
+        public void addIvMessages(Collection<XmlError> ivMessages) {
             this.ivMessages.addAll(ivMessages);
         }
-        
-        public void setCrash(boolean crash)
-        {
+
+        public void setCrash(boolean crash) {
             this.crash = crash;
         }
-        
-        public boolean isCrash()
-        {
+
+        public boolean isCrash() {
             return crash;
         }
-        
-        public boolean succeeded(boolean errcode)
-        {
+
+        public boolean succeeded(boolean errcode) {
             boolean success = !crash &&
-                (isIvActual() == testCase.isIvExpected()) &&
-                (isSvActual() == testCase.isSvExpected());
-            if (errcode && testCase.getErrorCode() != null)
+                              (isIvActual() == testCase.isIvExpected()) &&
+                              (isSvActual() == testCase.isSvExpected());
+            if (errcode && testCase.getErrorCode() != null) {
                 success &= errorReported(testCase.getErrorCode(), svMessages) || errorReported(testCase.getErrorCode(),
ivMessages);
+            }
             return success;
         }
 
     }
-    
-    public static interface Harness
-    {
-        public void runTestCase(TestCaseResult result);
+
+    public interface Harness {
+        void runTestCase(TestCaseResult result);
     }
-    
-    public static String makeHTMLLink(File file, boolean value)
-    {
-        if (file == null)
+
+    public static String makeHTMLLink(File file, boolean value) {
+        if (file == null) {
             return "&nbsp;";
+        }
         URI uri = file.getAbsoluteFile().toURI();
-        return "<a href=\"" + uri + "\" target=_blank>" + Boolean.toString(value) +
"</a>";
+        return "<a href=\"" + uri + "\" target=_blank>" + value + "</a>";
     }
-    
+
     private static final Pattern leadingSpace = Pattern.compile("^\\s+", Pattern.MULTILINE);
-    
-    public static String makeHTMLDescription(TestCase testCase)
-    {
+
+    public static String makeHTMLDescription(TestCase testCase) {
         StringBuilder sb = new StringBuilder();
         sb.append("<a class=noline href='javascript:openWindow(\"");
-        if (testCase.getSchemaFile() == null)
+        if (testCase.getSchemaFile() == null) {
             sb.append("about:No schema");
-        else
+        } else {
             sb.append(testCase.getSchemaFile().getAbsolutePath().replaceAll("\\\\", "\\\\\\\\"));
+        }
 
         sb.append("\", \"");
-        if (testCase.getInstanceFile() == null)
+        if (testCase.getInstanceFile() == null) {
             sb.append("about:No instance");
-        else
+        } else {
             sb.append(testCase.getInstanceFile().getAbsolutePath().replaceAll("\\\\", "\\\\\\\\"));
+        }
         sb.append("\")'><xmp>");
         sb.append(leadingSpace.matcher(testCase.getDescription()).replaceAll(""));
         sb.append("</xmp></a>");
         return sb.toString();
     }
-    
-    public static void summarizeResultAsHTMLTableRows(TestCaseResult result, PrintWriter
out, boolean errcode)
-    {
+
+    public static void summarizeResultAsHTMLTableRows(TestCaseResult result, PrintWriter
out, boolean errcode) {
         TestCase testCase = result.getTestCase();
 
         boolean errorRow = errcode && testCase.getErrorCode() != null;
@@ -377,131 +337,134 @@ public class XSTCTester
         boolean sRight = testCase.getSchemaFile() == null || testCase.isSvExpected() == result.isSvActual();
         boolean iRight = testCase.getInstanceFile() == null || testCase.isIvExpected() ==
result.isIvActual();
         boolean codeRight = true;
-        if (errorRow)
+        if (errorRow) {
             codeRight = (errorReported(testCase.getErrorCode(), result.svMessages) || errorReported(testCase.getErrorCode(),
result.ivMessages));
+        }
 
         out.println(result.isCrash() ? "<tr bgcolor=black color=white>" : "<tr>");
         int idRowSpan = 1 + (errorRow ? 1 : 0) + (messagesRow ? 1 : 0);
         out.println("<td rowspan=" + idRowSpan + " valign=top>" + testCase.getId()
+ "</td>");
         out.println("<td valign=top>" + makeHTMLDescription(testCase) + "</td>");
         String sLinks;
-        if (testCase.getResourceFile() == null)
+        if (testCase.getResourceFile() == null) {
             sLinks = makeHTMLLink(testCase.getSchemaFile(), result.isSvActual());
-        else
+        } else {
             sLinks = makeHTMLLink(testCase.getSchemaFile(), result.isSvActual()) + "<br>"
+ makeHTMLLink(testCase.getResourceFile(), result.isSvActual());
-        
+        }
+
         out.println((sRight ? "<td valign=top>" : result.isSvActual() ? "<td bgcolor=orange
valign=top>" : "<td bgcolor=red valign=top>") + sLinks + "</td>");
         out.println((iRight ? "<td valign=top>" : result.isIvActual() ? "<td bgcolor=orange
valign=top>" : "<td bgcolor=red valign=top>") + makeHTMLLink(testCase.getInstanceFile(),
result.isIvActual()) + "</td>");
         out.println("</tr>");
-        if (errorRow)
-        {
+        if (errorRow) {
             out.println("<tr>");
             out.println((codeRight ? "<td colspan=4 valid=top>" : "<td colspan=4
bgcolor=orange valign=top>") + "expected error: " + testCase.getErrorCode() + "</td>");
             out.println("</tr>");
         }
-        if (messagesRow)
-        {
-            if (!result.succeeded(errcode))
+        if (messagesRow) {
+            if (!result.succeeded(errcode)) {
                 out.println("<tr><td colspan=4 bgcolor=yellow><xmp>");
-            else
+            } else {
                 out.println("<tr><td colspan=4><xmp>");
-            for (Iterator j = result.getSvMessages().iterator(); j.hasNext(); )
-                out.println(j.next());
-            for (Iterator j = result.getIvMessages().iterator(); j.hasNext(); )
-                out.println(j.next());
+            }
+            for (XmlError s : result.getSvMessages()) {
+                out.println(s);
+            }
+            for (XmlError xmlError : result.getIvMessages()) {
+                out.println(xmlError);
+            }
             out.println("</xmp></tr></td>");
         }
     }
-    
-    public static TestCase[] parseLTGFile(File ltgFile, Collection outerErrors)
-    {
-        Collection errors = new ArrayList();
-        try
-        {
+
+    public static TestCase[] parseLTGFile(File ltgFile, Collection<String> outerErrors)
{
+        Collection<XmlError> errors = new ArrayList<>();
+        try {
             XmlOptions ltgOptions = new XmlOptions();
             ltgOptions.setLoadSubstituteNamespaces(Collections.singletonMap("", "http://www.bea.com/2003/05/xmlbean/ltgfmt"));
             ltgOptions.setErrorListener(errors);
             ltgOptions.setLoadLineNumbers();
             TestsDocument doc = TestsDocument.Factory.parse(ltgFile, ltgOptions);
-            if (!doc.validate(ltgOptions))
+            if (!doc.validate(ltgOptions)) {
                 throw new Exception("Document " + ltgFile + " not valid.");
-            
+            }
+
             org.apache.xmlbeans.impl.xb.ltgfmt.TestCase[] testCases = doc.getTests().getTestArray();
-            
-            Collection result = new ArrayList();
-            for (int i = 0; i < testCases.length; i++)
-            {
+
+            Collection<TestCase> result = new ArrayList<>();
+            for (org.apache.xmlbeans.impl.xb.ltgfmt.TestCase testCase : testCases) {
                 TestCase newCase = new TestCase();
                 newCase.ltgFile = ltgFile;
-                newCase.id = testCases[i].getId();
-                newCase.origin = testCases[i].getOrigin();
-                newCase.description = testCases[i].getDescription();
-                FileDesc[] filedescs = testCases[i].getFiles().getFileArray();
-                testCases[i].getOrigin();
-                for (int j = 0; j < filedescs.length; j++)
-                {
-                    String dir = filedescs[j].getFolder();
-                    String filename = filedescs[j].getFileName();
+                newCase.id = testCase.getId();
+                newCase.origin = testCase.getOrigin();
+                newCase.description = testCase.getDescription();
+                FileDesc[] filedescs = testCase.getFiles().getFileArray();
+                testCase.getOrigin();
+                for (FileDesc filedesc : filedescs) {
+                    String dir = filedesc.getFolder();
+                    String filename = filedesc.getFileName();
                     File theFile = new File(ltgFile.getParentFile(), dir + "/" + filename);
-                    if (!theFile.exists() || !theFile.isFile() || !theFile.canRead())
-                    {
-                        outerErrors.add(XmlError.forObject("Can't read file " + theFile,
filedescs[j]).toString());
+                    if (!theFile.exists() || !theFile.isFile() || !theFile.canRead()) {
+                        outerErrors.add(XmlError.forObject("Can't read file " + theFile,
filedesc).toString());
                         continue;
                     }
-                    
-                    switch (filedescs[j].getRole().intValue())
-                    {
+
+                    switch (filedesc.getRole().intValue()) {
                         case FileDesc.Role.INT_INSTANCE:
-                            if (newCase.instanceFile != null)
-                                outerErrors.add(XmlError.forObject("More than one instance
file speicifed - ignoring all but last", filedescs[j]).toString());
+                            if (newCase.instanceFile != null) {
+                                outerErrors.add(XmlError.forObject("More than one instance
file speicifed - ignoring all but last", filedesc).toString());
+                            }
                             newCase.instanceFile = theFile;
-                            newCase.ivExpected = filedescs[j].getValidity();
+                            newCase.ivExpected = filedesc.getValidity();
                             break;
-                            
+
                         case FileDesc.Role.INT_SCHEMA:
-                            if (newCase.schemaFile != null)
-                                outerErrors.add(XmlError.forObject("More than one schema
file speicifed - ignoring all but last", filedescs[j]).toString());
+                            if (newCase.schemaFile != null) {
+                                outerErrors.add(XmlError.forObject("More than one schema
file speicifed - ignoring all but last", filedesc).toString());
+                            }
                             newCase.schemaFile = theFile;
-                            newCase.svExpected = filedescs[j].getValidity();
+                            newCase.svExpected = filedesc.getValidity();
                             break;
-                            
+
                         case FileDesc.Role.INT_RESOURCE:
-                            if (newCase.resourceFile != null)
-                                outerErrors.add(XmlError.forObject("More than one resource
file speicifed - ignoring all but last", filedescs[j]).toString());
+                            if (newCase.resourceFile != null) {
+                                outerErrors.add(XmlError.forObject("More than one resource
file speicifed - ignoring all but last", filedesc).toString());
+                            }
                             newCase.resourceFile = theFile;
-                            newCase.rvExpected = filedescs[j].getValidity();
+                            newCase.rvExpected = filedesc.getValidity();
                             break;
-                        
+
                         default:
-                            throw new XmlException(XmlError.forObject("Unexpected file role",
filedescs[j]));
+                            throw new XmlException(XmlError.forObject("Unexpected file role",
filedesc));
                     }
 
-                    if (filedescs[j].getCode() != null)
-                        newCase.errorCode = filedescs[j].getCode().getID();
+                    if (filedesc.getCode() != null) {
+                        newCase.errorCode = filedesc.getCode().getID();
+                    }
                 }
                 result.add(newCase);
             }
-            return (TestCase[])result.toArray(new TestCase[result.size()]);
-        }
-        catch (Exception e)
-        {
-            if (errors.isEmpty())
+            return result.toArray(new TestCase[0]);
+        } catch (Exception e) {
+            if (errors.isEmpty()) {
                 outerErrors.add(e.getMessage());
-            else for (Iterator i = errors.iterator(); i.hasNext(); )
-                outerErrors.add(i.next().toString());
+            } else {
+                for (XmlError error : errors) {
+                    outerErrors.add(error.toString());
+                }
+            }
             return null;
         }
     }
 
-    public static boolean errorReported(String errorCode, Collection set)
-    {
-        if (errorCode == null || set == null || set.size() == 0)
+    public static boolean errorReported(String errorCode, Collection<XmlError> set)
{
+        if (errorCode == null || set == null || set.size() == 0) {
             return false;
+        }
 
-        for (Iterator i = set.iterator(); i.hasNext(); )
-        {
-            if (errorCode.equals(((XmlError)i.next()).getErrorCode()))
+        for (XmlError xmlError : set) {
+            if (errorCode.equals(xmlError.getErrorCode())) {
                 return true;
+            }
         }
 
         return false;

Modified: xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/util/FilerImpl.java
URL: http://svn.apache.org/viewvc/xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/util/FilerImpl.java?rev=1881669&r1=1881668&r2=1881669&view=diff
==============================================================================
--- xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/util/FilerImpl.java (original)
+++ xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/util/FilerImpl.java Sat Sep 12 23:25:46
2020
@@ -1,77 +1,66 @@
 /*   Copyright 2004 The Apache Software Foundation
-*
-*   Licensed under the Apache License, Version 2.0 (the "License");
-*   you may not use this file except in compliance with the License.
-*   You may obtain a copy of the License at
-*
-*       http://www.apache.org/licenses/LICENSE-2.0
-*
-*   Unless required by applicable law or agreed to in writing, software
-*   distributed under the License is distributed on an "AS IS" BASIS,
-*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-*   See the License for the specific language governing permissions and
-*  limitations under the License.
-*/
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
 
 package org.apache.xmlbeans.impl.util;
 
 import org.apache.xmlbeans.Filer;
+import org.apache.xmlbeans.impl.repackage.Repackager;
 
-import java.io.IOException;
-import java.io.File;
-import java.io.Writer;
-import java.io.FileReader;
-import java.io.FileOutputStream;
-import java.io.FileWriter;
-import java.io.OutputStream;
-import java.io.OutputStreamWriter;
-import java.io.StringReader;
-import java.io.StringWriter;
+import java.io.*;
 import java.nio.charset.Charset;
 import java.nio.charset.CharsetEncoder;
 import java.nio.charset.CodingErrorAction;
-import java.util.Set;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
-import java.util.ArrayList;
-
-import org.apache.xmlbeans.impl.repackage.Repackager;
+import java.util.Set;
 
 /**
  * This implementation of Filer writes to disk.
  */
-public class FilerImpl implements Filer
-{
-    private File classdir;
-    private File srcdir;
-    private Repackager repackager;
-    private boolean verbose;
-    private List sourceFiles;
-    private boolean incrSrcGen;
-    private Set seenTypes;
+public class FilerImpl implements Filer {
+    private final File classdir;
+    private final File srcdir;
+    private final Repackager repackager;
+    private final boolean verbose;
+    private final List<File> sourceFiles;
+    private final boolean incrSrcGen;
+    private Set<String> seenTypes;
     private static final Charset CHARSET;
 
-    static
-    {
+    static {
         Charset temp = null;
-        try
-        {
+        try {
             temp = Charset.forName(System.getProperty("file.encoding"));
+        } catch (Exception ignored) {
         }
-        catch (Exception e) {}
         CHARSET = temp;
     }
 
-    public FilerImpl(File classdir, File srcdir, Repackager repackager, boolean verbose,
boolean incrSrcGen)
-    {
+    public FilerImpl(File classdir, File srcdir, Repackager repackager, boolean verbose,
boolean incrSrcGen) {
         this.classdir = classdir;
         this.srcdir = srcdir;
         this.repackager = repackager;
         this.verbose = verbose;
-        this.sourceFiles = (sourceFiles != null ? sourceFiles : new ArrayList());
+        this.sourceFiles = new ArrayList<>();
         this.incrSrcGen = incrSrcGen;
-        if (this.incrSrcGen)
-            seenTypes = new HashSet();
+        if (this.incrSrcGen) {
+            seenTypes = new HashSet<>();
+        }
     }
 
     /**
@@ -79,18 +68,17 @@ public class FilerImpl implements Filer
      *
      * @param typename fully qualified type name
      * @return a stream to write the type to
-     * @throws java.io.IOException
      */
-    public OutputStream createBinaryFile(String typename) throws IOException
-    {
-        if (verbose)
+    public OutputStream createBinaryFile(String typename) throws IOException {
+        if (verbose) {
             System.err.println("created binary: " + typename);
+        }
         // KHK: for now the typename will already be a relative filename for the binary
         //String filename = typename.replace('.', File.separatorChar) + ".xsb";
         File source = new File(classdir, typename);
         source.getParentFile().mkdirs();
 
-        return new FileOutputStream( source );
+        return new FileOutputStream(source);
     }
 
     /**
@@ -98,57 +86,51 @@ public class FilerImpl implements Filer
      *
      * @param typename fully qualified type name
      * @return a stream to write the type to
-     * @throws java.io.IOException
      */
-    public Writer createSourceFile(String typename) throws IOException
-    {
-        if (incrSrcGen)
+    public Writer createSourceFile(String typename) throws IOException {
+        if (incrSrcGen) {
             seenTypes.add(typename);
+        }
 
-        if (typename.indexOf('$') > 0)
-        {
+        if (typename.indexOf('$') > 0) {
             typename =
-                typename.substring( 0, typename.lastIndexOf( '.' ) ) + "." +
-                typename.substring( typename.indexOf( '$' ) + 1 );
+                typename.substring(0, typename.lastIndexOf('.')) + "." +
+                typename.substring(typename.indexOf('$') + 1);
         }
 
         String filename = typename.replace('.', File.separatorChar) + ".java";
 
         File sourcefile = new File(srcdir, filename);
         sourcefile.getParentFile().mkdirs();
-        if (verbose)
+        if (verbose) {
             System.err.println("created source: " + sourcefile.getAbsolutePath());
+        }
 
         sourceFiles.add(sourcefile);
 
-        if (incrSrcGen && sourcefile.exists())
-        {
+        if (incrSrcGen && sourcefile.exists()) {
             // Generate the file in a buffer and then compare it to the
             // file already on disk
             return new IncrFileWriter(sourcefile, repackager);
-        }
-        else
-        {
+        } else {
             return repackager == null ?
-                (Writer) writerForFile( sourcefile ) :
-                (Writer) new RepackagingWriter( sourcefile, repackager );
+                writerForFile(sourcefile) :
+                new RepackagingWriter(sourcefile, repackager);
         }
     }
 
-    public List getSourceFiles()
-    {
-        return new ArrayList(sourceFiles);
+    public List<File> getSourceFiles() {
+        return new ArrayList<>(sourceFiles);
     }
 
-    public Repackager getRepackager()
-    {
+    public Repackager getRepackager() {
         return repackager;
     }
 
-    private static final Writer writerForFile(File f) throws IOException
-    {
-        if (CHARSET == null)
-            return new FileWriter(f);
+    private static Writer writerForFile(File f) throws IOException {
+        if (CHARSET == null) {
+            return Files.newBufferedWriter(f.toPath(), StandardCharsets.ISO_8859_1);
+        }
 
         FileOutputStream fileStream = new FileOutputStream(f);
         CharsetEncoder ce = CHARSET.newEncoder();
@@ -156,19 +138,16 @@ public class FilerImpl implements Filer
         return new OutputStreamWriter(fileStream, ce);
     }
 
-    static class IncrFileWriter extends StringWriter
-    {
-        private File _file;
-        private Repackager _repackager;
+    static class IncrFileWriter extends StringWriter {
+        private final File _file;
+        private final Repackager _repackager;
 
-        public IncrFileWriter(File file, Repackager repackager)
-        {
+        public IncrFileWriter(File file, Repackager repackager) {
             _file = file;
             _repackager = repackager;
         }
 
-        public void close() throws IOException
-        {
+        public void close() throws IOException {
             super.close();
 
             // This is where all the real work happens
@@ -176,56 +155,39 @@ public class FilerImpl implements Filer
                 _repackager.repackage(getBuffer()) :
                 getBuffer();
             String str = sb.toString();
-            List diffs = new ArrayList();
-            StringReader sReader = new StringReader(str);
-            FileReader fReader = new FileReader(_file);
+            List<String> diffs = new ArrayList<>();
 
-            try
-            {
+            try (StringReader sReader = new StringReader(str);
+                 FileReader fReader = new FileReader(_file)) {
                 Diff.readersAsText(sReader, "<generated>",
                     fReader, _file.getName(), diffs);
             }
-            finally
-            {
-                sReader.close();
-                fReader.close();
-            }
 
-            if (diffs.size() > 0)
-            {
+            if (diffs.size() > 0) {
                 // Diffs encountered, replace the file on disk with text from
                 // the buffer
-                Writer fw = writerForFile(_file);
-                try
-                {   fw.write(str); }
-                finally
-                {   fw.close(); }
+                try (Writer fw = writerForFile(_file)) {
+                    fw.write(str);
+                }
             }
-            else
-                ; // If no diffs, don't do anything
         }
     }
 
-    static class RepackagingWriter extends StringWriter
-    {
-        public RepackagingWriter ( File file, Repackager repackager )
-        {
+    static class RepackagingWriter extends StringWriter {
+        public RepackagingWriter(File file, Repackager repackager) {
             _file = file;
             _repackager = repackager;
         }
 
-        public void close ( ) throws IOException
-        {
+        public void close() throws IOException {
             super.close();
 
-            Writer fw = writerForFile( _file );
-            try
-            { fw.write( _repackager.repackage( getBuffer() ).toString() ); }
-            finally
-            { fw.close(); }
+            try (Writer fw = writerForFile(_file)) {
+                fw.write(_repackager.repackage(getBuffer()).toString());
+            }
         }
 
-        private File _file;
-        private Repackager _repackager;
+        private final File _file;
+        private final Repackager _repackager;
     }
 }

Modified: xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/JavaBase64Holder.java
URL: http://svn.apache.org/viewvc/xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/JavaBase64Holder.java?rev=1881669&r1=1881668&r2=1881669&view=diff
==============================================================================
--- xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/JavaBase64Holder.java (original)
+++ xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/JavaBase64Holder.java Sat
Sep 12 23:25:46 2020
@@ -110,7 +110,7 @@ public abstract class JavaBase64Holder e
     //because computing hashcode is expensive we'll cache it
     protected boolean _hashcached = false;
     protected int hashcode = 0;
-    protected static MessageDigest md5;
+    protected static final MessageDigest md5;
 
     static {
         try {

Modified: xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/JavaHexBinaryHolder.java
URL: http://svn.apache.org/viewvc/xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/JavaHexBinaryHolder.java?rev=1881669&r1=1881668&r2=1881669&view=diff
==============================================================================
--- xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/JavaHexBinaryHolder.java
(original)
+++ xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/values/JavaHexBinaryHolder.java
Sat Sep 12 23:25:46 2020
@@ -111,7 +111,7 @@ public abstract class JavaHexBinaryHolde
     //because computing hashcode is expensive we'll cache it
     protected boolean _hashcached = false;
     protected int hashcode = 0;
-    protected static MessageDigest md5;
+    protected static final MessageDigest md5;
 
     static {
         try {



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message