From 2c834078d24857f491d60a4677c4f2f658a2f7fe Mon Sep 17 00:00:00 2001 From: Bruno Sinou Date: Thu, 16 Jun 2011 15:48:26 +0000 Subject: [PATCH] + fix csv parser : ',' character are not removed anymore in quoted string corresponding JUnit test added + fix user handling in JcrUtils git-svn-id: https://svn.argeo.org/commons/trunk@4574 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- .../runtime/org.argeo.basic.nodeps/.classpath | 1 + .../org.argeo.basic.nodeps/build.properties | 1 + .../main/java/org/argeo/util/CsvParser.java | 9 ++- .../CsvParserWithQuotedSeparatorTest.java | 63 +++++++++++++++++++ .../org/argeo/util/ReferenceFile.csv | 20 +++--- .../ui/specific/DownloadServiceHandler.java | 2 + .../ui/specific/GenericUploadControl.java | 7 ++- .../src/main/java/org/argeo/jcr/JcrUtils.java | 7 ++- 8 files changed, 97 insertions(+), 13 deletions(-) create mode 100644 basic/runtime/org.argeo.basic.nodeps/src/test/java/org/argeo/util/CsvParserWithQuotedSeparatorTest.java diff --git a/basic/runtime/org.argeo.basic.nodeps/.classpath b/basic/runtime/org.argeo.basic.nodeps/.classpath index 3bf070c69..f0a6d79aa 100644 --- a/basic/runtime/org.argeo.basic.nodeps/.classpath +++ b/basic/runtime/org.argeo.basic.nodeps/.classpath @@ -2,6 +2,7 @@ + diff --git a/basic/runtime/org.argeo.basic.nodeps/build.properties b/basic/runtime/org.argeo.basic.nodeps/build.properties index 0b396f912..5f242a355 100644 --- a/basic/runtime/org.argeo.basic.nodeps/build.properties +++ b/basic/runtime/org.argeo.basic.nodeps/build.properties @@ -1,4 +1,5 @@ source.. = src/main/java/,\ + src/test/resources/,\ src/test/java/ bin.includes = META-INF/,\ . diff --git a/basic/runtime/org.argeo.basic.nodeps/src/main/java/org/argeo/util/CsvParser.java b/basic/runtime/org.argeo.basic.nodeps/src/main/java/org/argeo/util/CsvParser.java index ca81bc983..9acbfe246 100644 --- a/basic/runtime/org.argeo.basic.nodeps/src/main/java/org/argeo/util/CsvParser.java +++ b/basic/runtime/org.argeo.basic.nodeps/src/main/java/org/argeo/util/CsvParser.java @@ -111,7 +111,7 @@ public abstract class CsvParser { StringBuffer currStr, Boolean wasInquote) { // List tokens = new ArrayList(); - //System.out.println("#LINE: " + str); + // System.out.println("#LINE: " + str); if (wasInquote) currStr.append('\n'); @@ -126,6 +126,11 @@ public abstract class CsvParser { tokens.add(currStr.toString()); //System.out.println("# TOKEN: " + currStr); currStr.delete(0, currStr.length()); + } else { + // we don't remove separator that are in a quoted substring + // System.out + // .println("IN QUOTE, got a separator: [" + c + "]"); + currStr.append(c); } } else if (c == quote) { if (inQuote && (i + 1) < arr.length && arr[i + 1] == quote) { @@ -142,7 +147,7 @@ public abstract class CsvParser { if (!inQuote) { tokens.add(currStr.toString()); - //System.out.println("# TOKEN: " + currStr); + // System.out.println("# TOKEN: " + currStr); } // if (inQuote) // throw new ArgeoException("Missing quote at the end of the line " diff --git a/basic/runtime/org.argeo.basic.nodeps/src/test/java/org/argeo/util/CsvParserWithQuotedSeparatorTest.java b/basic/runtime/org.argeo.basic.nodeps/src/test/java/org/argeo/util/CsvParserWithQuotedSeparatorTest.java new file mode 100644 index 000000000..fb7707599 --- /dev/null +++ b/basic/runtime/org.argeo.basic.nodeps/src/test/java/org/argeo/util/CsvParserWithQuotedSeparatorTest.java @@ -0,0 +1,63 @@ +package org.argeo.util; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import junit.framework.TestCase; + +public class CsvParserWithQuotedSeparatorTest extends TestCase { + public void testSimpleParse() throws Exception { + String toParse = "Header1,\"Header2\",Header3,\"Header4\"\n" + + "\"Col1, Col2\",\"Col\n2\",Col3,\"\"\"Col4\"\"\"\n"; + + InputStream in = new ByteArrayInputStream(toParse.getBytes()); + + CsvParser csvParser = new CsvParser() { + protected void processLine(Integer lineNumber, List header, + List tokens) { + assertEquals(header.size(), tokens.size()); + assertEquals(4, tokens.size()); + assertEquals("Col1, Col2", tokens.get(0)); + } + }; + // System.out.println(toParse); + csvParser.parse(in); + in.close(); + + } + + public void testParseFile() throws Exception { + + final Map> lines = new HashMap>(); + InputStream in = getClass().getResourceAsStream( + "/org/argeo/util/ReferenceFile.csv"); + + CsvParserWithLinesAsMap parser = new CsvParserWithLinesAsMap() { + protected void processLine(Integer lineNumber, + Map line) { + // System.out.println("processing line #" + lineNumber); + lines.put(lineNumber, line); + } + }; + + parser.parse(in); + in.close(); + + Map line = lines.get(2); + assertEquals(",,,,", line.get("Coma testing")); + line = lines.get(3); + assertEquals(",, ,,", line.get("Coma testing")); + line = lines.get(4); + assertEquals("module1, module2", line.get("Coma testing")); + line = lines.get(5); + assertEquals("module1,module2", line.get("Coma testing")); + line = lines.get(6); + assertEquals(",module1,module2, \nmodule3, module4", + line.get("Coma testing")); + assertEquals(5, lines.size()); + + } +} diff --git a/basic/runtime/org.argeo.basic.nodeps/src/test/resources/org/argeo/util/ReferenceFile.csv b/basic/runtime/org.argeo.basic.nodeps/src/test/resources/org/argeo/util/ReferenceFile.csv index dc2f246bd..351453d10 100644 --- a/basic/runtime/org.argeo.basic.nodeps/src/test/resources/org/argeo/util/ReferenceFile.csv +++ b/basic/runtime/org.argeo.basic.nodeps/src/test/resources/org/argeo/util/ReferenceFile.csv @@ -1,22 +1,23 @@ -"ID","A long Text","Name","Other","Number","Reference","Target","Date","Update","Language","ID Ref","weird chars","line feeds","after line feed","Empty column","Status comment","Comments","Empty" -"AK251","Everything & with some line feed \n more “some” quote","B TY",,78.6,"A1155222221111268515131",,12/12/12,03/12/08,,9821308500721,"%%%ùù","ao","alf",,,"Bruno/Antoine what is the real identity?", +"ID","A long Text","Name","Other","Number","Reference","Target","Date","Update","Language","ID Ref","Weird chars","line feeds","after line feed","Empty column","Status comment","Comments","Empty","Coma testing" +"AK251","Everything & with some line feed + more “some” quote","Marge S.",,78.6,"A1155222221111268515131",,12/12/12,03/12/08,,9821308500721,"%%%ùù","ao","Nothing special",,,"Some very usefull comment",,",,,," "AG254","same","Roger “wallace” Big","15 – JI",78.5,"A1155222221111268515131","next milestone",12/12/12,03/12/08,"_fr (French - France)",9812309500953,"***µ”","a -o","alf",,"Do the job",, +o","after line feed",,"Do the job",,,",, ,," "FG211","Very long text with some bullets. 1 first 2 second -3. some more very very very long. some more very very very long. some more very very very long. some more very very very long. some more very very very long. some more very very very long. some more very very very long. some more very very very long. some more very very very long. some more very very very long. some more very very very long. some more very very very long","None","15 – JI",15.4,"A1155222221111268515131","next milestone",12/12/12,03/12/08,"_fr (French - France)",9812309500952,"///","a +3. some more very very very long. some more very very very long. some more very very very long. some more very very very long. some more very very very long. some more very very very long. some more very very very long. some more very very very long. some more very very very long. some more very very very long. some more very very very long. some more very very very long","Father & Son","15 – JI",15.4,"A1155222221111268515131","next milestone",12/12/12,03/12/08,"_fr (French - France)",9812309500952,"///","a -o","alf",,"Be fast",, +o","module1,module2",,"Be fast",,,"module1, module2" "RRT152","Very long text with some bullets. 1 first 2 second @@ -25,7 +26,12 @@ o","alf",,"Be fast",, o -","alf",,,, +","module1,module2",,,,,"module1,module2" "YU121","Another use case : “blank line” -After the blank.","nothing with brackets( )","15 – JI",15.2,"A1155222221111268515131",,12/12/12,03/12/08,"_fr (French - France)",9812309500925,",;:?./","ao","alf",,,, +After the blank.","nothing with brackets( )","15 – JI",15.2,"A1155222221111268515131",,12/12/12,03/12/08,"_fr (French - France)",9812309500925,",;:?./","ao"," + + + +After line feed again",,,,,",module1,module2, +module3, module4" diff --git a/eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/DownloadServiceHandler.java b/eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/DownloadServiceHandler.java index 7d47d5652..0c7eac67f 100644 --- a/eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/DownloadServiceHandler.java +++ b/eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/DownloadServiceHandler.java @@ -31,6 +31,8 @@ public class DownloadServiceHandler implements IServiceHandler { response.setContentLength(download.length); String contentDisposition = "attachment; filename=\"" + fileName + "\""; response.setHeader("Content-Disposition", contentDisposition); + // response.setHeader( "Cache-Control", "no-cache" ); + try { response.getOutputStream().write(download); } catch (IOException ioe) { diff --git a/eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/GenericUploadControl.java b/eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/GenericUploadControl.java index 6fcd21a89..aff7c5ac3 100644 --- a/eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/GenericUploadControl.java +++ b/eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/GenericUploadControl.java @@ -31,7 +31,7 @@ public class GenericUploadControl extends Composite { upload = new Upload(parent, SWT.BORDER); upload.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); - upload.setBrowseButtonText(browseLabel); + // upload.setBrowseButtonText(browseLabel); // upload.addModifyListener(new UploadListener()); parent.pack(); } @@ -49,6 +49,11 @@ public class GenericUploadControl extends Composite { public byte[] performUpload() { boolean success = upload.performUpload(); if (success) { + if (upload.getUploadItem().getFileSize() == -1) + throw new ArgeoException("File " + + upload.getUploadItem().getFileName() + + "has not been uploaded : its size = -1"); + InputStream inStream = null; byte[] fileBA = null; try { diff --git a/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java b/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java index b03be7396..19a3f7703 100644 --- a/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java +++ b/server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java @@ -858,7 +858,8 @@ public class JcrUtils implements ArgeoJcrConstants { } catch (Exception e) { // we use this workaround to be sure to get the stack trace // to identify the sink of the bug. - log.warn("trying to create an already existing userHome. Stack trace : "); + log.warn("trying to create an already existing userHome at path:" + + homePath + ". Stack trace : "); e.printStackTrace(); } } @@ -866,8 +867,8 @@ public class JcrUtils implements ArgeoJcrConstants { Node userHome = JcrUtils.mkdirs(session, homePath); Node userProfile; if (userHome.hasNode(ArgeoNames.ARGEO_PROFILE)) { - log.warn("User profile node already exists. We do not add a new one"); - + log.warn("userProfile node already exists for userHome path: " + + homePath + ". We do not add a new one"); } else { userProfile = userHome.addNode(ArgeoNames.ARGEO_PROFILE); userProfile.addMixin(ArgeoTypes.ARGEO_USER_PROFILE); -- 2.30.2