You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by or...@apache.org on 2018/02/26 16:41:27 UTC
[1/6] qpid-broker-j git commit: QPID-8103: [Broker-J] [WMC] [Query
UI] Add ability to download query results as CSV
Repository: qpid-broker-j
Updated Branches:
refs/heads/master 8c88850ee -> a2920afbc
QPID-8103: [Broker-J] [WMC] [Query UI] Add ability to download query results as CSV
Project: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/commit/a2920afb
Tree: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/tree/a2920afb
Diff: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/diff/a2920afb
Branch: refs/heads/master
Commit: a2920afbc2ce92345c18c1add3e6d310358d4c69
Parents: 8d0e68f
Author: Alex Rudyy <or...@apache.org>
Authored: Mon Feb 26 16:19:21 2018 +0000
Committer: Alex Rudyy <or...@apache.org>
Committed: Mon Feb 26 16:40:48 2018 +0000
----------------------------------------------------------------------
QPID-8066.tar.bz2 | Bin 0 -> 16303 bytes
.../server/management/plugin/csv/CSVFormat.java | 22 +++++++++-
.../plugin/servlet/rest/AbstractServlet.java | 23 ++++++++++
.../plugin/servlet/rest/QueryServlet.java | 44 ++++++++++++++++---
.../plugin/servlet/rest/RestServlet.java | 22 ----------
.../src/main/java/resources/css/common.css | 4 ++
.../resources/js/qpid/management/Management.js | 17 +++++--
.../js/qpid/management/query/QueryWidget.js | 31 ++++++++++++-
.../main/java/resources/query/QueryWidget.html | 7 +++
.../management/plugin/csv/CSVFormatTest.java | 14 ++++++
10 files changed, 151 insertions(+), 33 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/a2920afb/QPID-8066.tar.bz2
----------------------------------------------------------------------
diff --git a/QPID-8066.tar.bz2 b/QPID-8066.tar.bz2
new file mode 100644
index 0000000..1fe1414
Binary files /dev/null and b/QPID-8066.tar.bz2 differ
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/a2920afb/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVFormat.java
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVFormat.java b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVFormat.java
index 5636477..e738a82 100644
--- a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVFormat.java
+++ b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVFormat.java
@@ -22,7 +22,11 @@ package org.apache.qpid.server.management.plugin.csv;
import java.io.IOException;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
+import java.util.Calendar;
import java.util.Collection;
+import java.util.Date;
/**
* Simplified version of CSVFormat from Apache Commons CSV
@@ -44,6 +48,7 @@ public final class CSVFormat
private static final char LF = '\n';
private static final char SP = ' ';
+ private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private final char _delimiter;
@@ -120,9 +125,24 @@ public final class CSVFormat
{
charSequence = EMPTY;
}
+ else if (value instanceof CharSequence)
+ {
+ charSequence = (CharSequence) value;
+ }
+ else if (value instanceof Date || value instanceof Calendar)
+ {
+ final Date time = value instanceof Calendar ? ((Calendar) value).getTime() : (Date) value;
+
+ // CSV standard (rfc4180) does not specify the date time format
+ // Excel CSV format is local specific
+ // Some posts on stackoverflow indicate that Excel should support format "yyyy-MM-dd HH:mm:ss".
+ // for example, https://stackoverflow.com/questions/804118/best-timestamp-format-for-csv-excel
+ // Perhaps, it would be better to convert datetime into long (similar to json datetime representation)
+ charSequence = DATE_TIME_FORMATTER.format(time.toInstant().atZone(ZoneId.systemDefault()));
+ }
else
{
- charSequence = value instanceof CharSequence ? (CharSequence) value : value.toString();
+ charSequence = value.toString();
}
this.print(out, value, charSequence, 0, charSequence.length(), newRecord);
}
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/a2920afb/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/AbstractServlet.java
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/AbstractServlet.java b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/AbstractServlet.java
index 4403200..76d87f1 100644
--- a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/AbstractServlet.java
+++ b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/AbstractServlet.java
@@ -22,6 +22,7 @@ package org.apache.qpid.server.management.plugin.servlet.rest;
import static org.apache.qpid.server.management.plugin.HttpManagementUtil.CONTENT_ENCODING_HEADER;
import static org.apache.qpid.server.management.plugin.HttpManagementUtil.GZIP_CONTENT_ENCODING;
+import static org.apache.qpid.server.management.plugin.HttpManagementUtil.ensureFilenameIsRfc2183;
import java.io.IOException;
import java.io.OutputStream;
@@ -66,6 +67,11 @@ import org.apache.qpid.server.util.ConnectionScopedRuntimeException;
public abstract class AbstractServlet extends HttpServlet
{
public static final int SC_UNPROCESSABLE_ENTITY = 422;
+ /**
+ * Signifies that the agent wishes the servlet to set the Content-Disposition on the
+ * response with the value attachment. This filename will be derived from the parameter value.
+ */
+ public static final String CONTENT_DISPOSITION_ATTACHMENT_FILENAME_PARAM = "contentDispositionAttachmentFilename";
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractServlet.class);
public static final String CONTENT_DISPOSITION = "Content-Disposition";
@@ -158,6 +164,23 @@ public abstract class AbstractServlet extends HttpServlet
}
}
+ protected void setContentDispositionHeaderIfNecessary(final HttpServletResponse response,
+ final String attachmentFilename)
+ {
+ if (attachmentFilename != null)
+ {
+ String filenameRfc2183 = ensureFilenameIsRfc2183(attachmentFilename);
+ if (filenameRfc2183.length() > 0)
+ {
+ response.setHeader(CONTENT_DISPOSITION, String.format("attachment; filename=\"%s\"", filenameRfc2183));
+ }
+ else
+ {
+ response.setHeader(CONTENT_DISPOSITION, "attachment"); // Agent will allow user to choose a name
+ }
+ }
+ }
+
protected void doPut(HttpServletRequest req,
HttpServletResponse resp,
final ConfiguredObject<?> managedObject) throws ServletException, IOException
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/a2920afb/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/QueryServlet.java
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/QueryServlet.java b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/QueryServlet.java
index 2b72295..1214a34 100644
--- a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/QueryServlet.java
+++ b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/QueryServlet.java
@@ -21,6 +21,8 @@
package org.apache.qpid.server.management.plugin.servlet.rest;
import java.io.IOException;
+import java.io.PrintWriter;
+import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -33,6 +35,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.qpid.server.filter.SelectorParsingException;
+import org.apache.qpid.server.management.plugin.csv.CSVFormat;
import org.apache.qpid.server.management.plugin.servlet.query.ConfiguredObjectQuery;
import org.apache.qpid.server.management.plugin.servlet.query.EvaluationException;
import org.apache.qpid.server.model.ConfiguredObject;
@@ -42,6 +45,7 @@ public abstract class QueryServlet<X extends ConfiguredObject<?>> extends Abstra
{
private static final Logger LOGGER = LoggerFactory.getLogger(QueryServlet.class);
+ private static final CSVFormat CSV_FORMAT = new CSVFormat();
@Override
protected void doGet(HttpServletRequest request,
@@ -78,7 +82,6 @@ public abstract class QueryServlet<X extends ConfiguredObject<?>> extends Abstra
if (category != null)
{
List<ConfiguredObject<?>> objects = getAllObjects(parent, category, request);
- Map<String, Object> resultsObject = new LinkedHashMap<>();
try
{
@@ -89,10 +92,26 @@ public abstract class QueryServlet<X extends ConfiguredObject<?>> extends Abstra
request.getParameter("limit"),
request.getParameter("offset"));
- resultsObject.put("headers", query.getHeaders());
- resultsObject.put("results", query.getResults());
- resultsObject.put("total", query.getTotalNumberOfRows());
- sendJsonResponse(resultsObject, request, response);
+
+ String attachmentFilename = request.getParameter(CONTENT_DISPOSITION_ATTACHMENT_FILENAME_PARAM);
+ if (attachmentFilename != null)
+ {
+ setContentDispositionHeaderIfNecessary(response, attachmentFilename);
+ }
+
+ if ("csv".equalsIgnoreCase(request.getParameter("format")))
+ {
+ sendCsvResponse(query, response);
+ }
+ else
+ {
+ Map<String, Object> resultsObject = new LinkedHashMap<>();
+ resultsObject.put("headers", query.getHeaders());
+ resultsObject.put("results", query.getResults());
+ resultsObject.put("total", query.getTotalNumberOfRows());
+
+ sendJsonResponse(resultsObject, request, response);
+ }
}
catch (SelectorParsingException e)
{
@@ -125,6 +144,21 @@ public abstract class QueryServlet<X extends ConfiguredObject<?>> extends Abstra
}
+ private void sendCsvResponse(final ConfiguredObjectQuery query,
+ final HttpServletResponse response)
+ throws IOException
+ {
+ response.setStatus(HttpServletResponse.SC_OK);
+ response.setContentType("text/csv;charset=utf-8;");
+ response.setCharacterEncoding(StandardCharsets.UTF_8.name());
+ sendCachingHeadersOnResponse(response);
+ try (PrintWriter writer = response.getWriter())
+ {
+ CSV_FORMAT.printRecord(writer, query.getHeaders());
+ CSV_FORMAT.printRecords(writer, query.getResults());
+ }
+ }
+
abstract protected X getParent(final HttpServletRequest request, final ConfiguredObject<?> managedObject);
abstract protected Class<? extends ConfiguredObject> getSupportedCategory(final String categoryName,
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/a2920afb/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RestServlet.java
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RestServlet.java b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RestServlet.java
index 8361886..4e5e524 100644
--- a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RestServlet.java
+++ b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RestServlet.java
@@ -81,11 +81,6 @@ public class RestServlet extends AbstractServlet
public static final String EXTRACT_INITIAL_CONFIG_PARAM = "extractInitialConfig";
public static final String EXCLUDE_INHERITED_CONTEXT_PARAM = "excludeInheritedContext";
private static final String SINGLETON_MODEL_OBJECT_RESPONSE_AS_LIST = "singletonModelObjectResponseAsList";
- /**
- * Signifies that the agent wishes the servlet to set the Content-Disposition on the
- * response with the value attachment. This filename will be derived from the parameter value.
- */
- public static final String CONTENT_DISPOSITION_ATTACHMENT_FILENAME_PARAM = "contentDispositionAttachmentFilename";
public static final Set<String> RESERVED_PARAMS =
new HashSet<>(Arrays.asList(DEPTH_PARAM,
SORT_PARAM,
@@ -313,23 +308,6 @@ public class RestServlet extends AbstractServlet
return false;
}
- private void setContentDispositionHeaderIfNecessary(final HttpServletResponse response,
- final String attachmentFilename)
- {
- if (attachmentFilename != null)
- {
- String filenameRfc2183 = ensureFilenameIsRfc2183(attachmentFilename);
- if (filenameRfc2183.length() > 0)
- {
- response.setHeader(CONTENT_DISPOSITION, String.format("attachment; filename=\"%s\"", filenameRfc2183));
- }
- else
- {
- response.setHeader(CONTENT_DISPOSITION, "attachment"); // Agent will allow user to choose a name
- }
- }
- }
-
private Class<? extends ConfiguredObject> getConfiguredClass(HttpServletRequest request, ConfiguredObject<?> managedObject)
{
final String[] servletPathElements = request.getServletPath().split("/");
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/a2920afb/broker-plugins/management-http/src/main/java/resources/css/common.css
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/resources/css/common.css b/broker-plugins/management-http/src/main/java/resources/css/common.css
index e4b2511..b2577c7 100644
--- a/broker-plugins/management-http/src/main/java/resources/css/common.css
+++ b/broker-plugins/management-http/src/main/java/resources/css/common.css
@@ -611,6 +611,10 @@ td.advancedSearchField, col.autoWidth {
background-position: -180px -98px;
}
+.exportIcon.ui-icon {
+ background-position: -112px -112px;
+}
+
.claro .searchBox {
padding-right: 16px;
padding-left: 16px;
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/a2920afb/broker-plugins/management-http/src/main/java/resources/js/qpid/management/Management.js
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/resources/js/qpid/management/Management.js b/broker-plugins/management-http/src/main/java/resources/js/qpid/management/Management.js
index 5570636..bf56bf4 100644
--- a/broker-plugins/management-http/src/main/java/resources/js/qpid/management/Management.js
+++ b/broker-plugins/management-http/src/main/java/resources/js/qpid/management/Management.js
@@ -612,17 +612,26 @@ define(["dojo/_base/lang",
// Promise returned by dojo.request.xhr with modified then method allowing to use default error handler if none is specified.
Management.prototype.query = function (query)
{
- var url = "api/latest/" + (query.parent && query.parent.type === "virtualhost" ? "queryvhost/"
- + this.objectToPath({parent: query.parent}) : "querybroker") + (query.category ? "/"
- + query.category : "");
var request = {
- url: this.getFullUrl(url),
+ url: this.getQueryUrl(query),
query: {}
};
shallowCopy(query, request.query, ["parent", "category"]);
return this.get(request);
};
+ Management.prototype.getQueryUrl = function (query, parameters)
+ {
+ var url = "api/latest/" + (query.parent && query.parent.type === "virtualhost" ? "queryvhost/"
+ + this.objectToPath({parent: query.parent}) : "querybroker") + (query.category ? "/"
+ + query.category : "");
+ if (parameters)
+ {
+ url = url + "?" + ioQuery.objectToQuery(parameters);
+ }
+ return this.getFullUrl(url);
+ };
+
Management.prototype.savePreference = function(parentObject, preference)
{
var url = this.buildPreferenceUrl(parentObject, preference.type);
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/a2920afb/broker-plugins/management-http/src/main/java/resources/js/qpid/management/query/QueryWidget.js
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/resources/js/qpid/management/query/QueryWidget.js b/broker-plugins/management-http/src/main/java/resources/js/qpid/management/query/QueryWidget.js
index 4f6dad6..892e7ff 100644
--- a/broker-plugins/management-http/src/main/java/resources/js/qpid/management/query/QueryWidget.js
+++ b/broker-plugins/management-http/src/main/java/resources/js/qpid/management/query/QueryWidget.js
@@ -35,6 +35,7 @@ define(["dojo/_base/declare",
"dgrid/extensions/ColumnHider",
"qpid/management/query/QueryGrid",
"qpid/common/MessageDialog",
+ "dojox/uuid/generateRandomUuid",
"qpid/management/query/DropDownSelect",
"qpid/management/query/WhereExpression",
"dijit/_WidgetBase",
@@ -61,7 +62,8 @@ define(["dojo/_base/declare",
ColumnReorder,
ColumnHider,
QueryGrid,
- MessageDialog)
+ MessageDialog,
+ uuid)
{
var QueryCloneDialogForm = declare("qpid.management.query.QueryCloneDialogForm",
@@ -183,6 +185,8 @@ define(["dojo/_base/declare",
cloneButtonTooltip: null,
deleteButtonTooltip: null,
searchForm: null,
+ exportButton: null,
+ exportButtonTooltip: null,
/**
* constructor parameter
@@ -241,6 +245,7 @@ define(["dojo/_base/declare",
this.saveButton.on("click", lang.hitch(this, this._saveQuery));
this.cloneButton.on("click", lang.hitch(this, this._cloneQuery));
this.deleteButton.on("click", lang.hitch(this, this._deleteQuery));
+ this.exportButton.on("click", lang.hitch(this, this._exportQueryResults));
this._ownQuery = !this.preference
|| !this.preference.owner
@@ -248,6 +253,7 @@ define(["dojo/_base/declare",
var newQuery = !this.preference || !this.preference.createdDate;
this.saveButton.set("disabled", !this._ownQuery);
this.deleteButton.set("disabled", !this._ownQuery || newQuery);
+ this.exportButton.set("disabled", true);
if (!this._ownQuery)
{
@@ -537,6 +543,7 @@ define(["dojo/_base/declare",
zeroBased: false,
rowsPerPage: rowsPerPage,
_currentPage: currentPage,
+ allowTextSelection: true,
transformer: function (data)
{
var dataResults = data.results;
@@ -585,6 +592,7 @@ define(["dojo/_base/declare",
_queryCompleted: function (e)
{
this._buildColumnsIfHeadersChanged(e.data);
+ this.exportButton.set("disabled", !(e.data.total && e.data.total > 0));
},
_buildColumnsIfHeadersChanged: function (data)
{
@@ -977,6 +985,27 @@ define(["dojo/_base/declare",
this.emit("change", {preference: pref});
}
}
+ },
+ _exportQueryResults: function () {
+ var query = this._getQuery();
+ delete query.category;
+ var params = {};
+ for(var fieldName in query)
+ {
+ if (query.hasOwnProperty(fieldName) && query[fieldName])
+ {
+ params[fieldName] = query[fieldName];
+ }
+ }
+ params.format = "csv";
+ var id = uuid();
+ params.contentDispositionAttachmentFilename ="query-results-" + id + ".csv";
+ var url = this.management.getQueryUrl({category: this.categoryName, parent: this.parentObject}, params);
+ var iframe = document.createElement('iframe');
+ iframe.id = "query_downloader_" + id;
+ iframe.style.display = "none";
+ document.body.appendChild(iframe);
+ iframe.src = url;
}
});
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/a2920afb/broker-plugins/management-http/src/main/java/resources/query/QueryWidget.html
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/resources/query/QueryWidget.html b/broker-plugins/management-http/src/main/java/resources/query/QueryWidget.html
index ff1b471..0a28eac 100644
--- a/broker-plugins/management-http/src/main/java/resources/query/QueryWidget.html
+++ b/broker-plugins/management-http/src/main/java/resources/query/QueryWidget.html
@@ -40,6 +40,13 @@
<div data-dojo-attach-point="deleteButtonTooltip"
data-dojo-type="dijit/Tooltip"
data-dojo-props="connectId:'deleteButton_${id}',position:['below']">Delete query from preferences and close the tab</div>
+ <div id="exportButton_${id}"
+ data-dojo-type="dijit/form/Button"
+ data-dojo-attach-point="exportButton"
+ data-dojo-props="iconClass: 'exportIcon ui-icon'">Export</div>
+ <div data-dojo-attach-point="exportButtonTooltip"
+ data-dojo-type="dijit/Tooltip"
+ data-dojo-props="connectId:'exportButton_${id}',position:['below']">Export query results into CSV format</div>
<div data-dojo-type="dijit/form/Button"
data-dojo-attach-point="modeButton"
data-dojo-props="iconClass: 'advancedViewIcon ui-icon', title:'Switch to \'Advanced View\' search using SQL-like expressions'"
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/a2920afb/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/csv/CSVFormatTest.java
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/csv/CSVFormatTest.java b/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/csv/CSVFormatTest.java
index 4e08315..2e68c40 100644
--- a/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/csv/CSVFormatTest.java
+++ b/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/csv/CSVFormatTest.java
@@ -21,7 +21,9 @@
package org.apache.qpid.server.management.plugin.csv;
import java.io.StringWriter;
+import java.text.SimpleDateFormat;
import java.util.Arrays;
+import java.util.Date;
import org.apache.qpid.test.utils.QpidTestCase;
@@ -72,6 +74,18 @@ public class CSVFormatTest extends QpidTestCase
out.toString());
}
+ public void testDate() throws Exception
+ {
+ Date date = new Date();
+ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ CSVFormat csvFormat = new CSVFormat();
+ final StringWriter out = new StringWriter();
+ csvFormat.print(out, date, true);
+ assertEquals("Unexpected format ",
+ simpleDateFormat.format(date),
+ out.toString());
+ }
+
public void testPrintComments() throws Exception
{
CSVFormat csvFormat = new CSVFormat();
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org
[2/6] qpid-broker-j git commit: QPID-8103: [Broker-J] Import Common
CSV sources from revision 'eede739d18c69722ff39e8e42df6b68ae7627082'
Posted by or...@apache.org.
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/8758e7c8/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVParser.java
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVParser.java b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVParser.java
new file mode 100644
index 0000000..cebf253
--- /dev/null
+++ b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVParser.java
@@ -0,0 +1,624 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.qpid.server.management.plugin.csv;
+
+import static org.apache.qpid.server.management.plugin.csv.Token.Type.TOKEN;
+
+import java.io.Closeable;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.io.StringReader;
+import java.net.URL;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.TreeMap;
+
+/**
+ * Parses CSV files according to the specified format.
+ *
+ * Because CSV appears in many different dialects, the parser supports many formats by allowing the
+ * specification of a {@link CSVFormat}.
+ *
+ * The parser works record wise. It is not possible to go back, once a record has been parsed from the input stream.
+ *
+ * <h2>Creating instances</h2>
+ * <p>
+ * There are several static factory methods that can be used to create instances for various types of resources:
+ * </p>
+ * <ul>
+ * <li>{@link #parse(File, Charset, CSVFormat)}</li>
+ * <li>{@link #parse(String, CSVFormat)}</li>
+ * <li>{@link #parse(URL, Charset, CSVFormat)}</li>
+ * </ul>
+ * <p>
+ * Alternatively parsers can also be created by passing a {@link Reader} directly to the sole constructor.
+ *
+ * For those who like fluent APIs, parsers can be created using {@link CSVFormat#parse(Reader)} as a shortcut:
+ * </p>
+ * <pre>
+ * for(CSVRecord record : CSVFormat.EXCEL.parse(in)) {
+ * ...
+ * }
+ * </pre>
+ *
+ * <h2>Parsing record wise</h2>
+ * <p>
+ * To parse a CSV input from a file, you write:
+ * </p>
+ *
+ * <pre>
+ * File csvData = new File("/path/to/csv");
+ * CSVParser parser = CSVParser.parse(csvData, CSVFormat.RFC4180);
+ * for (CSVRecord csvRecord : parser) {
+ * ...
+ * }
+ * </pre>
+ *
+ * <p>
+ * This will read the parse the contents of the file using the
+ * <a href="http://tools.ietf.org/html/rfc4180" target="_blank">RFC 4180</a> format.
+ * </p>
+ *
+ * <p>
+ * To parse CSV input in a format like Excel, you write:
+ * </p>
+ *
+ * <pre>
+ * CSVParser parser = CSVParser.parse(csvData, CSVFormat.EXCEL);
+ * for (CSVRecord csvRecord : parser) {
+ * ...
+ * }
+ * </pre>
+ *
+ * <p>
+ * If the predefined formats don't match the format at hands, custom formats can be defined. More information about
+ * customising CSVFormats is available in {@link CSVFormat CSVFormat JavaDoc}.
+ * </p>
+ *
+ * <h2>Parsing into memory</h2>
+ * <p>
+ * If parsing record wise is not desired, the contents of the input can be read completely into memory.
+ * </p>
+ *
+ * <pre>
+ * Reader in = new StringReader("a;b\nc;d");
+ * CSVParser parser = new CSVParser(in, CSVFormat.EXCEL);
+ * List<CSVRecord> list = parser.getRecords();
+ * </pre>
+ *
+ * <p>
+ * There are two constraints that have to be kept in mind:
+ * </p>
+ *
+ * <ol>
+ * <li>Parsing into memory starts at the current position of the parser. If you have already parsed records from
+ * the input, those records will not end up in the in memory representation of your CSV data.</li>
+ * <li>Parsing into memory may consume a lot of system resources depending on the input. For example if you're
+ * parsing a 150MB file of CSV data the contents will be read completely into memory.</li>
+ * </ol>
+ *
+ * <h2>Notes</h2>
+ * <p>
+ * Internal parser state is completely covered by the format and the reader-state.
+ * </p>
+ *
+ * @see <a href="package-summary.html">package documentation for more details</a>
+ */
+public final class CSVParser implements Iterable<CSVRecord>, Closeable {
+
+ /**
+ * Creates a parser for the given {@link File}.
+ *
+ * @param file
+ * a CSV file. Must not be null.
+ * @param charset
+ * A Charset
+ * @param format
+ * the CSVFormat used for CSV parsing. Must not be null.
+ * @return a new parser
+ * @throws IllegalArgumentException
+ * If the parameters of the format are inconsistent or if either file or format are null.
+ * @throws IOException
+ * If an I/O error occurs
+ */
+ @SuppressWarnings("resource")
+ public static CSVParser parse(final File file, final Charset charset, final CSVFormat format) throws IOException {
+ Assertions.notNull(file, "file");
+ Assertions.notNull(format, "format");
+ return new CSVParser(new InputStreamReader(new FileInputStream(file), charset), format);
+ }
+
+ /**
+ * Creates a CSV parser using the given {@link CSVFormat}.
+ *
+ * <p>
+ * If you do not read all records from the given {@code reader}, you should call {@link #close()} on the parser,
+ * unless you close the {@code reader}.
+ * </p>
+ *
+ * @param inputStream
+ * an InputStream containing CSV-formatted input. Must not be null.
+ * @param charset
+ * a Charset.
+ * @param format
+ * the CSVFormat used for CSV parsing. Must not be null.
+ * @return a new CSVParser configured with the given reader and format.
+ * @throws IllegalArgumentException
+ * If the parameters of the format are inconsistent or if either reader or format are null.
+ * @throws IOException
+ * If there is a problem reading the header or skipping the first record
+ * @since 1.5
+ */
+ @SuppressWarnings("resource")
+ public static CSVParser parse(final InputStream inputStream, final Charset charset, final CSVFormat format)
+ throws IOException {
+ Assertions.notNull(inputStream, "inputStream");
+ Assertions.notNull(format, "format");
+ return parse(new InputStreamReader(inputStream, charset), format);
+ }
+
+ /**
+ * Creates a parser for the given {@link Path}.
+ *
+ * @param path
+ * a CSV file. Must not be null.
+ * @param charset
+ * A Charset
+ * @param format
+ * the CSVFormat used for CSV parsing. Must not be null.
+ * @return a new parser
+ * @throws IllegalArgumentException
+ * If the parameters of the format are inconsistent or if either file or format are null.
+ * @throws IOException
+ * If an I/O error occurs
+ * @since 1.5
+ */
+ public static CSVParser parse(final Path path, final Charset charset, final CSVFormat format) throws IOException {
+ Assertions.notNull(path, "path");
+ Assertions.notNull(format, "format");
+ return parse(Files.newBufferedReader(path, charset), format);
+ }
+
+ /**
+ * Creates a CSV parser using the given {@link CSVFormat}
+ *
+ * <p>
+ * If you do not read all records from the given {@code reader}, you should call {@link #close()} on the parser,
+ * unless you close the {@code reader}.
+ * </p>
+ *
+ * @param reader
+ * a Reader containing CSV-formatted input. Must not be null.
+ * @param format
+ * the CSVFormat used for CSV parsing. Must not be null.
+ * @return a new CSVParser configured with the given reader and format.
+ * @throws IllegalArgumentException
+ * If the parameters of the format are inconsistent or if either reader or format are null.
+ * @throws IOException
+ * If there is a problem reading the header or skipping the first record
+ * @since 1.5
+ */
+ public static CSVParser parse(final Reader reader, final CSVFormat format) throws IOException {
+ return new CSVParser(reader, format);
+ }
+
+ /**
+ * Creates a parser for the given {@link String}.
+ *
+ * @param string
+ * a CSV string. Must not be null.
+ * @param format
+ * the CSVFormat used for CSV parsing. Must not be null.
+ * @return a new parser
+ * @throws IllegalArgumentException
+ * If the parameters of the format are inconsistent or if either string or format are null.
+ * @throws IOException
+ * If an I/O error occurs
+ */
+ public static CSVParser parse(final String string, final CSVFormat format) throws IOException {
+ Assertions.notNull(string, "string");
+ Assertions.notNull(format, "format");
+
+ return new CSVParser(new StringReader(string), format);
+ }
+
+ /**
+ * Creates a parser for the given URL.
+ *
+ * <p>
+ * If you do not read all records from the given {@code url}, you should call {@link #close()} on the parser, unless
+ * you close the {@code url}.
+ * </p>
+ *
+ * @param url
+ * a URL. Must not be null.
+ * @param charset
+ * the charset for the resource. Must not be null.
+ * @param format
+ * the CSVFormat used for CSV parsing. Must not be null.
+ * @return a new parser
+ * @throws IllegalArgumentException
+ * If the parameters of the format are inconsistent or if either url, charset or format are null.
+ * @throws IOException
+ * If an I/O error occurs
+ */
+ public static CSVParser parse(final URL url, final Charset charset, final CSVFormat format) throws IOException {
+ Assertions.notNull(url, "url");
+ Assertions.notNull(charset, "charset");
+ Assertions.notNull(format, "format");
+
+ return new CSVParser(new InputStreamReader(url.openStream(), charset), format);
+ }
+
+ // the following objects are shared to reduce garbage
+
+ private final CSVFormat format;
+
+ /** A mapping of column names to column indices */
+ private final Map<String, Integer> headerMap;
+
+ private final Lexer lexer;
+
+ /** A record buffer for getRecord(). Grows as necessary and is reused. */
+ private final List<String> recordList = new ArrayList<>();
+
+ /**
+ * The next record number to assign.
+ */
+ private long recordNumber;
+
+ /**
+ * Lexer offset when the parser does not start parsing at the beginning of the source. Usually used in combination
+ * with {@link #recordNumber}.
+ */
+ private final long characterOffset;
+
+ private final Token reusableToken = new Token();
+
+ /**
+ * Customized CSV parser using the given {@link CSVFormat}
+ *
+ * <p>
+ * If you do not read all records from the given {@code reader}, you should call {@link #close()} on the parser,
+ * unless you close the {@code reader}.
+ * </p>
+ *
+ * @param reader
+ * a Reader containing CSV-formatted input. Must not be null.
+ * @param format
+ * the CSVFormat used for CSV parsing. Must not be null.
+ * @throws IllegalArgumentException
+ * If the parameters of the format are inconsistent or if either reader or format are null.
+ * @throws IOException
+ * If there is a problem reading the header or skipping the first record
+ */
+ public CSVParser(final Reader reader, final CSVFormat format) throws IOException {
+ this(reader, format, 0, 1);
+ }
+
+ /**
+ * Customized CSV parser using the given {@link CSVFormat}
+ *
+ * <p>
+ * If you do not read all records from the given {@code reader}, you should call {@link #close()} on the parser,
+ * unless you close the {@code reader}.
+ * </p>
+ *
+ * @param reader
+ * a Reader containing CSV-formatted input. Must not be null.
+ * @param format
+ * the CSVFormat used for CSV parsing. Must not be null.
+ * @param characterOffset
+ * Lexer offset when the parser does not start parsing at the beginning of the source.
+ * @param recordNumber
+ * The next record number to assign
+ * @throws IllegalArgumentException
+ * If the parameters of the format are inconsistent or if either reader or format are null.
+ * @throws IOException
+ * If there is a problem reading the header or skipping the first record
+ * @since 1.1
+ */
+ @SuppressWarnings("resource")
+ public CSVParser(final Reader reader, final CSVFormat format, final long characterOffset, final long recordNumber)
+ throws IOException {
+ Assertions.notNull(reader, "reader");
+ Assertions.notNull(format, "format");
+
+ this.format = format;
+ this.lexer = new Lexer(format, new ExtendedBufferedReader(reader));
+ this.headerMap = this.initializeHeader();
+ this.characterOffset = characterOffset;
+ this.recordNumber = recordNumber - 1;
+ }
+
+ private void addRecordValue(final boolean lastRecord) {
+ final String input = this.reusableToken.content.toString();
+ final String inputClean = this.format.getTrim() ? input.trim() : input;
+ if (lastRecord && inputClean.isEmpty() && this.format.getTrailingDelimiter()) {
+ return;
+ }
+ final String nullString = this.format.getNullString();
+ this.recordList.add(inputClean.equals(nullString) ? null : inputClean);
+ }
+
+ /**
+ * Closes resources.
+ *
+ * @throws IOException
+ * If an I/O error occurs
+ */
+ @Override
+ public void close() throws IOException {
+ if (this.lexer != null) {
+ this.lexer.close();
+ }
+ }
+
+ /**
+ * Returns the current line number in the input stream.
+ *
+ * <p>
+ * <strong>ATTENTION:</strong> If your CSV input has multi-line values, the returned number does not correspond to
+ * the record number.
+ * </p>
+ *
+ * @return current line number
+ */
+ public long getCurrentLineNumber() {
+ return this.lexer.getCurrentLineNumber();
+ }
+
+ /**
+ * Gets the first end-of-line string encountered.
+ *
+ * @return the first end-of-line string
+ * @since 1.5
+ */
+ public String getFirstEndOfLine() {
+ return lexer.getFirstEol();
+ }
+
+ /**
+ * Returns a copy of the header map that iterates in column order.
+ * <p>
+ * The map keys are column names. The map values are 0-based indices.
+ * </p>
+ * @return a copy of the header map that iterates in column order.
+ */
+ public Map<String, Integer> getHeaderMap() {
+ return this.headerMap == null ? null : new LinkedHashMap<>(this.headerMap);
+ }
+
+ /**
+ * Returns the current record number in the input stream.
+ *
+ * <p>
+ * <strong>ATTENTION:</strong> If your CSV input has multi-line values, the returned number does not correspond to
+ * the line number.
+ * </p>
+ *
+ * @return current record number
+ */
+ public long getRecordNumber() {
+ return this.recordNumber;
+ }
+
+ /**
+ * Parses the CSV input according to the given format and returns the content as a list of
+ * {@link CSVRecord CSVRecords}.
+ *
+ * <p>
+ * The returned content starts at the current parse-position in the stream.
+ * </p>
+ *
+ * @return list of {@link CSVRecord CSVRecords}, may be empty
+ * @throws IOException
+ * on parse error or input read-failure
+ */
+ public List<CSVRecord> getRecords() throws IOException {
+ CSVRecord rec;
+ final List<CSVRecord> records = new ArrayList<>();
+ while ((rec = this.nextRecord()) != null) {
+ records.add(rec);
+ }
+ return records;
+ }
+
+ /**
+ * Initializes the name to index mapping if the format defines a header.
+ *
+ * @return null if the format has no header.
+ * @throws IOException if there is a problem reading the header or skipping the first record
+ */
+ private Map<String, Integer> initializeHeader() throws IOException {
+ Map<String, Integer> hdrMap = null;
+ final String[] formatHeader = this.format.getHeader();
+ if (formatHeader != null) {
+ hdrMap = this.format.getIgnoreHeaderCase() ?
+ new TreeMap<String, Integer>(String.CASE_INSENSITIVE_ORDER) :
+ new LinkedHashMap<String, Integer>();
+
+ String[] headerRecord = null;
+ if (formatHeader.length == 0) {
+ // read the header from the first line of the file
+ final CSVRecord nextRecord = this.nextRecord();
+ if (nextRecord != null) {
+ headerRecord = nextRecord.values();
+ }
+ } else {
+ if (this.format.getSkipHeaderRecord()) {
+ this.nextRecord();
+ }
+ headerRecord = formatHeader;
+ }
+
+ // build the name to index mappings
+ if (headerRecord != null) {
+ for (int i = 0; i < headerRecord.length; i++) {
+ final String header = headerRecord[i];
+ final boolean containsHeader = hdrMap.containsKey(header);
+ final boolean emptyHeader = header == null || header.trim().isEmpty();
+ if (containsHeader && (!emptyHeader || !this.format.getAllowMissingColumnNames())) {
+ throw new IllegalArgumentException("The header contains a duplicate name: \"" + header +
+ "\" in " + Arrays.toString(headerRecord));
+ }
+ hdrMap.put(header, Integer.valueOf(i));
+ }
+ }
+ }
+ return hdrMap;
+ }
+
+ /**
+ * Gets whether this parser is closed.
+ *
+ * @return whether this parser is closed.
+ */
+ public boolean isClosed() {
+ return this.lexer.isClosed();
+ }
+
+ /**
+ * Returns an iterator on the records.
+ *
+ * <p>
+ * An {@link IOException} caught during the iteration are re-thrown as an
+ * {@link IllegalStateException}.
+ * </p>
+ * <p>
+ * If the parser is closed a call to {@link Iterator#next()} will throw a
+ * {@link NoSuchElementException}.
+ * </p>
+ */
+ @Override
+ public Iterator<CSVRecord> iterator() {
+ return new Iterator<CSVRecord>() {
+ private CSVRecord current;
+
+ private CSVRecord getNextRecord() {
+ try {
+ return CSVParser.this.nextRecord();
+ } catch (final IOException e) {
+ throw new IllegalStateException(
+ e.getClass().getSimpleName() + " reading next record: " + e.toString(), e);
+ }
+ }
+
+ @Override
+ public boolean hasNext() {
+ if (CSVParser.this.isClosed()) {
+ return false;
+ }
+ if (this.current == null) {
+ this.current = this.getNextRecord();
+ }
+
+ return this.current != null;
+ }
+
+ @Override
+ public CSVRecord next() {
+ if (CSVParser.this.isClosed()) {
+ throw new NoSuchElementException("CSVParser has been closed");
+ }
+ CSVRecord next = this.current;
+ this.current = null;
+
+ if (next == null) {
+ // hasNext() wasn't called before
+ next = this.getNextRecord();
+ if (next == null) {
+ throw new NoSuchElementException("No more CSV records available");
+ }
+ }
+
+ return next;
+ }
+
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ };
+ }
+
+ /**
+ * Parses the next record from the current point in the stream.
+ *
+ * @return the record as an array of values, or {@code null} if the end of the stream has been reached
+ * @throws IOException
+ * on parse error or input read-failure
+ */
+ CSVRecord nextRecord() throws IOException {
+ CSVRecord result = null;
+ this.recordList.clear();
+ StringBuilder sb = null;
+ final long startCharPosition = lexer.getCharacterPosition() + this.characterOffset;
+ do {
+ this.reusableToken.reset();
+ this.lexer.nextToken(this.reusableToken);
+ switch (this.reusableToken.type) {
+ case TOKEN:
+ this.addRecordValue(false);
+ break;
+ case EORECORD:
+ this.addRecordValue(true);
+ break;
+ case EOF:
+ if (this.reusableToken.isReady) {
+ this.addRecordValue(true);
+ }
+ break;
+ case INVALID:
+ throw new IOException("(line " + this.getCurrentLineNumber() + ") invalid parse sequence");
+ case COMMENT: // Ignored currently
+ if (sb == null) { // first comment for this record
+ sb = new StringBuilder();
+ } else {
+ sb.append(Constants.LF);
+ }
+ sb.append(this.reusableToken.content);
+ this.reusableToken.type = TOKEN; // Read another token
+ break;
+ default:
+ throw new IllegalStateException("Unexpected Token type: " + this.reusableToken.type);
+ }
+ } while (this.reusableToken.type == TOKEN);
+
+ if (!this.recordList.isEmpty()) {
+ this.recordNumber++;
+ final String comment = sb == null ? null : sb.toString();
+ result = new CSVRecord(this.recordList.toArray(new String[this.recordList.size()]), this.headerMap, comment,
+ this.recordNumber, startCharPosition);
+ }
+ return result;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/8758e7c8/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVPrinter.java
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVPrinter.java b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVPrinter.java
new file mode 100644
index 0000000..494e445
--- /dev/null
+++ b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVPrinter.java
@@ -0,0 +1,356 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.qpid.server.management.plugin.csv;
+
+import static org.apache.qpid.server.management.plugin.csv.Constants.CR;
+import static org.apache.qpid.server.management.plugin.csv.Constants.LF;
+import static org.apache.qpid.server.management.plugin.csv.Constants.SP;
+
+import java.io.Closeable;
+import java.io.Flushable;
+import java.io.IOException;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+/**
+ * Prints values in a CSV format.
+ */
+public final class CSVPrinter implements Flushable, Closeable {
+
+ /** The place that the values get written. */
+ private final Appendable out;
+ private final CSVFormat format;
+
+ /** True if we just began a new record. */
+ private boolean newRecord = true;
+
+ /**
+ * Creates a printer that will print values to the given stream following the CSVFormat.
+ * <p>
+ * Currently, only a pure encapsulation format or a pure escaping format is supported. Hybrid formats (encapsulation
+ * and escaping with a different character) are not supported.
+ * </p>
+ *
+ * @param out
+ * stream to which to print. Must not be null.
+ * @param format
+ * the CSV format. Must not be null.
+ * @throws IOException
+ * thrown if the optional header cannot be printed.
+ * @throws IllegalArgumentException
+ * thrown if the parameters of the format are inconsistent or if either out or format are null.
+ */
+ public CSVPrinter(final Appendable out, final CSVFormat format) throws IOException {
+ Assertions.notNull(out, "out");
+ Assertions.notNull(format, "format");
+
+ this.out = out;
+ this.format = format;
+ // TODO: Is it a good idea to do this here instead of on the first call to a print method?
+ // It seems a pain to have to track whether the header has already been printed or not.
+ if (format.getHeaderComments() != null) {
+ for (final String line : format.getHeaderComments()) {
+ if (line != null) {
+ this.printComment(line);
+ }
+ }
+ }
+ if (format.getHeader() != null && !format.getSkipHeaderRecord()) {
+ this.printRecord((Object[]) format.getHeader());
+ }
+ }
+
+ // ======================================================
+ // printing implementation
+ // ======================================================
+
+ @Override
+ public void close() throws IOException {
+ close(false);
+ }
+
+ /**
+ * Closes the underlying stream with an optional flush first.
+ * @param flush whether to flush before the actual close.
+ *
+ * @throws IOException
+ * If an I/O error occurs
+ * @since 1.6
+ */
+ public void close(final boolean flush) throws IOException {
+ if (flush || format.getAutoFlush()) {
+ if (out instanceof Flushable) {
+ ((Flushable) out).flush();
+ }
+ }
+ if (out instanceof Closeable) {
+ ((Closeable) out).close();
+ }
+ }
+
+ /**
+ * Flushes the underlying stream.
+ *
+ * @throws IOException
+ * If an I/O error occurs
+ */
+ @Override
+ public void flush() throws IOException {
+ if (out instanceof Flushable) {
+ ((Flushable) out).flush();
+ }
+ }
+
+ /**
+ * Gets the target Appendable.
+ *
+ * @return the target Appendable.
+ */
+ public Appendable getOut() {
+ return this.out;
+ }
+
+ /**
+ * Prints the string as the next value on the line. The value will be escaped or encapsulated as needed.
+ *
+ * @param value
+ * value to be output.
+ * @throws IOException
+ * If an I/O error occurs
+ */
+ public void print(final Object value) throws IOException {
+ format.print(value, out, newRecord);
+ newRecord = false;
+ }
+
+ /**
+ * Prints a comment on a new line among the delimiter separated values.
+ *
+ * <p>
+ * Comments will always begin on a new line and occupy a least one full line. The character specified to start
+ * comments and a space will be inserted at the beginning of each new line in the comment.
+ * </p>
+ *
+ * If comments are disabled in the current CSV format this method does nothing.
+ *
+ * @param comment
+ * the comment to output
+ * @throws IOException
+ * If an I/O error occurs
+ */
+ public void printComment(final String comment) throws IOException {
+ if (!format.isCommentMarkerSet()) {
+ return;
+ }
+ if (!newRecord) {
+ println();
+ }
+ out.append(format.getCommentMarker().charValue());
+ out.append(SP);
+ for (int i = 0; i < comment.length(); i++) {
+ final char c = comment.charAt(i);
+ switch (c) {
+ case CR:
+ if (i + 1 < comment.length() && comment.charAt(i + 1) == LF) {
+ i++;
+ }
+ //$FALL-THROUGH$ break intentionally excluded.
+ case LF:
+ println();
+ out.append(format.getCommentMarker().charValue());
+ out.append(SP);
+ break;
+ default:
+ out.append(c);
+ break;
+ }
+ }
+ println();
+ }
+
+ /**
+ * Outputs the record separator.
+ *
+ * @throws IOException
+ * If an I/O error occurs
+ */
+ public void println() throws IOException {
+ format.println(out);
+ newRecord = true;
+ }
+
+ /**
+ * Prints the given values a single record of delimiter separated values followed by the record separator.
+ *
+ * <p>
+ * The values will be quoted if needed. Quotes and newLine characters will be escaped. This method adds the record
+ * separator to the output after printing the record, so there is no need to call {@link #println()}.
+ * </p>
+ *
+ * @param values
+ * values to output.
+ * @throws IOException
+ * If an I/O error occurs
+ */
+ public void printRecord(final Iterable<?> values) throws IOException {
+ for (final Object value : values) {
+ print(value);
+ }
+ println();
+ }
+
+ /**
+ * Prints the given values a single record of delimiter separated values followed by the record separator.
+ *
+ * <p>
+ * The values will be quoted if needed. Quotes and newLine characters will be escaped. This method adds the record
+ * separator to the output after printing the record, so there is no need to call {@link #println()}.
+ * </p>
+ *
+ * @param values
+ * values to output.
+ * @throws IOException
+ * If an I/O error occurs
+ */
+ public void printRecord(final Object... values) throws IOException {
+ format.printRecord(out, values);
+ newRecord = true;
+ }
+
+ /**
+ * Prints all the objects in the given collection handling nested collections/arrays as records.
+ *
+ * <p>
+ * If the given collection only contains simple objects, this method will print a single record like
+ * {@link #printRecord(Iterable)}. If the given collections contains nested collections/arrays those nested elements
+ * will each be printed as records using {@link #printRecord(Object...)}.
+ * </p>
+ *
+ * <p>
+ * Given the following data structure:
+ * </p>
+ *
+ * <pre>
+ * <code>
+ * List<String[]> data = ...
+ * data.add(new String[]{ "A", "B", "C" });
+ * data.add(new String[]{ "1", "2", "3" });
+ * data.add(new String[]{ "A1", "B2", "C3" });
+ * </code>
+ * </pre>
+ *
+ * <p>
+ * Calling this method will print:
+ * </p>
+ *
+ * <pre>
+ * <code>
+ * A, B, C
+ * 1, 2, 3
+ * A1, B2, C3
+ * </code>
+ * </pre>
+ *
+ * @param values
+ * the values to print.
+ * @throws IOException
+ * If an I/O error occurs
+ */
+ public void printRecords(final Iterable<?> values) throws IOException {
+ for (final Object value : values) {
+ if (value instanceof Object[]) {
+ this.printRecord((Object[]) value);
+ } else if (value instanceof Iterable) {
+ this.printRecord((Iterable<?>) value);
+ } else {
+ this.printRecord(value);
+ }
+ }
+ }
+
+ /**
+ * Prints all the objects in the given array handling nested collections/arrays as records.
+ *
+ * <p>
+ * If the given array only contains simple objects, this method will print a single record like
+ * {@link #printRecord(Object...)}. If the given collections contains nested collections/arrays those nested
+ * elements will each be printed as records using {@link #printRecord(Object...)}.
+ * </p>
+ *
+ * <p>
+ * Given the following data structure:
+ * </p>
+ *
+ * <pre>
+ * <code>
+ * String[][] data = new String[3][]
+ * data[0] = String[]{ "A", "B", "C" };
+ * data[1] = new String[]{ "1", "2", "3" };
+ * data[2] = new String[]{ "A1", "B2", "C3" };
+ * </code>
+ * </pre>
+ *
+ * <p>
+ * Calling this method will print:
+ * </p>
+ *
+ * <pre>
+ * <code>
+ * A, B, C
+ * 1, 2, 3
+ * A1, B2, C3
+ * </code>
+ * </pre>
+ *
+ * @param values
+ * the values to print.
+ * @throws IOException
+ * If an I/O error occurs
+ */
+ public void printRecords(final Object... values) throws IOException {
+ for (final Object value : values) {
+ if (value instanceof Object[]) {
+ this.printRecord((Object[]) value);
+ } else if (value instanceof Iterable) {
+ this.printRecord((Iterable<?>) value);
+ } else {
+ this.printRecord(value);
+ }
+ }
+ }
+
+ /**
+ * Prints all the objects in the given JDBC result set.
+ *
+ * @param resultSet
+ * result set the values to print.
+ * @throws IOException
+ * If an I/O error occurs
+ * @throws SQLException
+ * if a database access error occurs
+ */
+ public void printRecords(final ResultSet resultSet) throws SQLException, IOException {
+ final int columnCount = resultSet.getMetaData().getColumnCount();
+ while (resultSet.next()) {
+ for (int i = 1; i <= columnCount; i++) {
+ print(resultSet.getObject(i));
+ }
+ println();
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/8758e7c8/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVRecord.java
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVRecord.java b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVRecord.java
new file mode 100644
index 0000000..e36bfbb
--- /dev/null
+++ b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVRecord.java
@@ -0,0 +1,276 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.qpid.server.management.plugin.csv;
+
+import java.io.Serializable;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+/**
+ * A CSV record parsed from a CSV file.
+ */
+public final class CSVRecord implements Serializable, Iterable<String> {
+
+ private static final String[] EMPTY_STRING_ARRAY = new String[0];
+
+ private static final long serialVersionUID = 1L;
+
+ private final long characterPosition;
+
+ /** The accumulated comments (if any) */
+ private final String comment;
+
+ /** The column name to index mapping. */
+ private final Map<String, Integer> mapping;
+
+ /** The record number. */
+ private final long recordNumber;
+
+ /** The values of the record */
+ private final String[] values;
+
+ CSVRecord(final String[] values, final Map<String, Integer> mapping, final String comment, final long recordNumber,
+ final long characterPosition) {
+ this.recordNumber = recordNumber;
+ this.values = values != null ? values : EMPTY_STRING_ARRAY;
+ this.mapping = mapping;
+ this.comment = comment;
+ this.characterPosition = characterPosition;
+ }
+
+ /**
+ * Returns a value by {@link Enum}.
+ *
+ * @param e
+ * an enum
+ * @return the String at the given enum String
+ */
+ public String get(final Enum<?> e) {
+ return get(e.toString());
+ }
+
+ /**
+ * Returns a value by index.
+ *
+ * @param i
+ * a column index (0-based)
+ * @return the String at the given index
+ */
+ public String get(final int i) {
+ return values[i];
+ }
+
+ /**
+ * Returns a value by name.
+ *
+ * @param name
+ * the name of the column to be retrieved.
+ * @return the column value, maybe null depending on {@link CSVFormat#getNullString()}.
+ * @throws IllegalStateException
+ * if no header mapping was provided
+ * @throws IllegalArgumentException
+ * if {@code name} is not mapped or if the record is inconsistent
+ * @see #isConsistent()
+ * @see CSVFormat#withNullString(String)
+ */
+ public String get(final String name) {
+ if (mapping == null) {
+ throw new IllegalStateException(
+ "No header mapping was specified, the record values can't be accessed by name");
+ }
+ final Integer index = mapping.get(name);
+ if (index == null) {
+ throw new IllegalArgumentException(String.format("Mapping for %s not found, expected one of %s", name,
+ mapping.keySet()));
+ }
+ try {
+ return values[index.intValue()];
+ } catch (final ArrayIndexOutOfBoundsException e) {
+ throw new IllegalArgumentException(String.format(
+ "Index for header '%s' is %d but CSVRecord only has %d values!", name, index,
+ Integer.valueOf(values.length)));
+ }
+ }
+
+ /**
+ * Returns the start position of this record as a character position in the source stream. This may or may not
+ * correspond to the byte position depending on the character set.
+ *
+ * @return the position of this record in the source stream.
+ */
+ public long getCharacterPosition() {
+ return characterPosition;
+ }
+
+ /**
+ * Returns the comment for this record, if any.
+ * Note that comments are attached to the following record.
+ * If there is no following record (i.e. the comment is at EOF)
+ * the comment will be ignored.
+ *
+ * @return the comment for this record, or null if no comment for this record is available.
+ */
+ public String getComment() {
+ return comment;
+ }
+
+ /**
+ * Returns the number of this record in the parsed CSV file.
+ *
+ * <p>
+ * <strong>ATTENTION:</strong> If your CSV input has multi-line values, the returned number does not correspond to
+ * the current line number of the parser that created this record.
+ * </p>
+ *
+ * @return the number of this record.
+ * @see CSVParser#getCurrentLineNumber()
+ */
+ public long getRecordNumber() {
+ return recordNumber;
+ }
+
+ /**
+ * Tells whether the record size matches the header size.
+ *
+ * <p>
+ * Returns true if the sizes for this record match and false if not. Some programs can export files that fail this
+ * test but still produce parsable files.
+ * </p>
+ *
+ * @return true of this record is valid, false if not
+ */
+ public boolean isConsistent() {
+ return mapping == null || mapping.size() == values.length;
+ }
+
+ /**
+ * Checks whether this record has a comment, false otherwise.
+ * Note that comments are attached to the following record.
+ * If there is no following record (i.e. the comment is at EOF)
+ * the comment will be ignored.
+ *
+ * @return true if this record has a comment, false otherwise
+ * @since 1.3
+ */
+ public boolean hasComment() {
+ return comment != null;
+ }
+
+ /**
+ * Checks whether a given column is mapped, i.e. its name has been defined to the parser.
+ *
+ * @param name
+ * the name of the column to be retrieved.
+ * @return whether a given column is mapped.
+ */
+ public boolean isMapped(final String name) {
+ return mapping != null && mapping.containsKey(name);
+ }
+
+ /**
+ * Checks whether a given columns is mapped and has a value.
+ *
+ * @param name
+ * the name of the column to be retrieved.
+ * @return whether a given columns is mapped and has a value
+ */
+ public boolean isSet(final String name) {
+ return isMapped(name) && mapping.get(name).intValue() < values.length;
+ }
+
+ /**
+ * Returns an iterator over the values of this record.
+ *
+ * @return an iterator over the values of this record.
+ */
+ @Override
+ public Iterator<String> iterator() {
+ return toList().iterator();
+ }
+
+ /**
+ * Puts all values of this record into the given Map.
+ *
+ * @param map
+ * The Map to populate.
+ * @return the given map.
+ */
+ <M extends Map<String, String>> M putIn(final M map) {
+ if (mapping == null) {
+ return map;
+ }
+ for (final Entry<String, Integer> entry : mapping.entrySet()) {
+ final int col = entry.getValue().intValue();
+ if (col < values.length) {
+ map.put(entry.getKey(), values[col]);
+ }
+ }
+ return map;
+ }
+
+ /**
+ * Returns the number of values in this record.
+ *
+ * @return the number of values.
+ */
+ public int size() {
+ return values.length;
+ }
+
+ /**
+ * Converts the values to a List.
+ *
+ * TODO: Maybe make this public?
+ *
+ * @return a new List
+ */
+ private List<String> toList() {
+ return Arrays.asList(values);
+ }
+
+ /**
+ * Copies this record into a new Map. The new map is not connect
+ *
+ * @return A new Map. The map is empty if the record has no headers.
+ */
+ public Map<String, String> toMap() {
+ return putIn(new HashMap<String, String>(values.length));
+ }
+
+ /**
+ * Returns a string representation of the contents of this record. The result is constructed by comment, mapping,
+ * recordNumber and by passing the internal values array to {@link Arrays#toString(Object[])}.
+ *
+ * @return a String representation of this record.
+ */
+ @Override
+ public String toString() {
+ return "CSVRecord [comment=" + comment + ", mapping=" + mapping +
+ ", recordNumber=" + recordNumber + ", values=" +
+ Arrays.toString(values) + "]";
+ }
+
+ String[] values() {
+ return values;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/8758e7c8/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/Constants.java
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/Constants.java b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/Constants.java
new file mode 100644
index 0000000..37ec6ae
--- /dev/null
+++ b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/Constants.java
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.qpid.server.management.plugin.csv;
+
+/**
+ * Constants for this package.
+ */
+final class Constants {
+
+ static final char BACKSLASH = '\\';
+
+ static final char BACKSPACE = '\b';
+
+ static final char COMMA = ',';
+
+ /**
+ * Starts a comment, the remainder of the line is the comment.
+ */
+ static final char COMMENT = '#';
+
+ static final char CR = '\r';
+
+ /** RFC 4180 defines line breaks as CRLF */
+ static final String CRLF = "\r\n";
+
+ static final Character DOUBLE_QUOTE_CHAR = Character.valueOf('"');
+
+ static final String EMPTY = "";
+
+ /** The end of stream symbol */
+ static final int END_OF_STREAM = -1;
+
+ static final char FF = '\f';
+
+ static final char LF = '\n';
+
+ /**
+ * Unicode line separator.
+ */
+ static final String LINE_SEPARATOR = "\u2028";
+
+ /**
+ * Unicode next line.
+ */
+ static final String NEXT_LINE = "\u0085";
+
+ /**
+ * Unicode paragraph separator.
+ */
+ static final String PARAGRAPH_SEPARATOR = "\u2029";
+
+ static final char PIPE = '|';
+
+ /** ASCII record separator */
+ static final char RS = 30;
+
+ static final char SP = ' ';
+
+ static final char TAB = '\t';
+
+ /** Undefined state for the lookahead char */
+ static final int UNDEFINED = -2;
+
+ /** ASCII unit separator */
+ static final char US = 31;
+
+}
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/8758e7c8/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/ExtendedBufferedReader.java
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/ExtendedBufferedReader.java b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/ExtendedBufferedReader.java
new file mode 100644
index 0000000..47f8a2e
--- /dev/null
+++ b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/ExtendedBufferedReader.java
@@ -0,0 +1,191 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.qpid.server.management.plugin.csv;
+
+import static org.apache.qpid.server.management.plugin.csv.Constants.CR;
+import static org.apache.qpid.server.management.plugin.csv.Constants.END_OF_STREAM;
+import static org.apache.qpid.server.management.plugin.csv.Constants.LF;
+import static org.apache.qpid.server.management.plugin.csv.Constants.UNDEFINED;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.Reader;
+
+/**
+ * A special buffered reader which supports sophisticated read access.
+ * <p>
+ * In particular the reader supports a look-ahead option, which allows you to see the next char returned by
+ * {@link #read()}. This reader also tracks how many characters have been read with {@link #getPosition()}.
+ * </p>
+ */
+final class ExtendedBufferedReader extends BufferedReader {
+
+ /** The last char returned */
+ private int lastChar = UNDEFINED;
+
+ /** The count of EOLs (CR/LF/CRLF) seen so far */
+ private long eolCounter;
+
+ /** The position, which is number of characters read so far */
+ private long position;
+
+ private boolean closed;
+
+ /**
+ * Created extended buffered reader using default buffer-size
+ */
+ ExtendedBufferedReader(final Reader reader) {
+ super(reader);
+ }
+
+ @Override
+ public int read() throws IOException {
+ final int current = super.read();
+ if (current == CR || current == LF && lastChar != CR) {
+ eolCounter++;
+ }
+ lastChar = current;
+ this.position++;
+ return lastChar;
+ }
+
+ /**
+ * Returns the last character that was read as an integer (0 to 65535). This will be the last character returned by
+ * any of the read methods. This will not include a character read using the {@link #lookAhead()} method. If no
+ * character has been read then this will return {@link Constants#UNDEFINED}. If the end of the stream was reached
+ * on the last read then this will return {@link Constants#END_OF_STREAM}.
+ *
+ * @return the last character that was read
+ */
+ int getLastChar() {
+ return lastChar;
+ }
+
+ @Override
+ public int read(final char[] buf, final int offset, final int length) throws IOException {
+ if (length == 0) {
+ return 0;
+ }
+
+ final int len = super.read(buf, offset, length);
+
+ if (len > 0) {
+
+ for (int i = offset; i < offset + len; i++) {
+ final char ch = buf[i];
+ if (ch == LF) {
+ if (CR != (i > 0 ? buf[i - 1] : lastChar)) {
+ eolCounter++;
+ }
+ } else if (ch == CR) {
+ eolCounter++;
+ }
+ }
+
+ lastChar = buf[offset + len - 1];
+
+ } else if (len == -1) {
+ lastChar = END_OF_STREAM;
+ }
+
+ position += len;
+ return len;
+ }
+
+ /**
+ * Calls {@link BufferedReader#readLine()} which drops the line terminator(s). This method should only be called
+ * when processing a comment, otherwise information can be lost.
+ * <p>
+ * Increments {@link #eolCounter}
+ * <p>
+ * Sets {@link #lastChar} to {@link Constants#END_OF_STREAM} at EOF, otherwise to LF
+ *
+ * @return the line that was read, or null if reached EOF.
+ */
+ @Override
+ public String readLine() throws IOException {
+ final String line = super.readLine();
+
+ if (line != null) {
+ lastChar = LF; // needed for detecting start of line
+ eolCounter++;
+ } else {
+ lastChar = END_OF_STREAM;
+ }
+
+ return line;
+ }
+
+ /**
+ * Returns the next character in the current reader without consuming it. So the next call to {@link #read()} will
+ * still return this value. Does not affect line number or last character.
+ *
+ * @return the next character
+ *
+ * @throws IOException
+ * if there is an error in reading
+ */
+ int lookAhead() throws IOException {
+ super.mark(1);
+ final int c = super.read();
+ super.reset();
+
+ return c;
+ }
+
+ /**
+ * Returns the current line number
+ *
+ * @return the current line number
+ */
+ long getCurrentLineNumber() {
+ // Check if we are at EOL or EOF or just starting
+ if (lastChar == CR || lastChar == LF || lastChar == UNDEFINED || lastChar == END_OF_STREAM) {
+ return eolCounter; // counter is accurate
+ }
+ return eolCounter + 1; // Allow for counter being incremented only at EOL
+ }
+
+ /**
+ * Gets the character position in the reader.
+ *
+ * @return the current position in the reader (counting characters, not bytes since this is a Reader)
+ */
+ long getPosition() {
+ return this.position;
+ }
+
+ public boolean isClosed() {
+ return closed;
+ }
+
+ /**
+ * Closes the stream.
+ *
+ * @throws IOException
+ * If an I/O error occurs
+ */
+ @Override
+ public void close() throws IOException {
+ // Set ivars before calling super close() in case close() throws an IOException.
+ closed = true;
+ lastChar = END_OF_STREAM;
+ super.close();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/8758e7c8/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/Lexer.java
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/Lexer.java b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/Lexer.java
new file mode 100644
index 0000000..95a3ff0
--- /dev/null
+++ b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/Lexer.java
@@ -0,0 +1,461 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.qpid.server.management.plugin.csv;
+
+import static org.apache.qpid.server.management.plugin.csv.Constants.BACKSPACE;
+import static org.apache.qpid.server.management.plugin.csv.Constants.CR;
+import static org.apache.qpid.server.management.plugin.csv.Constants.END_OF_STREAM;
+import static org.apache.qpid.server.management.plugin.csv.Constants.FF;
+import static org.apache.qpid.server.management.plugin.csv.Constants.LF;
+import static org.apache.qpid.server.management.plugin.csv.Constants.TAB;
+import static org.apache.qpid.server.management.plugin.csv.Constants.UNDEFINED;
+import static org.apache.qpid.server.management.plugin.csv.Token.Type.COMMENT;
+import static org.apache.qpid.server.management.plugin.csv.Token.Type.EOF;
+import static org.apache.qpid.server.management.plugin.csv.Token.Type.EORECORD;
+import static org.apache.qpid.server.management.plugin.csv.Token.Type.INVALID;
+import static org.apache.qpid.server.management.plugin.csv.Token.Type.TOKEN;
+
+import java.io.Closeable;
+import java.io.IOException;
+
+/**
+ * Lexical analyzer.
+ */
+final class Lexer implements Closeable {
+
+ private static final String CR_STRING = Character.toString(Constants.CR);
+ private static final String LF_STRING = Character.toString(Constants.LF);
+
+ /**
+ * Constant char to use for disabling comments, escapes and encapsulation. The value -2 is used because it
+ * won't be confused with an EOF signal (-1), and because the Unicode value {@code FFFE} would be encoded as two
+ * chars (using surrogates) and thus there should never be a collision with a real text char.
+ */
+ private static final char DISABLED = '\ufffe';
+
+ private final char delimiter;
+ private final char escape;
+ private final char quoteChar;
+ private final char commentStart;
+
+ private final boolean ignoreSurroundingSpaces;
+ private final boolean ignoreEmptyLines;
+
+ /** The input stream */
+ private final ExtendedBufferedReader reader;
+ private String firstEol;
+
+ String getFirstEol(){
+ return firstEol;
+ }
+
+ Lexer(final CSVFormat format, final ExtendedBufferedReader reader) {
+ this.reader = reader;
+ this.delimiter = format.getDelimiter();
+ this.escape = mapNullToDisabled(format.getEscapeCharacter());
+ this.quoteChar = mapNullToDisabled(format.getQuoteCharacter());
+ this.commentStart = mapNullToDisabled(format.getCommentMarker());
+ this.ignoreSurroundingSpaces = format.getIgnoreSurroundingSpaces();
+ this.ignoreEmptyLines = format.getIgnoreEmptyLines();
+ }
+
+ /**
+ * Returns the next token.
+ * <p>
+ * A token corresponds to a term, a record change or an end-of-file indicator.
+ * </p>
+ *
+ * @param token
+ * an existing Token object to reuse. The caller is responsible to initialize the Token.
+ * @return the next token found
+ * @throws IOException
+ * on stream access error
+ */
+ Token nextToken(final Token token) throws IOException {
+
+ // get the last read char (required for empty line detection)
+ int lastChar = reader.getLastChar();
+
+ // read the next char and set eol
+ int c = reader.read();
+ /*
+ * Note: The following call will swallow LF if c == CR. But we don't need to know if the last char was CR or LF
+ * - they are equivalent here.
+ */
+ boolean eol = readEndOfLine(c);
+
+ // empty line detection: eol AND (last char was EOL or beginning)
+ if (ignoreEmptyLines) {
+ while (eol && isStartOfLine(lastChar)) {
+ // go on char ahead ...
+ lastChar = c;
+ c = reader.read();
+ eol = readEndOfLine(c);
+ // reached end of file without any content (empty line at the end)
+ if (isEndOfFile(c)) {
+ token.type = EOF;
+ // don't set token.isReady here because no content
+ return token;
+ }
+ }
+ }
+
+ // did we reach eof during the last iteration already ? EOF
+ if (isEndOfFile(lastChar) || !isDelimiter(lastChar) && isEndOfFile(c)) {
+ token.type = EOF;
+ // don't set token.isReady here because no content
+ return token;
+ }
+
+ if (isStartOfLine(lastChar) && isCommentStart(c)) {
+ final String line = reader.readLine();
+ if (line == null) {
+ token.type = EOF;
+ // don't set token.isReady here because no content
+ return token;
+ }
+ final String comment = line.trim();
+ token.content.append(comment);
+ token.type = COMMENT;
+ return token;
+ }
+
+ // important: make sure a new char gets consumed in each iteration
+ while (token.type == INVALID) {
+ // ignore whitespaces at beginning of a token
+ if (ignoreSurroundingSpaces) {
+ while (isWhitespace(c) && !eol) {
+ c = reader.read();
+ eol = readEndOfLine(c);
+ }
+ }
+
+ // ok, start of token reached: encapsulated, or token
+ if (isDelimiter(c)) {
+ // empty token return TOKEN("")
+ token.type = TOKEN;
+ } else if (eol) {
+ // empty token return EORECORD("")
+ // noop: token.content.append("");
+ token.type = EORECORD;
+ } else if (isQuoteChar(c)) {
+ // consume encapsulated token
+ parseEncapsulatedToken(token);
+ } else if (isEndOfFile(c)) {
+ // end of file return EOF()
+ // noop: token.content.append("");
+ token.type = EOF;
+ token.isReady = true; // there is data at EOF
+ } else {
+ // next token must be a simple token
+ // add removed blanks when not ignoring whitespace chars...
+ parseSimpleToken(token, c);
+ }
+ }
+ return token;
+ }
+
+ /**
+ * Parses a simple token.
+ * <p/>
+ * Simple token are tokens which are not surrounded by encapsulators. A simple token might contain escaped
+ * delimiters (as \, or \;). The token is finished when one of the following conditions become true:
+ * <ul>
+ * <li>end of line has been reached (EORECORD)</li>
+ * <li>end of stream has been reached (EOF)</li>
+ * <li>an unescaped delimiter has been reached (TOKEN)</li>
+ * </ul>
+ *
+ * @param token
+ * the current token
+ * @param ch
+ * the current character
+ * @return the filled token
+ * @throws IOException
+ * on stream access error
+ */
+ private Token parseSimpleToken(final Token token, int ch) throws IOException {
+ // Faster to use while(true)+break than while(token.type == INVALID)
+ while (true) {
+ if (readEndOfLine(ch)) {
+ token.type = EORECORD;
+ break;
+ } else if (isEndOfFile(ch)) {
+ token.type = EOF;
+ token.isReady = true; // There is data at EOF
+ break;
+ } else if (isDelimiter(ch)) {
+ token.type = TOKEN;
+ break;
+ } else if (isEscape(ch)) {
+ final int unescaped = readEscape();
+ if (unescaped == END_OF_STREAM) { // unexpected char after escape
+ token.content.append((char) ch).append((char) reader.getLastChar());
+ } else {
+ token.content.append((char) unescaped);
+ }
+ ch = reader.read(); // continue
+ } else {
+ token.content.append((char) ch);
+ ch = reader.read(); // continue
+ }
+ }
+
+ if (ignoreSurroundingSpaces) {
+ trimTrailingSpaces(token.content);
+ }
+
+ return token;
+ }
+
+ /**
+ * Parses an encapsulated token.
+ * <p/>
+ * Encapsulated tokens are surrounded by the given encapsulating-string. The encapsulator itself might be included
+ * in the token using a doubling syntax (as "", '') or using escaping (as in \", \'). Whitespaces before and after
+ * an encapsulated token are ignored. The token is finished when one of the following conditions become true:
+ * <ul>
+ * <li>an unescaped encapsulator has been reached, and is followed by optional whitespace then:</li>
+ * <ul>
+ * <li>delimiter (TOKEN)</li>
+ * <li>end of line (EORECORD)</li>
+ * </ul>
+ * <li>end of stream has been reached (EOF)</li> </ul>
+ *
+ * @param token
+ * the current token
+ * @return a valid token object
+ * @throws IOException
+ * on invalid state: EOF before closing encapsulator or invalid character before delimiter or EOL
+ */
+ private Token parseEncapsulatedToken(final Token token) throws IOException {
+ // save current line number in case needed for IOE
+ final long startLineNumber = getCurrentLineNumber();
+ int c;
+ while (true) {
+ c = reader.read();
+
+ if (isEscape(c)) {
+ final int unescaped = readEscape();
+ if (unescaped == END_OF_STREAM) { // unexpected char after escape
+ token.content.append((char) c).append((char) reader.getLastChar());
+ } else {
+ token.content.append((char) unescaped);
+ }
+ } else if (isQuoteChar(c)) {
+ if (isQuoteChar(reader.lookAhead())) {
+ // double or escaped encapsulator -> add single encapsulator to token
+ c = reader.read();
+ token.content.append((char) c);
+ } else {
+ // token finish mark (encapsulator) reached: ignore whitespace till delimiter
+ while (true) {
+ c = reader.read();
+ if (isDelimiter(c)) {
+ token.type = TOKEN;
+ return token;
+ } else if (isEndOfFile(c)) {
+ token.type = EOF;
+ token.isReady = true; // There is data at EOF
+ return token;
+ } else if (readEndOfLine(c)) {
+ token.type = EORECORD;
+ return token;
+ } else if (!isWhitespace(c)) {
+ // error invalid char between token and next delimiter
+ throw new IOException("(line " + getCurrentLineNumber() +
+ ") invalid char between encapsulated token and delimiter");
+ }
+ }
+ }
+ } else if (isEndOfFile(c)) {
+ // error condition (end of file before end of token)
+ throw new IOException("(startline " + startLineNumber +
+ ") EOF reached before encapsulated token finished");
+ } else {
+ // consume character
+ token.content.append((char) c);
+ }
+ }
+ }
+
+ private char mapNullToDisabled(final Character c) {
+ return c == null ? DISABLED : c.charValue();
+ }
+
+ /**
+ * Returns the current line number
+ *
+ * @return the current line number
+ */
+ long getCurrentLineNumber() {
+ return reader.getCurrentLineNumber();
+ }
+
+ /**
+ * Returns the current character position
+ *
+ * @return the current character position
+ */
+ long getCharacterPosition() {
+ return reader.getPosition();
+ }
+
+ // TODO escape handling needs more work
+ /**
+ * Handle an escape sequence.
+ * The current character must be the escape character.
+ * On return, the next character is available by calling {@link ExtendedBufferedReader#getLastChar()}
+ * on the input stream.
+ *
+ * @return the unescaped character (as an int) or {@link Constants#END_OF_STREAM} if char following the escape is
+ * invalid.
+ * @throws IOException if there is a problem reading the stream or the end of stream is detected:
+ * the escape character is not allowed at end of stream
+ */
+ int readEscape() throws IOException {
+ // the escape char has just been read (normally a backslash)
+ final int ch = reader.read();
+ switch (ch) {
+ case 'r':
+ return CR;
+ case 'n':
+ return LF;
+ case 't':
+ return TAB;
+ case 'b':
+ return BACKSPACE;
+ case 'f':
+ return FF;
+ case CR:
+ case LF:
+ case FF: // TODO is this correct?
+ case TAB: // TODO is this correct? Do tabs need to be escaped?
+ case BACKSPACE: // TODO is this correct?
+ return ch;
+ case END_OF_STREAM:
+ throw new IOException("EOF whilst processing escape sequence");
+ default:
+ // Now check for meta-characters
+ if (isMetaChar(ch)) {
+ return ch;
+ }
+ // indicate unexpected char - available from in.getLastChar()
+ return END_OF_STREAM;
+ }
+ }
+
+ void trimTrailingSpaces(final StringBuilder buffer) {
+ int length = buffer.length();
+ while (length > 0 && Character.isWhitespace(buffer.charAt(length - 1))) {
+ length = length - 1;
+ }
+ if (length != buffer.length()) {
+ buffer.setLength(length);
+ }
+ }
+
+ /**
+ * Greedily accepts \n, \r and \r\n This checker consumes silently the second control-character...
+ *
+ * @return true if the given or next character is a line-terminator
+ */
+ boolean readEndOfLine(int ch) throws IOException {
+ // check if we have \r\n...
+ if (ch == CR && reader.lookAhead() == LF) {
+ // note: does not change ch outside of this method!
+ ch = reader.read();
+ // Save the EOL state
+ if (firstEol == null) {
+ this.firstEol = Constants.CRLF;
+ }
+ }
+ // save EOL state here.
+ if (firstEol == null) {
+ if (ch == LF) {
+ this.firstEol = LF_STRING;
+ } else if (ch == CR) {
+ this.firstEol = CR_STRING;
+ }
+ }
+
+ return ch == LF || ch == CR;
+ }
+
+ boolean isClosed() {
+ return reader.isClosed();
+ }
+
+ /**
+ * @return true if the given char is a whitespace character
+ */
+ boolean isWhitespace(final int ch) {
+ return !isDelimiter(ch) && Character.isWhitespace((char) ch);
+ }
+
+ /**
+ * Checks if the current character represents the start of a line: a CR, LF or is at the start of the file.
+ *
+ * @param ch the character to check
+ * @return true if the character is at the start of a line.
+ */
+ boolean isStartOfLine(final int ch) {
+ return ch == LF || ch == CR || ch == UNDEFINED;
+ }
+
+ /**
+ * @return true if the given character indicates end of file
+ */
+ boolean isEndOfFile(final int ch) {
+ return ch == END_OF_STREAM;
+ }
+
+ boolean isDelimiter(final int ch) {
+ return ch == delimiter;
+ }
+
+ boolean isEscape(final int ch) {
+ return ch == escape;
+ }
+
+ boolean isQuoteChar(final int ch) {
+ return ch == quoteChar;
+ }
+
+ boolean isCommentStart(final int ch) {
+ return ch == commentStart;
+ }
+
+ private boolean isMetaChar(final int ch) {
+ return ch == delimiter ||
+ ch == escape ||
+ ch == quoteChar ||
+ ch == commentStart;
+ }
+
+ /**
+ * Closes resources.
+ *
+ * @throws IOException
+ * If an I/O error occurs
+ */
+ @Override
+ public void close() throws IOException {
+ reader.close();
+ }
+}
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/8758e7c8/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/QuoteMode.java
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/QuoteMode.java b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/QuoteMode.java
new file mode 100644
index 0000000..25c6b31
--- /dev/null
+++ b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/QuoteMode.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.qpid.server.management.plugin.csv;
+
+/**
+ * Defines quoting behavior when printing.
+ */
+public enum QuoteMode {
+
+ /**
+ * Quotes all fields.
+ */
+ ALL,
+
+ /**
+ * Quotes all non-null fields.
+ */
+ ALL_NON_NULL,
+
+ /**
+ * Quotes fields which contain special characters such as a the field delimiter, quote character or any of the
+ * characters in the line separator string.
+ */
+ MINIMAL,
+
+ /**
+ * Quotes all non-numeric fields.
+ */
+ NON_NUMERIC,
+
+ /**
+ * Never quotes fields. When the delimiter occurs in data, the printer prefixes it with the escape character. If the
+ * escape character is not set, format validation throws an exception.
+ */
+ NONE
+}
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/8758e7c8/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/Token.java
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/Token.java b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/Token.java
new file mode 100644
index 0000000..c3c94e1
--- /dev/null
+++ b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/Token.java
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.qpid.server.management.plugin.csv;
+
+import static org.apache.qpid.server.management.plugin.csv.Token.Type.INVALID;
+
+/**
+ * Internal token representation.
+ * <p/>
+ * It is used as contract between the lexer and the parser.
+ */
+final class Token {
+
+ /** length of the initial token (content-)buffer */
+ private static final int INITIAL_TOKEN_LENGTH = 50;
+
+ enum Type {
+ /** Token has no valid content, i.e. is in its initialized state. */
+ INVALID,
+
+ /** Token with content, at beginning or in the middle of a line. */
+ TOKEN,
+
+ /** Token (which can have content) when the end of file is reached. */
+ EOF,
+
+ /** Token with content when the end of a line is reached. */
+ EORECORD,
+
+ /** Token is a comment line. */
+ COMMENT
+ }
+
+ /** Token type */
+ Token.Type type = INVALID;
+
+ /** The content buffer. */
+ final StringBuilder content = new StringBuilder(INITIAL_TOKEN_LENGTH);
+
+ /** Token ready flag: indicates a valid token with content (ready for the parser). */
+ boolean isReady;
+
+ void reset() {
+ content.setLength(0);
+ type = INVALID;
+ isReady = false;
+ }
+
+ /**
+ * Eases IDE debugging.
+ *
+ * @return a string helpful for debugging.
+ */
+ @Override
+ public String toString() {
+ return type.name() + " [" + content.toString() + "]";
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org
[5/6] qpid-broker-j git commit: QPID-8103: [Broker-J] Leave
minimalistic implementation of CSV format
Posted by or...@apache.org.
QPID-8103: [Broker-J] Leave minimalistic implementation of CSV format
Project: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/commit/8d0e68fc
Tree: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/tree/8d0e68fc
Diff: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/diff/8d0e68fc
Branch: refs/heads/master
Commit: 8d0e68fc5178e976696e96ed28d4ccdb820300f4
Parents: 8758e7c
Author: Alex Rudyy <or...@apache.org>
Authored: Mon Feb 26 16:07:41 2018 +0000
Committer: Alex Rudyy <or...@apache.org>
Committed: Mon Feb 26 16:40:48 2018 +0000
----------------------------------------------------------------------
.../management/plugin/csv/Assertions.java | 38 -
.../server/management/plugin/csv/CSVFormat.java | 2121 ++----------------
.../server/management/plugin/csv/CSVParser.java | 624 ------
.../management/plugin/csv/CSVPrinter.java | 356 ---
.../server/management/plugin/csv/CSVRecord.java | 276 ---
.../server/management/plugin/csv/Constants.java | 82 -
.../plugin/csv/ExtendedBufferedReader.java | 191 --
.../server/management/plugin/csv/Lexer.java | 461 ----
.../server/management/plugin/csv/QuoteMode.java | 50 -
.../server/management/plugin/csv/Token.java | 73 -
.../management/plugin/csv/CSVFormatTest.java | 84 +
11 files changed, 282 insertions(+), 4074 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/8d0e68fc/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/Assertions.java
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/Assertions.java b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/Assertions.java
deleted file mode 100644
index 63e8d17..0000000
--- a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/Assertions.java
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.qpid.server.management.plugin.csv;
-
-import java.util.Objects;
-
-/**
- * Utility class for input parameter validation.
- *
- * TODO Replace usage with {@link Objects} when we switch to Java 7.
- */
-final class Assertions {
-
- private Assertions() {
- // can not be instantiated
- }
-
- public static void notNull(final Object parameter, final String parameterName) {
- if (parameter == null) {
- throw new IllegalArgumentException("Parameter '" + parameterName + "' must not be null!");
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/8d0e68fc/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVFormat.java
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVFormat.java b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVFormat.java
index 97a0b6c..5636477 100644
--- a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVFormat.java
+++ b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVFormat.java
@@ -1,1983 +1,258 @@
/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
*
- * 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.qpid.server.management.plugin.csv;
-import static org.apache.qpid.server.management.plugin.csv.Constants.*;
-import java.io.File;
-import java.io.FileOutputStream;
import java.io.IOException;
-import java.io.OutputStreamWriter;
-import java.io.Reader;
-import java.io.Serializable;
-import java.io.StringWriter;
-import java.nio.charset.Charset;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.sql.ResultSet;
-import java.sql.ResultSetMetaData;
-import java.sql.SQLException;
-import java.util.Arrays;
-import java.util.HashSet;
-import java.util.Set;
+import java.util.Collection;
/**
- * Specifies the format of a CSV file and parses input.
- *
- * <h2>Using predefined formats</h2>
- *
- * <p>
- * You can use one of the predefined formats:
- * </p>
- *
- * <ul>
- * <li>{@link #DEFAULT}</li>
- * <li>{@link #EXCEL}</li>
- * <li>{@link #MYSQL}</li>
- * <li>{@link #RFC4180}</li>
- * <li>{@link #TDF}</li>
- * </ul>
- *
- * <p>
- * For example:
- * </p>
- *
- * <pre>
- * CSVParser parser = CSVFormat.EXCEL.parse(reader);
- * </pre>
- *
- * <p>
- * The {@link CSVParser} provides static methods to parse other input types, for example:
- * </p>
- *
- * <pre>
- * CSVParser parser = CSVParser.parse(file, StandardCharsets.US_ASCII, CSVFormat.EXCEL);
- * </pre>
- *
- * <h2>Defining formats</h2>
- *
- * <p>
- * You can extend a format by calling the {@code with} methods. For example:
- * </p>
- *
- * <pre>
- * CSVFormat.EXCEL.withNullString("N/A").withIgnoreSurroundingSpaces(true);
- * </pre>
- *
- * <h2>Defining column names</h2>
- *
- * <p>
- * To define the column names you want to use to access records, write:
- * </p>
- *
- * <pre>
- * CSVFormat.EXCEL.withHeader("Col1", "Col2", "Col3");
- * </pre>
- *
- * <p>
- * Calling {@link #withHeader(String...)} let's you use the given names to address values in a {@link CSVRecord}, and
- * assumes that your CSV source does not contain a first record that also defines column names.
- *
- * If it does, then you are overriding this metadata with your names and you should skip the first record by calling
- * {@link #withSkipHeaderRecord(boolean)} with {@code true}.
- * </p>
- *
- * <h2>Parsing</h2>
- *
- * <p>
- * You can use a format directly to parse a reader. For example, to parse an Excel file with columns header, write:
- * </p>
- *
- * <pre>
- * Reader in = ...;
- * CSVFormat.EXCEL.withHeader("Col1", "Col2", "Col3").parse(in);
- * </pre>
- *
- * <p>
- * For other input types, like resources, files, and URLs, use the static methods on {@link CSVParser}.
- * </p>
- *
- * <h2>Referencing columns safely</h2>
- *
- * <p>
- * If your source contains a header record, you can simplify your code and safely reference columns, by using
- * {@link #withHeader(String...)} with no arguments:
- * </p>
- *
- * <pre>
- * CSVFormat.EXCEL.withHeader();
- * </pre>
- *
- * <p>
- * This causes the parser to read the first record and use its values as column names.
- *
- * Then, call one of the {@link CSVRecord} get method that takes a String column name argument:
- * </p>
- *
- * <pre>
- * String value = record.get("Col1");
- * </pre>
- *
- * <p>
- * This makes your code impervious to changes in column order in the CSV file.
- * </p>
- *
- * <h2>Notes</h2>
- *
- * <p>
- * This class is immutable.
- * </p>
+ * Simplified version of CSVFormat from Apache Commons CSV
*/
-public final class CSVFormat implements Serializable {
-
- /**
- * Predefines formats.
- *
- * @since 1.2
- */
- public enum Predefined {
-
- /**
- * @see CSVFormat#DEFAULT
- */
- Default(CSVFormat.DEFAULT),
+public final class CSVFormat
+{
+ private static final char COMMA = ',';
- /**
- * @see CSVFormat#EXCEL
- */
- Excel(CSVFormat.EXCEL),
+ private static final char COMMENT = '#';
- /**
- * @see CSVFormat#INFORMIX_UNLOAD
- * @since 1.3
- */
- InformixUnload(CSVFormat.INFORMIX_UNLOAD),
+ private static final char CR = '\r';
- /**
- * @see CSVFormat#INFORMIX_UNLOAD_CSV
- * @since 1.3
- */
- InformixUnloadCsv(CSVFormat.INFORMIX_UNLOAD_CSV),
+ private static final String CRLF = "\r\n";
- /**
- * @see CSVFormat#MYSQL
- */
- MySQL(CSVFormat.MYSQL),
+ private static final Character DOUBLE_QUOTE_CHAR = '"';
- /**
- * @see CSVFormat#POSTGRESQL_CSV
- * @since 1.5
- */
- PostgreSQLCsv(CSVFormat.POSTGRESQL_CSV),
+ private static final String EMPTY = "";
- /**
- * @see CSVFormat#POSTGRESQL_CSV
- */
- PostgreSQLText(CSVFormat.POSTGRESQL_TEXT),
+ private static final char LF = '\n';
- /**
- * @see CSVFormat#RFC4180
- */
- RFC4180(CSVFormat.RFC4180),
+ private static final char SP = ' ';
- /**
- * @see CSVFormat#TDF
- */
- TDF(CSVFormat.TDF);
+ private final char _delimiter;
- private final CSVFormat format;
+ private final Character _quoteCharacter; // null if quoting is disabled
- Predefined(final CSVFormat format) {
- this.format = format;
- }
+ private final String _recordSeparator; // for outputs
- /**
- * Gets the format.
- *
- * @return the format.
- */
- public CSVFormat getFormat() {
- return format;
- }
+ public CSVFormat()
+ {
+ this(COMMA, DOUBLE_QUOTE_CHAR, CRLF);
}
/**
- * Standard comma separated format, as for {@link #RFC4180} but allowing empty lines.
- *
- * <p>
- * Settings are:
- * </p>
- * <ul>
- * <li>withDelimiter(',')</li>
- * <li>withQuote('"')</li>
- * <li>withRecordSeparator("\r\n")</li>
- * <li>withIgnoreEmptyLines(true)</li>
- * </ul>
- *
- * @see Predefined#Default
- */
- public static final CSVFormat DEFAULT = new CSVFormat(COMMA, DOUBLE_QUOTE_CHAR, null, null, null, false, true, CRLF,
- null, null, null, false, false, false, false, false, false);
-
- /**
- * Excel file format (using a comma as the value delimiter). Note that the actual value delimiter used by Excel is
- * locale dependent, it might be necessary to customize this format to accommodate to your regional settings.
- *
- * <p>
- * For example for parsing or generating a CSV file on a French system the following format will be used:
- * </p>
- *
- * <pre>
- * CSVFormat fmt = CSVFormat.EXCEL.withDelimiter(';');
- * </pre>
- *
- * <p>
- * Settings are:
- * </p>
- * <ul>
- * <li>{@link #withDelimiter(char) withDelimiter(',')}</li>
- * <li>{@link #withQuote(char) withQuote('"')}</li>
- * <li>{@link #withRecordSeparator(String) withRecordSeparator("\r\n")}</li>
- * <li>{@link #withIgnoreEmptyLines(boolean) withIgnoreEmptyLines(false)}</li>
- * <li>{@link #withAllowMissingColumnNames(boolean) withAllowMissingColumnNames(true)}</li>
- * </ul>
- * <p>
- * Note: this is currently like {@link #RFC4180} plus {@link #withAllowMissingColumnNames(boolean)
- * withAllowMissingColumnNames(true)}.
- * </p>
- *
- * @see Predefined#Excel
- */
- // @formatter:off
- public static final CSVFormat EXCEL = DEFAULT
- .withIgnoreEmptyLines(false)
- .withAllowMissingColumnNames();
- // @formatter:on
-
- /**
- * Default Informix CSV UNLOAD format used by the {@code UNLOAD TO file_name} operation.
- *
- * <p>
- * This is a comma-delimited format with a LF character as the line separator. Values are not quoted and special
- * characters are escaped with {@code '\'}. The default NULL string is {@code "\\N"}.
- * </p>
- *
- * <p>
- * Settings are:
- * </p>
- * <ul>
- * <li>withDelimiter(',')</li>
- * <li>withQuote("\"")</li>
- * <li>withRecordSeparator('\n')</li>
- * <li>withEscape('\\')</li>
- * </ul>
- *
- * @see Predefined#MySQL
- * @see <a href=
- * "http://www.ibm.com/support/knowledgecenter/SSBJG3_2.5.0/com.ibm.gen_busug.doc/c_fgl_InOutSql_UNLOAD.htm">
- * http://www.ibm.com/support/knowledgecenter/SSBJG3_2.5.0/com.ibm.gen_busug.doc/c_fgl_InOutSql_UNLOAD.htm</a>
- * @since 1.3
- */
- // @formatter:off
- public static final CSVFormat INFORMIX_UNLOAD = DEFAULT
- .withDelimiter(PIPE)
- .withEscape(BACKSLASH)
- .withQuote(DOUBLE_QUOTE_CHAR)
- .withRecordSeparator(LF);
- // @formatter:on
-
- /**
- * Default Informix CSV UNLOAD format used by the {@code UNLOAD TO file_name} operation (escaping is disabled.)
- *
- * <p>
- * This is a comma-delimited format with a LF character as the line separator. Values are not quoted and special
- * characters are escaped with {@code '\'}. The default NULL string is {@code "\\N"}.
- * </p>
- *
- * <p>
- * Settings are:
- * </p>
- * <ul>
- * <li>withDelimiter(',')</li>
- * <li>withQuote("\"")</li>
- * <li>withRecordSeparator('\n')</li>
- * </ul>
- *
- * @see Predefined#MySQL
- * @see <a href=
- * "http://www.ibm.com/support/knowledgecenter/SSBJG3_2.5.0/com.ibm.gen_busug.doc/c_fgl_InOutSql_UNLOAD.htm">
- * http://www.ibm.com/support/knowledgecenter/SSBJG3_2.5.0/com.ibm.gen_busug.doc/c_fgl_InOutSql_UNLOAD.htm</a>
- * @since 1.3
- */
- // @formatter:off
- public static final CSVFormat INFORMIX_UNLOAD_CSV = DEFAULT
- .withDelimiter(COMMA)
- .withQuote(DOUBLE_QUOTE_CHAR)
- .withRecordSeparator(LF);
- // @formatter:on
-
- /**
- * Default MySQL format used by the {@code SELECT INTO OUTFILE} and {@code LOAD DATA INFILE} operations.
- *
- * <p>
- * This is a tab-delimited format with a LF character as the line separator. Values are not quoted and special
- * characters are escaped with {@code '\'}. The default NULL string is {@code "\\N"}.
- * </p>
- *
- * <p>
- * Settings are:
- * </p>
- * <ul>
- * <li>withDelimiter('\t')</li>
- * <li>withQuote(null)</li>
- * <li>withRecordSeparator('\n')</li>
- * <li>withIgnoreEmptyLines(false)</li>
- * <li>withEscape('\\')</li>
- * <li>withNullString("\\N")</li>
- * <li>withQuoteMode(QuoteMode.ALL_NON_NULL)</li>
- * </ul>
- *
- * @see Predefined#MySQL
- * @see <a href="http://dev.mysql.com/doc/refman/5.1/en/load-data.html"> http://dev.mysql.com/doc/refman/5.1/en/load
- * -data.html</a>
- */
- // @formatter:off
- public static final CSVFormat MYSQL = DEFAULT
- .withDelimiter(TAB)
- .withEscape(BACKSLASH)
- .withIgnoreEmptyLines(false)
- .withQuote(null)
- .withRecordSeparator(LF)
- .withNullString("\\N")
- .withQuoteMode(QuoteMode.ALL_NON_NULL);
- // @formatter:off
-
- /**
- * Default PostgreSQL CSV format used by the {@code COPY} operation.
- *
- * <p>
- * This is a comma-delimited format with a LF character as the line separator. Values are double quoted and special
- * characters are escaped with {@code '"'}. The default NULL string is {@code ""}.
- * </p>
- *
- * <p>
- * Settings are:
- * </p>
- * <ul>
- * <li>withDelimiter(',')</li>
- * <li>withQuote('"')</li>
- * <li>withRecordSeparator('\n')</li>
- * <li>withIgnoreEmptyLines(false)</li>
- * <li>withEscape('\\')</li>
- * <li>withNullString("")</li>
- * <li>withQuoteMode(QuoteMode.ALL_NON_NULL)</li>
- * </ul>
- *
- * @see Predefined#MySQL
- * @see <a href="http://dev.mysql.com/doc/refman/5.1/en/load-data.html"> http://dev.mysql.com/doc/refman/5.1/en/load
- * -data.html</a>
- * @since 1.5
- */
- // @formatter:off
- public static final CSVFormat POSTGRESQL_CSV = DEFAULT
- .withDelimiter(COMMA)
- .withEscape(DOUBLE_QUOTE_CHAR)
- .withIgnoreEmptyLines(false)
- .withQuote(DOUBLE_QUOTE_CHAR)
- .withRecordSeparator(LF)
- .withNullString(EMPTY)
- .withQuoteMode(QuoteMode.ALL_NON_NULL);
- // @formatter:off
-
- /**
- * Default PostgreSQL text format used by the {@code COPY} operation.
- *
- * <p>
- * This is a tab-delimited format with a LF character as the line separator. Values are double quoted and special
- * characters are escaped with {@code '"'}. The default NULL string is {@code "\\N"}.
- * </p>
- *
- * <p>
- * Settings are:
- * </p>
- * <ul>
- * <li>withDelimiter('\t')</li>
- * <li>withQuote('"')</li>
- * <li>withRecordSeparator('\n')</li>
- * <li>withIgnoreEmptyLines(false)</li>
- * <li>withEscape('\\')</li>
- * <li>withNullString("\\N")</li>
- * <li>withQuoteMode(QuoteMode.ALL_NON_NULL)</li>
- * </ul>
- *
- * @see Predefined#MySQL
- * @see <a href="http://dev.mysql.com/doc/refman/5.1/en/load-data.html"> http://dev.mysql.com/doc/refman/5.1/en/load
- * -data.html</a>
- * @since 1.5
- */
- // @formatter:off
- public static final CSVFormat POSTGRESQL_TEXT = DEFAULT
- .withDelimiter(TAB)
- .withEscape(DOUBLE_QUOTE_CHAR)
- .withIgnoreEmptyLines(false)
- .withQuote(DOUBLE_QUOTE_CHAR)
- .withRecordSeparator(LF)
- .withNullString("\\N")
- .withQuoteMode(QuoteMode.ALL_NON_NULL);
- // @formatter:off
-
- /**
- * Comma separated format as defined by <a href="http://tools.ietf.org/html/rfc4180">RFC 4180</a>.
- *
- * <p>
- * Settings are:
- * </p>
- * <ul>
- * <li>withDelimiter(',')</li>
- * <li>withQuote('"')</li>
- * <li>withRecordSeparator("\r\n")</li>
- * <li>withIgnoreEmptyLines(false)</li>
- * </ul>
- *
- * @see Predefined#RFC4180
- */
- public static final CSVFormat RFC4180 = DEFAULT.withIgnoreEmptyLines(false);
-
- private static final long serialVersionUID = 1L;
-
- /**
- * Tab-delimited format.
- *
- * <p>
- * Settings are:
- * </p>
- * <ul>
- * <li>withDelimiter('\t')</li>
- * <li>withQuote('"')</li>
- * <li>withRecordSeparator("\r\n")</li>
- * <li>withIgnoreSurroundingSpaces(true)</li>
- * </ul>
+ * Creates a customized CSV format.
*
- * @see Predefined#TDF
+ * @param delimiter the char used for value separation, must not be a line break character
+ * @param quoteCharacter the Character used as value encapsulation marker, may be {@code null} to disable
+ * @param recordSeparator the line separator to use for output
+ * @throws IllegalArgumentException if the _delimiter is a line break character
*/
- // @formatter:off
- public static final CSVFormat TDF = DEFAULT
- .withDelimiter(TAB)
- .withIgnoreSurroundingSpaces();
- // @formatter:on
+ private CSVFormat(final char delimiter,
+ final Character quoteCharacter,
+ final String recordSeparator)
+ {
+ if (delimiter == LF || delimiter == CR)
+ {
+ throw new IllegalArgumentException("The _delimiter cannot be a line break");
+ }
- /**
- * Returns true if the given character is a line break character.
- *
- * @param c
- * the character to check
- *
- * @return true if <code>c</code> is a line break character
- */
- private static boolean isLineBreak(final char c) {
- return c == LF || c == CR;
- }
+ if (quoteCharacter != null && delimiter == quoteCharacter)
+ {
+ throw new IllegalArgumentException(
+ "The quote character and the delimiter cannot be the same ('" + quoteCharacter + "')");
+ }
- /**
- * Returns true if the given character is a line break character.
- *
- * @param c
- * the character to check, may be null
- *
- * @return true if <code>c</code> is a line break character (and not null)
- */
- private static boolean isLineBreak(final Character c) {
- return c != null && isLineBreak(c.charValue());
+ this._delimiter = delimiter;
+ this._quoteCharacter = quoteCharacter;
+ this._recordSeparator = recordSeparator;
}
- /**
- * Creates a new CSV format with the specified delimiter.
- *
- * <p>
- * Use this method if you want to create a CSVFormat from scratch. All fields but the delimiter will be initialized
- * with null/false.
- * </p>
- *
- * @param delimiter
- * the char used for value separation, must not be a line break character
- * @return a new CSV format.
- * @throws IllegalArgumentException
- * if the delimiter is a line break character
- *
- * @see #DEFAULT
- * @see #RFC4180
- * @see #MYSQL
- * @see #EXCEL
- * @see #TDF
- */
- public static CSVFormat newFormat(final char delimiter) {
- return new CSVFormat(delimiter, null, null, null, null, false, false, null, null, null, null, false, false,
- false, false, false, false);
+ public <T extends Collection<?>> void printRecord(final Appendable out, final T record) throws IOException
+ {
+ boolean newRecord = true;
+ for (Object item : record)
+ {
+ print(out, item, newRecord);
+ newRecord = false;
+ }
+ println(out);
}
- /**
- * Gets one of the predefined formats from {@link CSVFormat.Predefined}.
- *
- * @param format
- * name
- * @return one of the predefined formats
- * @since 1.2
- */
- public static CSVFormat valueOf(final String format) {
- return CSVFormat.Predefined.valueOf(format).getFormat();
+ public <C extends Collection<? extends Collection<?>>> void printRecords(final Appendable out, final C records)
+ throws IOException
+ {
+ for (Collection<?> record : records)
+ {
+ printRecord(out, record);
+ }
}
- private final boolean allowMissingColumnNames;
-
- private final Character commentMarker; // null if commenting is disabled
- private final char delimiter;
-
- private final Character escapeCharacter; // null if escaping is disabled
-
- private final String[] header; // array of header column names
-
- private final String[] headerComments; // array of header comment lines
-
- private final boolean ignoreEmptyLines;
-
- private final boolean ignoreHeaderCase; // should ignore header names case
-
- private final boolean ignoreSurroundingSpaces; // Should leading/trailing spaces be ignored around values?
-
- private final String nullString; // the string to be used for null values
-
- private final Character quoteCharacter; // null if quoting is disabled
-
- private final QuoteMode quoteMode;
-
- private final String recordSeparator; // for outputs
-
- private final boolean skipHeaderRecord;
-
- private final boolean trailingDelimiter;
-
- private final boolean trim;
-
- private final boolean autoFlush;
-
- /**
- * Creates a customized CSV format.
- *
- * @param delimiter
- * the char used for value separation, must not be a line break character
- * @param quoteChar
- * the Character used as value encapsulation marker, may be {@code null} to disable
- * @param quoteMode
- * the quote mode
- * @param commentStart
- * the Character used for comment identification, may be {@code null} to disable
- * @param escape
- * the Character used to escape special characters in values, may be {@code null} to disable
- * @param ignoreSurroundingSpaces
- * {@code true} when whitespaces enclosing values should be ignored
- * @param ignoreEmptyLines
- * {@code true} when the parser should skip empty lines
- * @param recordSeparator
- * the line separator to use for output
- * @param nullString
- * the line separator to use for output
- * @param headerComments
- * the comments to be printed by the Printer before the actual CSV data
- * @param header
- * the header
- * @param skipHeaderRecord
- * TODO
- * @param allowMissingColumnNames
- * TODO
- * @param ignoreHeaderCase
- * TODO
- * @param trim
- * TODO
- * @param trailingDelimiter
- * TODO
- * @param autoFlush
- * @throws IllegalArgumentException
- * if the delimiter is a line break character
- */
- private CSVFormat(final char delimiter, final Character quoteChar, final QuoteMode quoteMode,
- final Character commentStart, final Character escape, final boolean ignoreSurroundingSpaces,
- final boolean ignoreEmptyLines, final String recordSeparator, final String nullString,
- final Object[] headerComments, final String[] header, final boolean skipHeaderRecord,
- final boolean allowMissingColumnNames, final boolean ignoreHeaderCase, final boolean trim,
- final boolean trailingDelimiter, final boolean autoFlush) {
- this.delimiter = delimiter;
- this.quoteCharacter = quoteChar;
- this.quoteMode = quoteMode;
- this.commentMarker = commentStart;
- this.escapeCharacter = escape;
- this.ignoreSurroundingSpaces = ignoreSurroundingSpaces;
- this.allowMissingColumnNames = allowMissingColumnNames;
- this.ignoreEmptyLines = ignoreEmptyLines;
- this.recordSeparator = recordSeparator;
- this.nullString = nullString;
- this.headerComments = toStringArray(headerComments);
- this.header = header == null ? null : header.clone();
- this.skipHeaderRecord = skipHeaderRecord;
- this.ignoreHeaderCase = ignoreHeaderCase;
- this.trailingDelimiter = trailingDelimiter;
- this.trim = trim;
- this.autoFlush = autoFlush;
- validate();
+ public void println(final Appendable out) throws IOException
+ {
+ if (_recordSeparator != null)
+ {
+ out.append(_recordSeparator);
+ }
}
- @Override
- public boolean equals(final Object obj) {
- if (this == obj) {
- return true;
+ public void print(final Appendable out, final Object value, final boolean newRecord) throws IOException
+ {
+ CharSequence charSequence;
+ if (value == null)
+ {
+ charSequence = EMPTY;
}
- if (obj == null) {
- return false;
+ else
+ {
+ charSequence = value instanceof CharSequence ? (CharSequence) value : value.toString();
}
- if (getClass() != obj.getClass()) {
- return false;
+ this.print(out, value, charSequence, 0, charSequence.length(), newRecord);
+ }
+
+
+ public void printComments(final Appendable out,
+ final String... comments) throws IOException
+ {
+ for (String comment: comments)
+ {
+ out.append(COMMENT).append(SP).append(comment);
+ println(out);
}
+ }
- final CSVFormat other = (CSVFormat) obj;
- if (delimiter != other.delimiter) {
- return false;
+ private void print(final Appendable out,
+ final Object object,
+ final CharSequence value,
+ final int offset,
+ final int len,
+ final boolean newRecord) throws IOException
+ {
+ if (!newRecord)
+ {
+ out.append(_delimiter);
}
- if (quoteMode != other.quoteMode) {
- return false;
+ if (object == null)
+ {
+ out.append(value);
}
- if (quoteCharacter == null) {
- if (other.quoteCharacter != null) {
- return false;
- }
- } else if (!quoteCharacter.equals(other.quoteCharacter)) {
- return false;
+ else if (_quoteCharacter != null)
+ {
+ printAndQuote(value, offset, len, out, newRecord);
}
- if (commentMarker == null) {
- if (other.commentMarker != null) {
- return false;
- }
- } else if (!commentMarker.equals(other.commentMarker)) {
- return false;
+ else
+ {
+ out.append(value, offset, offset + len);
}
- if (escapeCharacter == null) {
- if (other.escapeCharacter != null) {
- return false;
+ }
+
+ private void printAndQuote(final CharSequence value, final int offset, final int len,
+ final Appendable out, final boolean newRecord) throws IOException
+ {
+ boolean quote = false;
+ int start = offset;
+ int pos = offset;
+ final int end = offset + len;
+
+ final char quoteChar = _quoteCharacter;
+
+ if (len <= 0)
+ {
+ // always quote an empty token that is the first
+ // on the line, as it may be the only thing on the
+ // line. If it were not quoted in that case,
+ // an empty line has no tokens.
+ if (newRecord)
+ {
+ quote = true;
}
- } else if (!escapeCharacter.equals(other.escapeCharacter)) {
- return false;
}
- if (nullString == null) {
- if (other.nullString != null) {
- return false;
+ else
+ {
+ char c = value.charAt(pos);
+
+ if (c <= COMMENT)
+ {
+ // Some other chars at the start of a value caused the parser to fail, so for now
+ // encapsulate if we start in anything less than '#'. We are being conservative
+ // by including the default comment char too.
+ quote = true;
}
- } else if (!nullString.equals(other.nullString)) {
- return false;
- }
- if (!Arrays.equals(header, other.header)) {
- return false;
- }
- if (ignoreSurroundingSpaces != other.ignoreSurroundingSpaces) {
- return false;
- }
- if (ignoreEmptyLines != other.ignoreEmptyLines) {
- return false;
- }
- if (skipHeaderRecord != other.skipHeaderRecord) {
- return false;
- }
- if (recordSeparator == null) {
- if (other.recordSeparator != null) {
- return false;
+ else
+ {
+ while (pos < end)
+ {
+ c = value.charAt(pos);
+ if (c == LF || c == CR || c == quoteChar || c == _delimiter)
+ {
+ quote = true;
+ break;
+ }
+ pos++;
+ }
+
+ if (!quote)
+ {
+ pos = end - 1;
+ c = value.charAt(pos);
+ // Some other chars at the end caused the parser to fail, so for now
+ // encapsulate if we end in anything less than ' '
+ if (c <= SP)
+ {
+ quote = true;
+ }
+ }
}
- } else if (!recordSeparator.equals(other.recordSeparator)) {
- return false;
}
- return true;
- }
- /**
- * Formats the specified values.
- *
- * @param values
- * the values to format
- * @return the formatted values
- */
- public String format(final Object... values) {
- final StringWriter out = new StringWriter();
- try (final CSVPrinter csvPrinter = new CSVPrinter(out, this)) {
- csvPrinter.printRecord(values);
- return out.toString().trim();
- } catch (final IOException e) {
- // should not happen because a StringWriter does not do IO.
- throw new IllegalStateException(e);
+ if (!quote)
+ {
+ // no encapsulation needed - write out the original value
+ out.append(value, start, end);
+ return;
}
- }
-
- /**
- * Specifies whether missing column names are allowed when parsing the header line.
- *
- * @return {@code true} if missing column names are allowed when parsing the header line, {@code false} to throw an
- * {@link IllegalArgumentException}.
- */
- public boolean getAllowMissingColumnNames() {
- return allowMissingColumnNames;
- }
-
- /**
- * Returns whether to flush on close.
- *
- * @return whether to flush on close.
- * @since 1.6
- */
- public boolean getAutoFlush() {
- return autoFlush;
- }
-
- /**
- * Returns the character marking the start of a line comment.
- *
- * @return the comment start marker, may be {@code null}
- */
- public Character getCommentMarker() {
- return commentMarker;
- }
-
- /**
- * Returns the character delimiting the values (typically ';', ',' or '\t').
- *
- * @return the delimiter character
- */
- public char getDelimiter() {
- return delimiter;
- }
-
- /**
- * Returns the escape character.
- *
- * @return the escape character, may be {@code null}
- */
- public Character getEscapeCharacter() {
- return escapeCharacter;
- }
-
- /**
- * Returns a copy of the header array.
- *
- * @return a copy of the header array; {@code null} if disabled, the empty array if to be read from the file
- */
- public String[] getHeader() {
- return header != null ? header.clone() : null;
- }
-
- /**
- * Returns a copy of the header comment array.
- *
- * @return a copy of the header comment array; {@code null} if disabled.
- */
- public String[] getHeaderComments() {
- return headerComments != null ? headerComments.clone() : null;
- }
-
- /**
- * Specifies whether empty lines between records are ignored when parsing input.
- *
- * @return {@code true} if empty lines between records are ignored, {@code false} if they are turned into empty
- * records.
- */
- public boolean getIgnoreEmptyLines() {
- return ignoreEmptyLines;
- }
-
- /**
- * Specifies whether header names will be accessed ignoring case.
- *
- * @return {@code true} if header names cases are ignored, {@code false} if they are case sensitive.
- * @since 1.3
- */
- public boolean getIgnoreHeaderCase() {
- return ignoreHeaderCase;
- }
-
- /**
- * Specifies whether spaces around values are ignored when parsing input.
- *
- * @return {@code true} if spaces around values are ignored, {@code false} if they are treated as part of the value.
- */
- public boolean getIgnoreSurroundingSpaces() {
- return ignoreSurroundingSpaces;
- }
-
- /**
- * Gets the String to convert to and from {@code null}.
- * <ul>
- * <li><strong>Reading:</strong> Converts strings equal to the given {@code nullString} to {@code null} when reading
- * records.</li>
- * <li><strong>Writing:</strong> Writes {@code null} as the given {@code nullString} when writing records.</li>
- * </ul>
- *
- * @return the String to convert to and from {@code null}. No substitution occurs if {@code null}
- */
- public String getNullString() {
- return nullString;
- }
- /**
- * Returns the character used to encapsulate values containing special characters.
- *
- * @return the quoteChar character, may be {@code null}
- */
- public Character getQuoteCharacter() {
- return quoteCharacter;
- }
+ // we hit something that needed encapsulation
+ out.append(quoteChar);
- /**
- * Returns the quote policy output fields.
- *
- * @return the quote policy
- */
- public QuoteMode getQuoteMode() {
- return quoteMode;
- }
+ // Pick up where we left off: pos should be positioned on the first character that caused
+ // the need for encapsulation.
+ while (pos < end)
+ {
+ final char c = value.charAt(pos);
+ if (c == quoteChar)
+ {
+ // write out the chunk up until this point
- /**
- * Returns the record separator delimiting output records.
- *
- * @return the record separator
- */
- public String getRecordSeparator() {
- return recordSeparator;
- }
+ // add 1 to the length to write out the encapsulator also
+ out.append(value, start, pos + 1);
+ // put the next starting position on the encapsulator so we will
+ // write it out again with the next string (effectively doubling it)
+ start = pos;
+ }
+ pos++;
+ }
- /**
- * Returns whether to skip the header record.
- *
- * @return whether to skip the header record.
- */
- public boolean getSkipHeaderRecord() {
- return skipHeaderRecord;
+ // write the last segment
+ out.append(value, start, pos);
+ out.append(quoteChar);
}
- /**
- * Returns whether to add a trailing delimiter.
- *
- * @return whether to add a trailing delimiter.
- * @since 1.3
- */
- public boolean getTrailingDelimiter() {
- return trailingDelimiter;
- }
-
- /**
- * Returns whether to trim leading and trailing blanks.
- *
- * @return whether to trim leading and trailing blanks.
- */
- public boolean getTrim() {
- return trim;
- }
-
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
-
- result = prime * result + delimiter;
- result = prime * result + ((quoteMode == null) ? 0 : quoteMode.hashCode());
- result = prime * result + ((quoteCharacter == null) ? 0 : quoteCharacter.hashCode());
- result = prime * result + ((commentMarker == null) ? 0 : commentMarker.hashCode());
- result = prime * result + ((escapeCharacter == null) ? 0 : escapeCharacter.hashCode());
- result = prime * result + ((nullString == null) ? 0 : nullString.hashCode());
- result = prime * result + (ignoreSurroundingSpaces ? 1231 : 1237);
- result = prime * result + (ignoreHeaderCase ? 1231 : 1237);
- result = prime * result + (ignoreEmptyLines ? 1231 : 1237);
- result = prime * result + (skipHeaderRecord ? 1231 : 1237);
- result = prime * result + ((recordSeparator == null) ? 0 : recordSeparator.hashCode());
- result = prime * result + Arrays.hashCode(header);
- return result;
- }
-
- /**
- * Specifies whether comments are supported by this format.
- *
- * Note that the comment introducer character is only recognized at the start of a line.
- *
- * @return {@code true} is comments are supported, {@code false} otherwise
- */
- public boolean isCommentMarkerSet() {
- return commentMarker != null;
- }
-
- /**
- * Returns whether escape are being processed.
- *
- * @return {@code true} if escapes are processed
- */
- public boolean isEscapeCharacterSet() {
- return escapeCharacter != null;
- }
-
- /**
- * Returns whether a nullString has been defined.
- *
- * @return {@code true} if a nullString is defined
- */
- public boolean isNullStringSet() {
- return nullString != null;
- }
-
- /**
- * Returns whether a quoteChar has been defined.
- *
- * @return {@code true} if a quoteChar is defined
- */
- public boolean isQuoteCharacterSet() {
- return quoteCharacter != null;
- }
-
- /**
- * Parses the specified content.
- *
- * <p>
- * See also the various static parse methods on {@link CSVParser}.
- * </p>
- *
- * @param in
- * the input stream
- * @return a parser over a stream of {@link CSVRecord}s.
- * @throws IOException
- * If an I/O error occurs
- */
- public CSVParser parse(final Reader in) throws IOException {
- return new CSVParser(in, this);
- }
-
- /**
- * Prints to the specified output.
- *
- * <p>
- * See also {@link CSVPrinter}.
- * </p>
- *
- * @param out
- * the output.
- * @return a printer to an output.
- * @throws IOException
- * thrown if the optional header cannot be printed.
- */
- public CSVPrinter print(final Appendable out) throws IOException {
- return new CSVPrinter(out, this);
- }
-
- /**
- * Prints to the specified output.
- *
- * <p>
- * See also {@link CSVPrinter}.
- * </p>
- *
- * @param out
- * the output.
- * @param charset
- * A charset.
- * @return a printer to an output.
- * @throws IOException
- * thrown if the optional header cannot be printed.
- * @since 1.5
- */
- @SuppressWarnings("resource")
- public CSVPrinter print(final File out, final Charset charset) throws IOException {
- // The writer will be closed when close() is called.
- return new CSVPrinter(new OutputStreamWriter(new FileOutputStream(out), charset), this);
- }
-
- /**
- * Prints the {@code value} as the next value on the line to {@code out}. The value will be escaped or encapsulated
- * as needed. Useful when one wants to avoid creating CSVPrinters.
- *
- * @param value
- * value to output.
- * @param out
- * where to print the value.
- * @param newRecord
- * if this a new record.
- * @throws IOException
- * If an I/O error occurs.
- * @since 1.4
- */
- public void print(final Object value, final Appendable out, final boolean newRecord) throws IOException {
- // null values are considered empty
- // Only call CharSequence.toString() if you have to, helps GC-free use cases.
- CharSequence charSequence;
- if (value == null) {
- // https://issues.apache.org/jira/browse/CSV-203
- if (null == nullString) {
- charSequence = EMPTY;
- } else {
- if (QuoteMode.ALL == quoteMode) {
- charSequence = quoteCharacter + nullString + quoteCharacter;
- } else {
- charSequence = nullString;
- }
- }
- } else {
- charSequence = value instanceof CharSequence ? (CharSequence) value : value.toString();
- }
- charSequence = getTrim() ? trim(charSequence) : charSequence;
- this.print(value, charSequence, 0, charSequence.length(), out, newRecord);
- }
-
- private void print(final Object object, final CharSequence value, final int offset, final int len,
- final Appendable out, final boolean newRecord) throws IOException {
- if (!newRecord) {
- out.append(getDelimiter());
- }
- if (object == null) {
- out.append(value);
- } else if (isQuoteCharacterSet()) {
- // the original object is needed so can check for Number
- printAndQuote(object, value, offset, len, out, newRecord);
- } else if (isEscapeCharacterSet()) {
- printAndEscape(value, offset, len, out);
- } else {
- out.append(value, offset, offset + len);
- }
- }
-
- /**
- * Prints to the specified output.
- *
- * <p>
- * See also {@link CSVPrinter}.
- * </p>
- *
- * @param out
- * the output.
- * @param charset
- * A charset.
- * @return a printer to an output.
- * @throws IOException
- * thrown if the optional header cannot be printed.
- * @since 1.5
- */
- public CSVPrinter print(final Path out, final Charset charset) throws IOException {
- return print(Files.newBufferedWriter(out, charset));
- }
-
- /*
- * Note: must only be called if escaping is enabled, otherwise will generate NPE
- */
- private void printAndEscape(final CharSequence value, final int offset, final int len, final Appendable out)
- throws IOException {
- int start = offset;
- int pos = offset;
- final int end = offset + len;
-
- final char delim = getDelimiter();
- final char escape = getEscapeCharacter().charValue();
-
- while (pos < end) {
- char c = value.charAt(pos);
- if (c == CR || c == LF || c == delim || c == escape) {
- // write out segment up until this char
- if (pos > start) {
- out.append(value, start, pos);
- }
- if (c == LF) {
- c = 'n';
- } else if (c == CR) {
- c = 'r';
- }
-
- out.append(escape);
- out.append(c);
-
- start = pos + 1; // start on the current char after this one
- }
-
- pos++;
- }
-
- // write last segment
- if (pos > start) {
- out.append(value, start, pos);
- }
- }
-
- /*
- * Note: must only be called if quoting is enabled, otherwise will generate NPE
- */
- // the original object is needed so can check for Number
- private void printAndQuote(final Object object, final CharSequence value, final int offset, final int len,
- final Appendable out, final boolean newRecord) throws IOException {
- boolean quote = false;
- int start = offset;
- int pos = offset;
- final int end = offset + len;
-
- final char delimChar = getDelimiter();
- final char quoteChar = getQuoteCharacter().charValue();
-
- QuoteMode quoteModePolicy = getQuoteMode();
- if (quoteModePolicy == null) {
- quoteModePolicy = QuoteMode.MINIMAL;
- }
- switch (quoteModePolicy) {
- case ALL:
- case ALL_NON_NULL:
- quote = true;
- break;
- case NON_NUMERIC:
- quote = !(object instanceof Number);
- break;
- case NONE:
- // Use the existing escaping code
- printAndEscape(value, offset, len, out);
- return;
- case MINIMAL:
- if (len <= 0) {
- // always quote an empty token that is the first
- // on the line, as it may be the only thing on the
- // line. If it were not quoted in that case,
- // an empty line has no tokens.
- if (newRecord) {
- quote = true;
- }
- } else {
- char c = value.charAt(pos);
-
- if (c <= COMMENT) {
- // Some other chars at the start of a value caused the parser to fail, so for now
- // encapsulate if we start in anything less than '#'. We are being conservative
- // by including the default comment char too.
- quote = true;
- } else {
- while (pos < end) {
- c = value.charAt(pos);
- if (c == LF || c == CR || c == quoteChar || c == delimChar) {
- quote = true;
- break;
- }
- pos++;
- }
-
- if (!quote) {
- pos = end - 1;
- c = value.charAt(pos);
- // Some other chars at the end caused the parser to fail, so for now
- // encapsulate if we end in anything less than ' '
- if (c <= SP) {
- quote = true;
- }
- }
- }
- }
-
- if (!quote) {
- // no encapsulation needed - write out the original value
- out.append(value, start, end);
- return;
- }
- break;
- default:
- throw new IllegalStateException("Unexpected Quote value: " + quoteModePolicy);
- }
-
- if (!quote) {
- // no encapsulation needed - write out the original value
- out.append(value, start, end);
- return;
- }
-
- // we hit something that needed encapsulation
- out.append(quoteChar);
-
- // Pick up where we left off: pos should be positioned on the first character that caused
- // the need for encapsulation.
- while (pos < end) {
- final char c = value.charAt(pos);
- if (c == quoteChar) {
- // write out the chunk up until this point
-
- // add 1 to the length to write out the encapsulator also
- out.append(value, start, pos + 1);
- // put the next starting position on the encapsulator so we will
- // write it out again with the next string (effectively doubling it)
- start = pos;
- }
- pos++;
- }
-
- // write the last segment
- out.append(value, start, pos);
- out.append(quoteChar);
- }
-
- /**
- * Prints to the {@link System#out}.
- *
- * <p>
- * See also {@link CSVPrinter}.
- * </p>
- *
- * @return a printer to {@link System#out}.
- * @throws IOException
- * thrown if the optional header cannot be printed.
- * @since 1.5
- */
- public CSVPrinter printer() throws IOException {
- return new CSVPrinter(System.out, this);
- }
-
- /**
- * Outputs the trailing delimiter (if set) followed by the record separator (if set).
- *
- * @param out
- * where to write
- * @throws IOException
- * If an I/O error occurs
- * @since 1.4
- */
- public void println(final Appendable out) throws IOException {
- if (getTrailingDelimiter()) {
- out.append(getDelimiter());
- }
- if (recordSeparator != null) {
- out.append(recordSeparator);
- }
- }
-
- /**
- * Prints the given {@code values} to {@code out} as a single record of delimiter separated values followed by the
- * record separator.
- *
- * <p>
- * The values will be quoted if needed. Quotes and new-line characters will be escaped. This method adds the record
- * separator to the output after printing the record, so there is no need to call {@link #println(Appendable)}.
- * </p>
- *
- * @param out
- * where to write.
- * @param values
- * values to output.
- * @throws IOException
- * If an I/O error occurs.
- * @since 1.4
- */
- public void printRecord(final Appendable out, final Object... values) throws IOException {
- for (int i = 0; i < values.length; i++) {
- print(values[i], out, i == 0);
- }
- println(out);
- }
-
- @Override
- public String toString() {
- final StringBuilder sb = new StringBuilder();
- sb.append("Delimiter=<").append(delimiter).append('>');
- if (isEscapeCharacterSet()) {
- sb.append(' ');
- sb.append("Escape=<").append(escapeCharacter).append('>');
- }
- if (isQuoteCharacterSet()) {
- sb.append(' ');
- sb.append("QuoteChar=<").append(quoteCharacter).append('>');
- }
- if (isCommentMarkerSet()) {
- sb.append(' ');
- sb.append("CommentStart=<").append(commentMarker).append('>');
- }
- if (isNullStringSet()) {
- sb.append(' ');
- sb.append("NullString=<").append(nullString).append('>');
- }
- if (recordSeparator != null) {
- sb.append(' ');
- sb.append("RecordSeparator=<").append(recordSeparator).append('>');
- }
- if (getIgnoreEmptyLines()) {
- sb.append(" EmptyLines:ignored");
- }
- if (getIgnoreSurroundingSpaces()) {
- sb.append(" SurroundingSpaces:ignored");
- }
- if (getIgnoreHeaderCase()) {
- sb.append(" IgnoreHeaderCase:ignored");
- }
- sb.append(" SkipHeaderRecord:").append(skipHeaderRecord);
- if (headerComments != null) {
- sb.append(' ');
- sb.append("HeaderComments:").append(Arrays.toString(headerComments));
- }
- if (header != null) {
- sb.append(' ');
- sb.append("Header:").append(Arrays.toString(header));
- }
- return sb.toString();
- }
-
- private String[] toStringArray(final Object[] values) {
- if (values == null) {
- return null;
- }
- final String[] strings = new String[values.length];
- for (int i = 0; i < values.length; i++) {
- final Object value = values[i];
- strings[i] = value == null ? null : value.toString();
- }
- return strings;
- }
-
- private CharSequence trim(final CharSequence charSequence) {
- if (charSequence instanceof String) {
- return ((String) charSequence).trim();
- }
- final int count = charSequence.length();
- int len = count;
- int pos = 0;
-
- while (pos < len && charSequence.charAt(pos) <= SP) {
- pos++;
- }
- while (pos < len && charSequence.charAt(len - 1) <= SP) {
- len--;
- }
- return pos > 0 || len < count ? charSequence.subSequence(pos, len) : charSequence;
- }
-
- /**
- * Verifies the consistency of the parameters and throws an IllegalArgumentException if necessary.
- *
- * @throws IllegalArgumentException
- */
- private void validate() throws IllegalArgumentException {
- if (isLineBreak(delimiter)) {
- throw new IllegalArgumentException("The delimiter cannot be a line break");
- }
-
- if (quoteCharacter != null && delimiter == quoteCharacter.charValue()) {
- throw new IllegalArgumentException(
- "The quoteChar character and the delimiter cannot be the same ('" + quoteCharacter + "')");
- }
-
- if (escapeCharacter != null && delimiter == escapeCharacter.charValue()) {
- throw new IllegalArgumentException(
- "The escape character and the delimiter cannot be the same ('" + escapeCharacter + "')");
- }
-
- if (commentMarker != null && delimiter == commentMarker.charValue()) {
- throw new IllegalArgumentException(
- "The comment start character and the delimiter cannot be the same ('" + commentMarker + "')");
- }
-
- if (quoteCharacter != null && quoteCharacter.equals(commentMarker)) {
- throw new IllegalArgumentException(
- "The comment start character and the quoteChar cannot be the same ('" + commentMarker + "')");
- }
-
- if (escapeCharacter != null && escapeCharacter.equals(commentMarker)) {
- throw new IllegalArgumentException(
- "The comment start and the escape character cannot be the same ('" + commentMarker + "')");
- }
-
- if (escapeCharacter == null && quoteMode == QuoteMode.NONE) {
- throw new IllegalArgumentException("No quotes mode set but no escape character is set");
- }
-
- // validate header
- if (header != null) {
- final Set<String> dupCheck = new HashSet<>();
- for (final String hdr : header) {
- if (!dupCheck.add(hdr)) {
- throw new IllegalArgumentException(
- "The header contains a duplicate entry: '" + hdr + "' in " + Arrays.toString(header));
- }
- }
- }
- }
-
- /**
- * Returns a new {@code CSVFormat} with the missing column names behavior of the format set to {@code true}
- *
- * @return A new CSVFormat that is equal to this but with the specified missing column names behavior.
- * @see #withAllowMissingColumnNames(boolean)
- * @since 1.1
- */
- public CSVFormat withAllowMissingColumnNames() {
- return this.withAllowMissingColumnNames(true);
- }
-
- /**
- * Returns a new {@code CSVFormat} with the missing column names behavior of the format set to the given value.
- *
- * @param allowMissingColumnNames
- * the missing column names behavior, {@code true} to allow missing column names in the header line,
- * {@code false} to cause an {@link IllegalArgumentException} to be thrown.
- * @return A new CSVFormat that is equal to this but with the specified missing column names behavior.
- */
- public CSVFormat withAllowMissingColumnNames(final boolean allowMissingColumnNames) {
- return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
- skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
- }
-
- /**
- * Returns a new {@code CSVFormat} with whether to flush on close.
- *
- * @param autoFlush
- * whether to flush on close.
- *
- * @return A new CSVFormat that is equal to this but with the specified autoFlush setting.
- * @since 1.6
- */
- public CSVFormat withAutoFlush(final boolean autoFlush) {
- return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
- skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
- }
-
- /**
- * Returns a new {@code CSVFormat} with the comment start marker of the format set to the specified character.
- *
- * Note that the comment start character is only recognized at the start of a line.
- *
- * @param commentMarker
- * the comment start marker
- * @return A new CSVFormat that is equal to this one but with the specified character as the comment start marker
- * @throws IllegalArgumentException
- * thrown if the specified character is a line break
- */
- public CSVFormat withCommentMarker(final char commentMarker) {
- return withCommentMarker(Character.valueOf(commentMarker));
- }
-
- /**
- * Returns a new {@code CSVFormat} with the comment start marker of the format set to the specified character.
- *
- * Note that the comment start character is only recognized at the start of a line.
- *
- * @param commentMarker
- * the comment start marker, use {@code null} to disable
- * @return A new CSVFormat that is equal to this one but with the specified character as the comment start marker
- * @throws IllegalArgumentException
- * thrown if the specified character is a line break
- */
- public CSVFormat withCommentMarker(final Character commentMarker) {
- if (isLineBreak(commentMarker)) {
- throw new IllegalArgumentException("The comment start marker character cannot be a line break");
- }
- return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
- skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
- }
-
- /**
- * Returns a new {@code CSVFormat} with the delimiter of the format set to the specified character.
- *
- * @param delimiter
- * the delimiter character
- * @return A new CSVFormat that is equal to this with the specified character as delimiter
- * @throws IllegalArgumentException
- * thrown if the specified character is a line break
- */
- public CSVFormat withDelimiter(final char delimiter) {
- if (isLineBreak(delimiter)) {
- throw new IllegalArgumentException("The delimiter cannot be a line break");
- }
- return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
- skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
- }
-
- /**
- * Returns a new {@code CSVFormat} with the escape character of the format set to the specified character.
- *
- * @param escape
- * the escape character
- * @return A new CSVFormat that is equal to his but with the specified character as the escape character
- * @throws IllegalArgumentException
- * thrown if the specified character is a line break
- */
- public CSVFormat withEscape(final char escape) {
- return withEscape(Character.valueOf(escape));
- }
-
- /**
- * Returns a new {@code CSVFormat} with the escape character of the format set to the specified character.
- *
- * @param escape
- * the escape character, use {@code null} to disable
- * @return A new CSVFormat that is equal to this but with the specified character as the escape character
- * @throws IllegalArgumentException
- * thrown if the specified character is a line break
- */
- public CSVFormat withEscape(final Character escape) {
- if (isLineBreak(escape)) {
- throw new IllegalArgumentException("The escape character cannot be a line break");
- }
- return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escape, ignoreSurroundingSpaces,
- ignoreEmptyLines, recordSeparator, nullString, headerComments, header, skipHeaderRecord,
- allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
- }
-
- /**
- * Returns a new {@code CSVFormat} using the first record as header.
- *
- * <p>
- * Calling this method is equivalent to calling:
- * </p>
- *
- * <pre>
- * CSVFormat format = aFormat.withHeader().withSkipHeaderRecord();
- * </pre>
- *
- * @return A new CSVFormat that is equal to this but using the first record as header.
- * @see #withSkipHeaderRecord(boolean)
- * @see #withHeader(String...)
- * @since 1.3
- */
- public CSVFormat withFirstRecordAsHeader() {
- return withHeader().withSkipHeaderRecord();
- }
-
- /**
- * Returns a new {@code CSVFormat} with the header of the format defined by the enum class.
- *
- * <p>
- * Example:
- * </p>
- * <pre>
- * public enum Header {
- * Name, Email, Phone
- * }
- *
- * CSVFormat format = aformat.withHeader(Header.class);
- * </pre>
- * <p>
- * The header is also used by the {@link CSVPrinter}.
- * </p>
- *
- * @param headerEnum
- * the enum defining the header, {@code null} if disabled, empty if parsed automatically, user specified
- * otherwise.
- *
- * @return A new CSVFormat that is equal to this but with the specified header
- * @see #withHeader(String...)
- * @see #withSkipHeaderRecord(boolean)
- * @since 1.3
- */
- public CSVFormat withHeader(final Class<? extends Enum<?>> headerEnum) {
- String[] header = null;
- if (headerEnum != null) {
- final Enum<?>[] enumValues = headerEnum.getEnumConstants();
- header = new String[enumValues.length];
- for (int i = 0; i < enumValues.length; i++) {
- header[i] = enumValues[i].name();
- }
- }
- return withHeader(header);
- }
-
- /**
- * Returns a new {@code CSVFormat} with the header of the format set from the result set metadata. The header can
- * either be parsed automatically from the input file with:
- *
- * <pre>
- * CSVFormat format = aformat.withHeader();
- * </pre>
- *
- * or specified manually with:
- *
- * <pre>
- * CSVFormat format = aformat.withHeader(resultSet);
- * </pre>
- * <p>
- * The header is also used by the {@link CSVPrinter}.
- * </p>
- *
- * @param resultSet
- * the resultSet for the header, {@code null} if disabled, empty if parsed automatically, user specified
- * otherwise.
- *
- * @return A new CSVFormat that is equal to this but with the specified header
- * @throws SQLException
- * SQLException if a database access error occurs or this method is called on a closed result set.
- * @since 1.1
- */
- public CSVFormat withHeader(final ResultSet resultSet) throws SQLException {
- return withHeader(resultSet != null ? resultSet.getMetaData() : null);
- }
-
- /**
- * Returns a new {@code CSVFormat} with the header of the format set from the result set metadata. The header can
- * either be parsed automatically from the input file with:
- *
- * <pre>
- * CSVFormat format = aformat.withHeader();
- * </pre>
- *
- * or specified manually with:
- *
- * <pre>
- * CSVFormat format = aformat.withHeader(metaData);
- * </pre>
- * <p>
- * The header is also used by the {@link CSVPrinter}.
- * </p>
- *
- * @param metaData
- * the metaData for the header, {@code null} if disabled, empty if parsed automatically, user specified
- * otherwise.
- *
- * @return A new CSVFormat that is equal to this but with the specified header
- * @throws SQLException
- * SQLException if a database access error occurs or this method is called on a closed result set.
- * @since 1.1
- */
- public CSVFormat withHeader(final ResultSetMetaData metaData) throws SQLException {
- String[] labels = null;
- if (metaData != null) {
- final int columnCount = metaData.getColumnCount();
- labels = new String[columnCount];
- for (int i = 0; i < columnCount; i++) {
- labels[i] = metaData.getColumnLabel(i + 1);
- }
- }
- return withHeader(labels);
- }
-
- /**
- * Returns a new {@code CSVFormat} with the header of the format set to the given values. The header can either be
- * parsed automatically from the input file with:
- *
- * <pre>
- * CSVFormat format = aformat.withHeader();
- * </pre>
- *
- * or specified manually with:
- *
- * <pre>
- * CSVFormat format = aformat.withHeader("name", "email", "phone");
- * </pre>
- * <p>
- * The header is also used by the {@link CSVPrinter}.
- * </p>
- *
- * @param header
- * the header, {@code null} if disabled, empty if parsed automatically, user specified otherwise.
- *
- * @return A new CSVFormat that is equal to this but with the specified header
- * @see #withSkipHeaderRecord(boolean)
- */
- public CSVFormat withHeader(final String... header) {
- return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
- skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
- }
-
- /**
- * Returns a new {@code CSVFormat} with the header comments of the format set to the given values. The comments will
- * be printed first, before the headers. This setting is ignored by the parser.
- *
- * <pre>
- * CSVFormat format = aformat.withHeaderComments("Generated by Apache Commons CSV 1.1.", new Date());
- * </pre>
- *
- * @param headerComments
- * the headerComments which will be printed by the Printer before the actual CSV data.
- *
- * @return A new CSVFormat that is equal to this but with the specified header
- * @see #withSkipHeaderRecord(boolean)
- * @since 1.1
- */
- public CSVFormat withHeaderComments(final Object... headerComments) {
- return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
- skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
- }
-
- /**
- * Returns a new {@code CSVFormat} with the empty line skipping behavior of the format set to {@code true}.
- *
- * @return A new CSVFormat that is equal to this but with the specified empty line skipping behavior.
- * @since {@link #withIgnoreEmptyLines(boolean)}
- * @since 1.1
- */
- public CSVFormat withIgnoreEmptyLines() {
- return this.withIgnoreEmptyLines(true);
- }
-
- /**
- * Returns a new {@code CSVFormat} with the empty line skipping behavior of the format set to the given value.
- *
- * @param ignoreEmptyLines
- * the empty line skipping behavior, {@code true} to ignore the empty lines between the records,
- * {@code false} to translate empty lines to empty records.
- * @return A new CSVFormat that is equal to this but with the specified empty line skipping behavior.
- */
- public CSVFormat withIgnoreEmptyLines(final boolean ignoreEmptyLines) {
- return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
- skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
- }
-
- /**
- * Returns a new {@code CSVFormat} with the header ignore case behavior set to {@code true}.
- *
- * @return A new CSVFormat that will ignore case header name.
- * @see #withIgnoreHeaderCase(boolean)
- * @since 1.3
- */
- public CSVFormat withIgnoreHeaderCase() {
- return this.withIgnoreHeaderCase(true);
- }
-
- /**
- * Returns a new {@code CSVFormat} with whether header names should be accessed ignoring case.
- *
- * @param ignoreHeaderCase
- * the case mapping behavior, {@code true} to access name/values, {@code false} to leave the mapping as
- * is.
- * @return A new CSVFormat that will ignore case header name if specified as {@code true}
- * @since 1.3
- */
- public CSVFormat withIgnoreHeaderCase(final boolean ignoreHeaderCase) {
- return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
- skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
- }
-
- /**
- * Returns a new {@code CSVFormat} with the trimming behavior of the format set to {@code true}.
- *
- * @return A new CSVFormat that is equal to this but with the specified trimming behavior.
- * @see #withIgnoreSurroundingSpaces(boolean)
- * @since 1.1
- */
- public CSVFormat withIgnoreSurroundingSpaces() {
- return this.withIgnoreSurroundingSpaces(true);
- }
-
- /**
- * Returns a new {@code CSVFormat} with the trimming behavior of the format set to the given value.
- *
- * @param ignoreSurroundingSpaces
- * the trimming behavior, {@code true} to remove the surrounding spaces, {@code false} to leave the
- * spaces as is.
- * @return A new CSVFormat that is equal to this but with the specified trimming behavior.
- */
- public CSVFormat withIgnoreSurroundingSpaces(final boolean ignoreSurroundingSpaces) {
- return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
- skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
- }
-
- /**
- * Returns a new {@code CSVFormat} with conversions to and from null for strings on input and output.
- * <ul>
- * <li><strong>Reading:</strong> Converts strings equal to the given {@code nullString} to {@code null} when reading
- * records.</li>
- * <li><strong>Writing:</strong> Writes {@code null} as the given {@code nullString} when writing records.</li>
- * </ul>
- *
- * @param nullString
- * the String to convert to and from {@code null}. No substitution occurs if {@code null}
- *
- * @return A new CSVFormat that is equal to this but with the specified null conversion string.
- */
- public CSVFormat withNullString(final String nullString) {
- return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
- skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
- }
-
- /**
- * Returns a new {@code CSVFormat} with the quoteChar of the format set to the specified character.
- *
- * @param quoteChar
- * the quoteChar character
- * @return A new CSVFormat that is equal to this but with the specified character as quoteChar
- * @throws IllegalArgumentException
- * thrown if the specified character is a line break
- */
- public CSVFormat withQuote(final char quoteChar) {
- return withQuote(Character.valueOf(quoteChar));
- }
-
- /**
- * Returns a new {@code CSVFormat} with the quoteChar of the format set to the specified character.
- *
- * @param quoteChar
- * the quoteChar character, use {@code null} to disable
- * @return A new CSVFormat that is equal to this but with the specified character as quoteChar
- * @throws IllegalArgumentException
- * thrown if the specified character is a line break
- */
- public CSVFormat withQuote(final Character quoteChar) {
- if (isLineBreak(quoteChar)) {
- throw new IllegalArgumentException("The quoteChar cannot be a line break");
- }
- return new CSVFormat(delimiter, quoteChar, quoteMode, commentMarker, escapeCharacter, ignoreSurroundingSpaces,
- ignoreEmptyLines, recordSeparator, nullString, headerComments, header, skipHeaderRecord,
- allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
- }
-
- /**
- * Returns a new {@code CSVFormat} with the output quote policy of the format set to the specified value.
- *
- * @param quoteModePolicy
- * the quote policy to use for output.
- *
- * @return A new CSVFormat that is equal to this but with the specified quote policy
- */
- public CSVFormat withQuoteMode(final QuoteMode quoteModePolicy) {
- return new CSVFormat(delimiter, quoteCharacter, quoteModePolicy, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
- skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
- }
-
- /**
- * Returns a new {@code CSVFormat} with the record separator of the format set to the specified character.
- *
- * <p>
- * <strong>Note:</strong> This setting is only used during printing and does not affect parsing. Parsing currently
- * only works for inputs with '\n', '\r' and "\r\n"
- * </p>
- *
- * @param recordSeparator
- * the record separator to use for output.
- *
- * @return A new CSVFormat that is equal to this but with the specified output record separator
- */
- public CSVFormat withRecordSeparator(final char recordSeparator) {
- return withRecordSeparator(String.valueOf(recordSeparator));
- }
-
- /**
- * Returns a new {@code CSVFormat} with the record separator of the format set to the specified String.
- *
- * <p>
- * <strong>Note:</strong> This setting is only used during printing and does not affect parsing. Parsing currently
- * only works for inputs with '\n', '\r' and "\r\n"
- * </p>
- *
- * @param recordSeparator
- * the record separator to use for output.
- *
- * @return A new CSVFormat that is equal to this but with the specified output record separator
- * @throws IllegalArgumentException
- * if recordSeparator is none of CR, LF or CRLF
- */
- public CSVFormat withRecordSeparator(final String recordSeparator) {
- return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
- skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
- }
-
- /**
- * Returns a new {@code CSVFormat} with skipping the header record set to {@code true}.
- *
- * @return A new CSVFormat that is equal to this but with the the specified skipHeaderRecord setting.
- * @see #withSkipHeaderRecord(boolean)
- * @see #withHeader(String...)
- * @since 1.1
- */
- public CSVFormat withSkipHeaderRecord() {
- return this.withSkipHeaderRecord(true);
- }
-
- /**
- * Returns a new {@code CSVFormat} with whether to skip the header record.
- *
- * @param skipHeaderRecord
- * whether to skip the header record.
- *
- * @return A new CSVFormat that is equal to this but with the the specified skipHeaderRecord setting.
- * @see #withHeader(String...)
- */
- public CSVFormat withSkipHeaderRecord(final boolean skipHeaderRecord) {
- return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
- skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
- }
-
- /**
- * Returns a new {@code CSVFormat} with the record separator of the format set to the operating system's line
- * separator string, typically CR+LF on Windows and LF on Linux.
- *
- * <p>
- * <strong>Note:</strong> This setting is only used during printing and does not affect parsing. Parsing currently
- * only works for inputs with '\n', '\r' and "\r\n"
- * </p>
- *
- * @return A new CSVFormat that is equal to this but with the operating system's line separator stringr
- * @since 1.6
- */
- public CSVFormat withSystemRecordSeparator() {
- return withRecordSeparator(System.getProperty("line.separator"));
- }
-
- /**
- * Returns a new {@code CSVFormat} to add a trailing delimiter.
- *
- * @return A new CSVFormat that is equal to this but with the trailing delimiter setting.
- * @since 1.3
- */
- public CSVFormat withTrailingDelimiter() {
- return withTrailingDelimiter(true);
- }
-
- /**
- * Returns a new {@code CSVFormat} with whether to add a trailing delimiter.
- *
- * @param trailingDelimiter
- * whether to add a trailing delimiter.
- *
- * @return A new CSVFormat that is equal to this but with the specified trailing delimiter setting.
- * @since 1.3
- */
- public CSVFormat withTrailingDelimiter(final boolean trailingDelimiter) {
- return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
- skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
- }
-
- /**
- * Returns a new {@code CSVFormat} to trim leading and trailing blanks.
- *
- * @return A new CSVFormat that is equal to this but with the trim setting on.
- * @since 1.3
- */
- public CSVFormat withTrim() {
- return withTrim(true);
- }
-
- /**
- * Returns a new {@code CSVFormat} with whether to trim leading and trailing blanks.
- *
- * @param trim
- * whether to trim leading and trailing blanks.
- *
- * @return A new CSVFormat that is equal to this but with the specified trim setting.
- * @since 1.3
- */
- public CSVFormat withTrim(final boolean trim) {
- return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
- ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
- skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
- }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org
[6/6] qpid-broker-j git commit: Revert "QPID-8103: [Broker-J] [WMC]
[Query UI] Add ability to download results as CSV"
Posted by or...@apache.org.
Revert "QPID-8103: [Broker-J] [WMC] [Query UI] Add ability to download results as CSV"
This reverts commit 7dbb88471a414d993e4ee3bd3e8fde2f30ca2f46.
Project: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/commit/6ea20349
Tree: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/tree/6ea20349
Diff: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/diff/6ea20349
Branch: refs/heads/master
Commit: 6ea20349ac9e2c0052e889035d604e4f35507c99
Parents: 8c88850
Author: Alex Rudyy <or...@apache.org>
Authored: Mon Feb 26 15:20:45 2018 +0000
Committer: Alex Rudyy <or...@apache.org>
Committed: Mon Feb 26 16:40:48 2018 +0000
----------------------------------------------------------------------
.../plugin/servlet/csv/CSVFormat.java | 320 -------------------
.../plugin/servlet/rest/AbstractServlet.java | 23 --
.../plugin/servlet/rest/QueryServlet.java | 58 +---
.../plugin/servlet/rest/RestServlet.java | 22 ++
.../src/main/java/resources/css/common.css | 4 -
.../resources/js/qpid/management/Management.js | 17 +-
.../js/qpid/management/query/QueryWidget.js | 23 +-
.../main/java/resources/query/QueryWidget.html | 7 -
.../plugin/servlet/csv/CSVFormatTest.java | 84 -----
9 files changed, 32 insertions(+), 526 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/6ea20349/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/csv/CSVFormat.java
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/csv/CSVFormat.java b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/csv/CSVFormat.java
deleted file mode 100644
index 90a5f5f..0000000
--- a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/csv/CSVFormat.java
+++ /dev/null
@@ -1,320 +0,0 @@
-/*
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you 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.qpid.server.management.plugin.servlet.csv;
-
-
-import java.io.IOException;
-import java.util.Collection;
-
-/**
- * Simplified version of CSVFormat from Apache Commons CSV
- */
-public final class CSVFormat
-{
- private static final char COMMA = ',';
-
- private static final char COMMENT = '#';
-
- private static final char CR = '\r';
-
- private static final String CRLF = "\r\n";
-
- private static final Character DOUBLE_QUOTE_CHAR = '"';
-
- private static final String EMPTY = "";
-
- private static final char LF = '\n';
-
- private static final char SP = ' ';
-
- private final char _delimiter;
-
- private final Character _escapeCharacter; // null if escaping is disabled
-
- private final Character _quoteCharacter; // null if quoting is disabled
-
- private final String _recordSeparator; // for outputs
-
- public CSVFormat()
- {
- this(COMMA, DOUBLE_QUOTE_CHAR, null, CRLF);
- }
-
- /**
- * Creates a customized CSV format.
- *
- * @param delimiter the char used for value separation, must not be a line break character
- * @param quoteCharacter the Character used as value encapsulation marker, may be {@code null} to disable
- * @param escapeCharacter the Character used to escape special characters in values, may be {@code null} to disable
- * @param recordSeparator the line separator to use for output
- * @throws IllegalArgumentException if the _delimiter is a line break character
- */
- CSVFormat(final char delimiter,
- final Character quoteCharacter,
- final Character escapeCharacter,
- final String recordSeparator)
- {
- if (delimiter == LF || delimiter == CR)
- {
- throw new IllegalArgumentException("The _delimiter cannot be a line break");
- }
-
- if (quoteCharacter != null && delimiter == quoteCharacter)
- {
- throw new IllegalArgumentException(
- "The quote character and the delimiter cannot be the same ('" + quoteCharacter + "')");
- }
-
- if (escapeCharacter != null && delimiter == escapeCharacter)
- {
- throw new IllegalArgumentException(
- "The escape character and the delimiter cannot be the same ('" + escapeCharacter + "')");
- }
-
- this._delimiter = delimiter;
- this._quoteCharacter = quoteCharacter;
- this._escapeCharacter = escapeCharacter;
- this._recordSeparator = recordSeparator;
- }
-
- public <T extends Collection<?>> void printRecord(final Appendable out, final T record) throws IOException
- {
- boolean newRecord = true;
- for (Object item : record)
- {
- print(out, item, newRecord);
- newRecord = false;
- }
- println(out);
- }
-
- public <C extends Collection<? extends Collection<?>>> void printRecords(final Appendable out, final C records)
- throws IOException
- {
- for (Collection<?> record : records)
- {
- printRecord(out, record);
- }
- }
-
-
- public void println(final Appendable out) throws IOException
- {
- if (_recordSeparator != null)
- {
- out.append(_recordSeparator);
- }
- }
-
- public void print(final Appendable out, final Object value, final boolean newRecord) throws IOException
- {
- CharSequence charSequence;
- if (value == null)
- {
- charSequence = EMPTY;
- }
- else
- {
- charSequence = value instanceof CharSequence ? (CharSequence) value : value.toString();
- }
- this.print(out, value, charSequence, 0, charSequence.length(), newRecord);
- }
-
-
- public void printComments(final Appendable out,
- final String... comments) throws IOException
- {
- for (String comment: comments)
- {
- out.append(COMMENT).append(SP).append(comment);
- println(out);
- }
- }
-
- private void print(final Appendable out,
- final Object object,
- final CharSequence value,
- final int offset,
- final int len,
- final boolean newRecord) throws IOException
- {
- if (!newRecord)
- {
- out.append(_delimiter);
- }
- if (object == null)
- {
- out.append(value);
- }
- else if (_quoteCharacter != null)
- {
- printAndQuote(value, offset, len, out, newRecord);
- }
- else if (_escapeCharacter != null)
- {
- printAndEscape(out, value, offset, len);
- }
- else
- {
- out.append(value, offset, offset + len);
- }
- }
-
- private void printAndEscape(final Appendable out,
- final CharSequence value,
- final int offset,
- final int len)
- throws IOException
- {
- int start = offset;
- int pos = offset;
- final int end = offset + len;
-
- final char escape = _escapeCharacter;
-
- while (pos < end)
- {
- char c = value.charAt(pos);
- if (c == CR || c == LF || c == _delimiter || c == escape)
- {
- // write out segment up until this char
- if (pos > start)
- {
- out.append(value, start, pos);
- }
- if (c == LF)
- {
- c = 'n';
- }
- else if (c == CR)
- {
- c = 'r';
- }
-
- out.append(escape);
- out.append(c);
-
- start = pos + 1; // start on the current char after this one
- }
-
- pos++;
- }
-
- // write last segment
- if (pos > start)
- {
- out.append(value, start, pos);
- }
- }
-
- private void printAndQuote(final CharSequence value, final int offset, final int len,
- final Appendable out, final boolean newRecord) throws IOException
- {
- boolean quote = false;
- int start = offset;
- int pos = offset;
- final int end = offset + len;
-
- final char quoteChar = _quoteCharacter;
-
- if (len <= 0)
- {
- // always quote an empty token that is the first
- // on the line, as it may be the only thing on the
- // line. If it were not quoted in that case,
- // an empty line has no tokens.
- if (newRecord)
- {
- quote = true;
- }
- }
- else
- {
- char c = value.charAt(pos);
-
- if (c <= COMMENT)
- {
- // Some other chars at the start of a value caused the parser to fail, so for now
- // encapsulate if we start in anything less than '#'. We are being conservative
- // by including the default comment char too.
- quote = true;
- }
- else
- {
- while (pos < end)
- {
- c = value.charAt(pos);
- if (c == LF || c == CR || c == quoteChar || c == _delimiter)
- {
- quote = true;
- break;
- }
- pos++;
- }
-
- if (!quote)
- {
- pos = end - 1;
- c = value.charAt(pos);
- // Some other chars at the end caused the parser to fail, so for now
- // encapsulate if we end in anything less than ' '
- if (c <= SP)
- {
- quote = true;
- }
- }
- }
- }
-
- if (!quote)
- {
- // no encapsulation needed - write out the original value
- out.append(value, start, end);
- return;
- }
-
- // we hit something that needed encapsulation
- out.append(quoteChar);
-
- // Pick up where we left off: pos should be positioned on the first character that caused
- // the need for encapsulation.
- while (pos < end)
- {
- final char c = value.charAt(pos);
- if (c == quoteChar)
- {
- // write out the chunk up until this point
-
- // add 1 to the length to write out the encapsulator also
- out.append(value, start, pos + 1);
- // put the next starting position on the encapsulator so we will
- // write it out again with the next string (effectively doubling it)
- start = pos;
- }
- pos++;
- }
-
- // write the last segment
- out.append(value, start, pos);
- out.append(quoteChar);
- }
-
-}
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/6ea20349/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/AbstractServlet.java
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/AbstractServlet.java b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/AbstractServlet.java
index 76d87f1..4403200 100644
--- a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/AbstractServlet.java
+++ b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/AbstractServlet.java
@@ -22,7 +22,6 @@ package org.apache.qpid.server.management.plugin.servlet.rest;
import static org.apache.qpid.server.management.plugin.HttpManagementUtil.CONTENT_ENCODING_HEADER;
import static org.apache.qpid.server.management.plugin.HttpManagementUtil.GZIP_CONTENT_ENCODING;
-import static org.apache.qpid.server.management.plugin.HttpManagementUtil.ensureFilenameIsRfc2183;
import java.io.IOException;
import java.io.OutputStream;
@@ -67,11 +66,6 @@ import org.apache.qpid.server.util.ConnectionScopedRuntimeException;
public abstract class AbstractServlet extends HttpServlet
{
public static final int SC_UNPROCESSABLE_ENTITY = 422;
- /**
- * Signifies that the agent wishes the servlet to set the Content-Disposition on the
- * response with the value attachment. This filename will be derived from the parameter value.
- */
- public static final String CONTENT_DISPOSITION_ATTACHMENT_FILENAME_PARAM = "contentDispositionAttachmentFilename";
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractServlet.class);
public static final String CONTENT_DISPOSITION = "Content-Disposition";
@@ -164,23 +158,6 @@ public abstract class AbstractServlet extends HttpServlet
}
}
- protected void setContentDispositionHeaderIfNecessary(final HttpServletResponse response,
- final String attachmentFilename)
- {
- if (attachmentFilename != null)
- {
- String filenameRfc2183 = ensureFilenameIsRfc2183(attachmentFilename);
- if (filenameRfc2183.length() > 0)
- {
- response.setHeader(CONTENT_DISPOSITION, String.format("attachment; filename=\"%s\"", filenameRfc2183));
- }
- else
- {
- response.setHeader(CONTENT_DISPOSITION, "attachment"); // Agent will allow user to choose a name
- }
- }
- }
-
protected void doPut(HttpServletRequest req,
HttpServletResponse resp,
final ConfiguredObject<?> managedObject) throws ServletException, IOException
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/6ea20349/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/QueryServlet.java
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/QueryServlet.java b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/QueryServlet.java
index 8ae06f8..2b72295 100644
--- a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/QueryServlet.java
+++ b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/QueryServlet.java
@@ -21,8 +21,6 @@
package org.apache.qpid.server.management.plugin.servlet.rest;
import java.io.IOException;
-import java.io.PrintWriter;
-import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -35,18 +33,15 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.qpid.server.filter.SelectorParsingException;
-import org.apache.qpid.server.management.plugin.servlet.csv.CSVFormat;
import org.apache.qpid.server.management.plugin.servlet.query.ConfiguredObjectQuery;
import org.apache.qpid.server.management.plugin.servlet.query.EvaluationException;
import org.apache.qpid.server.model.ConfiguredObject;
-import org.apache.qpid.server.model.Container;
import org.apache.qpid.server.model.Model;
public abstract class QueryServlet<X extends ConfiguredObject<?>> extends AbstractServlet
{
private static final Logger LOGGER = LoggerFactory.getLogger(QueryServlet.class);
- private static final CSVFormat CSV_FORMAT = new CSVFormat();
@Override
protected void doGet(HttpServletRequest request,
@@ -83,6 +78,7 @@ public abstract class QueryServlet<X extends ConfiguredObject<?>> extends Abstra
if (category != null)
{
List<ConfiguredObject<?>> objects = getAllObjects(parent, category, request);
+ Map<String, Object> resultsObject = new LinkedHashMap<>();
try
{
@@ -93,26 +89,10 @@ public abstract class QueryServlet<X extends ConfiguredObject<?>> extends Abstra
request.getParameter("limit"),
request.getParameter("offset"));
-
- String attachmentFilename = request.getParameter(CONTENT_DISPOSITION_ATTACHMENT_FILENAME_PARAM);
- if (attachmentFilename != null)
- {
- setContentDispositionHeaderIfNecessary(response, attachmentFilename);
- }
-
- if ("csv".equalsIgnoreCase(request.getParameter("format")))
- {
- sendCsvResponse(categoryName, parent, query, request, response);
- }
- else
- {
- Map<String, Object> resultsObject = new LinkedHashMap<>();
- resultsObject.put("headers", query.getHeaders());
- resultsObject.put("results", query.getResults());
- resultsObject.put("total", query.getTotalNumberOfRows());
-
- sendJsonResponse(resultsObject, request, response);
- }
+ resultsObject.put("headers", query.getHeaders());
+ resultsObject.put("results", query.getResults());
+ resultsObject.put("total", query.getTotalNumberOfRows());
+ sendJsonResponse(resultsObject, request, response);
}
catch (SelectorParsingException e)
{
@@ -145,34 +125,6 @@ public abstract class QueryServlet<X extends ConfiguredObject<?>> extends Abstra
}
- private void sendCsvResponse(final String categoryName,
- final X parent,
- final ConfiguredObjectQuery query,
- final HttpServletRequest request,
- final HttpServletResponse response)
- throws IOException
- {
- response.setStatus(HttpServletResponse.SC_OK);
- response.setContentType("text/csv;charset=utf-8;");
- response.setCharacterEncoding(StandardCharsets.UTF_8.name());
- sendCachingHeadersOnResponse(response);
- try (PrintWriter writer = response.getWriter())
- {
- CSV_FORMAT.printComments(writer,
- String.format("parent : %s %s ",
- parent.getCategoryClass().getSimpleName(),
- (parent instanceof Container
- ? ""
- : parent.getName())),
- String.format("category : %s", categoryName),
- String.format("select : %s", request.getParameter("select")),
- String.format("where : %s", request.getParameter("where")),
- String.format("order by : %s", request.getParameter("orderBy")));
- CSV_FORMAT.printRecord(writer, query.getHeaders());
- CSV_FORMAT.printRecords(writer, query.getResults());
- }
- }
-
abstract protected X getParent(final HttpServletRequest request, final ConfiguredObject<?> managedObject);
abstract protected Class<? extends ConfiguredObject> getSupportedCategory(final String categoryName,
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/6ea20349/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RestServlet.java
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RestServlet.java b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RestServlet.java
index 4e5e524..8361886 100644
--- a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RestServlet.java
+++ b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RestServlet.java
@@ -81,6 +81,11 @@ public class RestServlet extends AbstractServlet
public static final String EXTRACT_INITIAL_CONFIG_PARAM = "extractInitialConfig";
public static final String EXCLUDE_INHERITED_CONTEXT_PARAM = "excludeInheritedContext";
private static final String SINGLETON_MODEL_OBJECT_RESPONSE_AS_LIST = "singletonModelObjectResponseAsList";
+ /**
+ * Signifies that the agent wishes the servlet to set the Content-Disposition on the
+ * response with the value attachment. This filename will be derived from the parameter value.
+ */
+ public static final String CONTENT_DISPOSITION_ATTACHMENT_FILENAME_PARAM = "contentDispositionAttachmentFilename";
public static final Set<String> RESERVED_PARAMS =
new HashSet<>(Arrays.asList(DEPTH_PARAM,
SORT_PARAM,
@@ -308,6 +313,23 @@ public class RestServlet extends AbstractServlet
return false;
}
+ private void setContentDispositionHeaderIfNecessary(final HttpServletResponse response,
+ final String attachmentFilename)
+ {
+ if (attachmentFilename != null)
+ {
+ String filenameRfc2183 = ensureFilenameIsRfc2183(attachmentFilename);
+ if (filenameRfc2183.length() > 0)
+ {
+ response.setHeader(CONTENT_DISPOSITION, String.format("attachment; filename=\"%s\"", filenameRfc2183));
+ }
+ else
+ {
+ response.setHeader(CONTENT_DISPOSITION, "attachment"); // Agent will allow user to choose a name
+ }
+ }
+ }
+
private Class<? extends ConfiguredObject> getConfiguredClass(HttpServletRequest request, ConfiguredObject<?> managedObject)
{
final String[] servletPathElements = request.getServletPath().split("/");
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/6ea20349/broker-plugins/management-http/src/main/java/resources/css/common.css
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/resources/css/common.css b/broker-plugins/management-http/src/main/java/resources/css/common.css
index b2577c7..e4b2511 100644
--- a/broker-plugins/management-http/src/main/java/resources/css/common.css
+++ b/broker-plugins/management-http/src/main/java/resources/css/common.css
@@ -611,10 +611,6 @@ td.advancedSearchField, col.autoWidth {
background-position: -180px -98px;
}
-.exportIcon.ui-icon {
- background-position: -112px -112px;
-}
-
.claro .searchBox {
padding-right: 16px;
padding-left: 16px;
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/6ea20349/broker-plugins/management-http/src/main/java/resources/js/qpid/management/Management.js
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/resources/js/qpid/management/Management.js b/broker-plugins/management-http/src/main/java/resources/js/qpid/management/Management.js
index bf56bf4..5570636 100644
--- a/broker-plugins/management-http/src/main/java/resources/js/qpid/management/Management.js
+++ b/broker-plugins/management-http/src/main/java/resources/js/qpid/management/Management.js
@@ -612,26 +612,17 @@ define(["dojo/_base/lang",
// Promise returned by dojo.request.xhr with modified then method allowing to use default error handler if none is specified.
Management.prototype.query = function (query)
{
+ var url = "api/latest/" + (query.parent && query.parent.type === "virtualhost" ? "queryvhost/"
+ + this.objectToPath({parent: query.parent}) : "querybroker") + (query.category ? "/"
+ + query.category : "");
var request = {
- url: this.getQueryUrl(query),
+ url: this.getFullUrl(url),
query: {}
};
shallowCopy(query, request.query, ["parent", "category"]);
return this.get(request);
};
- Management.prototype.getQueryUrl = function (query, parameters)
- {
- var url = "api/latest/" + (query.parent && query.parent.type === "virtualhost" ? "queryvhost/"
- + this.objectToPath({parent: query.parent}) : "querybroker") + (query.category ? "/"
- + query.category : "");
- if (parameters)
- {
- url = url + "?" + ioQuery.objectToQuery(parameters);
- }
- return this.getFullUrl(url);
- };
-
Management.prototype.savePreference = function(parentObject, preference)
{
var url = this.buildPreferenceUrl(parentObject, preference.type);
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/6ea20349/broker-plugins/management-http/src/main/java/resources/js/qpid/management/query/QueryWidget.js
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/resources/js/qpid/management/query/QueryWidget.js b/broker-plugins/management-http/src/main/java/resources/js/qpid/management/query/QueryWidget.js
index 799ec67..4f6dad6 100644
--- a/broker-plugins/management-http/src/main/java/resources/js/qpid/management/query/QueryWidget.js
+++ b/broker-plugins/management-http/src/main/java/resources/js/qpid/management/query/QueryWidget.js
@@ -35,7 +35,6 @@ define(["dojo/_base/declare",
"dgrid/extensions/ColumnHider",
"qpid/management/query/QueryGrid",
"qpid/common/MessageDialog",
- "dojox/uuid/generateRandomUuid",
"qpid/management/query/DropDownSelect",
"qpid/management/query/WhereExpression",
"dijit/_WidgetBase",
@@ -62,8 +61,7 @@ define(["dojo/_base/declare",
ColumnReorder,
ColumnHider,
QueryGrid,
- MessageDialog,
- uuid)
+ MessageDialog)
{
var QueryCloneDialogForm = declare("qpid.management.query.QueryCloneDialogForm",
@@ -185,8 +183,6 @@ define(["dojo/_base/declare",
cloneButtonTooltip: null,
deleteButtonTooltip: null,
searchForm: null,
- exportButton: null,
- exportButtonTooltip: null,
/**
* constructor parameter
@@ -245,7 +241,6 @@ define(["dojo/_base/declare",
this.saveButton.on("click", lang.hitch(this, this._saveQuery));
this.cloneButton.on("click", lang.hitch(this, this._cloneQuery));
this.deleteButton.on("click", lang.hitch(this, this._deleteQuery));
- this.exportButton.on("click", lang.hitch(this, this._exportQueryResults));
this._ownQuery = !this.preference
|| !this.preference.owner
@@ -253,7 +248,6 @@ define(["dojo/_base/declare",
var newQuery = !this.preference || !this.preference.createdDate;
this.saveButton.set("disabled", !this._ownQuery);
this.deleteButton.set("disabled", !this._ownQuery || newQuery);
- this.exportButton.set("disabled", true);
if (!this._ownQuery)
{
@@ -543,7 +537,6 @@ define(["dojo/_base/declare",
zeroBased: false,
rowsPerPage: rowsPerPage,
_currentPage: currentPage,
- allowTextSelection: true,
transformer: function (data)
{
var dataResults = data.results;
@@ -592,7 +585,6 @@ define(["dojo/_base/declare",
_queryCompleted: function (e)
{
this._buildColumnsIfHeadersChanged(e.data);
- this.exportButton.set("disabled", !(e.data.total && e.data.total > 0));
},
_buildColumnsIfHeadersChanged: function (data)
{
@@ -985,19 +977,6 @@ define(["dojo/_base/declare",
this.emit("change", {preference: pref});
}
}
- },
- _exportQueryResults: function () {
- var query = this._getQuery();
- query.format = "csv";
- var id = uuid();
- query.contentDispositionAttachmentFilename ="query-results-" + id + ".csv";
- delete query.category;
- var url = this.management.getQueryUrl({category: this.categoryName, parent: this.parentObject}, query);
- var iframe = document.createElement('iframe');
- iframe.id = "query_downloader_" + id;
- iframe.style.display = "none";
- document.body.appendChild(iframe);
- iframe.src = url;
}
});
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/6ea20349/broker-plugins/management-http/src/main/java/resources/query/QueryWidget.html
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/resources/query/QueryWidget.html b/broker-plugins/management-http/src/main/java/resources/query/QueryWidget.html
index 0a28eac..ff1b471 100644
--- a/broker-plugins/management-http/src/main/java/resources/query/QueryWidget.html
+++ b/broker-plugins/management-http/src/main/java/resources/query/QueryWidget.html
@@ -40,13 +40,6 @@
<div data-dojo-attach-point="deleteButtonTooltip"
data-dojo-type="dijit/Tooltip"
data-dojo-props="connectId:'deleteButton_${id}',position:['below']">Delete query from preferences and close the tab</div>
- <div id="exportButton_${id}"
- data-dojo-type="dijit/form/Button"
- data-dojo-attach-point="exportButton"
- data-dojo-props="iconClass: 'exportIcon ui-icon'">Export</div>
- <div data-dojo-attach-point="exportButtonTooltip"
- data-dojo-type="dijit/Tooltip"
- data-dojo-props="connectId:'exportButton_${id}',position:['below']">Export query results into CSV format</div>
<div data-dojo-type="dijit/form/Button"
data-dojo-attach-point="modeButton"
data-dojo-props="iconClass: 'advancedViewIcon ui-icon', title:'Switch to \'Advanced View\' search using SQL-like expressions'"
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/6ea20349/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/servlet/csv/CSVFormatTest.java
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/servlet/csv/CSVFormatTest.java b/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/servlet/csv/CSVFormatTest.java
deleted file mode 100644
index 4adb0b7..0000000
--- a/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/servlet/csv/CSVFormatTest.java
+++ /dev/null
@@ -1,84 +0,0 @@
-/*
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you 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.qpid.server.management.plugin.servlet.csv;
-
-import java.io.StringWriter;
-import java.util.Arrays;
-
-import org.apache.qpid.test.utils.QpidTestCase;
-
-public class CSVFormatTest extends QpidTestCase
-{
-
- public void testPrintRecord() throws Exception
- {
- CSVFormat csvFormat = new CSVFormat();
- final StringWriter out = new StringWriter();
- csvFormat.printRecord(out, Arrays.asList("test", 1, true, "\"quoted\" test"));
- assertEquals("Unexpected format",
- String.format("%s,%d,%b,%s%s", "test", 1, true, "\"\"\"quoted\"\" test\"", "\r\n"),
- out.toString());
- }
-
- public void testPrintRecords() throws Exception
- {
- CSVFormat csvFormat = new CSVFormat();
- final StringWriter out = new StringWriter();
- csvFormat.printRecords(out, Arrays.asList(Arrays.asList("test", 1, true, "\"quoted\" test"),
- Arrays.asList("delimeter,test", 1.0f, false,
- "quote\" in the middle")));
- assertEquals("Unexpected format",
- String.format("%s,%d,%b,%s%s%s,%s,%b,%s%s", "test", 1, true, "\"\"\"quoted\"\" test\"", "\r\n",
- "\"delimeter,test\"", "1.0", false, "\"quote\"\" in the middle\"", "\r\n"),
- out.toString());
- }
-
- public void testPrintln() throws Exception
- {
- CSVFormat csvFormat = new CSVFormat();
- final StringWriter out = new StringWriter();
- csvFormat.println(out);
- assertEquals("Unexpected new line", "\r\n", out.toString());
- }
-
- public void testPrint() throws Exception
- {
- CSVFormat csvFormat = new CSVFormat();
- final StringWriter out = new StringWriter();
- csvFormat.print(out, "test", true);
- csvFormat.print(out, 1, false);
- csvFormat.print(out, true, false);
- csvFormat.print(out, "\"quoted\" test", false);
- assertEquals("Unexpected format ",
- String.format("%s,%d,%b,%s", "test", 1, true, "\"\"\"quoted\"\" test\""),
- out.toString());
- }
-
- public void testPrintComments() throws Exception
- {
- CSVFormat csvFormat = new CSVFormat();
- final StringWriter out = new StringWriter();
- csvFormat.printComments(out, "comment1", "comment2");
- assertEquals("Unexpected format of comments",
- String.format("# %s%s# %s%s", "comment1", "\r\n", "comment2", "\r\n"),
- out.toString());
- }
-}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org
[3/6] qpid-broker-j git commit: QPID-8103: [Broker-J] Import Common
CSV sources from revision 'eede739d18c69722ff39e8e42df6b68ae7627082'
Posted by or...@apache.org.
QPID-8103: [Broker-J] Import Common CSV sources from revision 'eede739d18c69722ff39e8e42df6b68ae7627082'
Project: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/commit/8758e7c8
Tree: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/tree/8758e7c8
Diff: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/diff/8758e7c8
Branch: refs/heads/master
Commit: 8758e7c826943a399bf5c6ee2a1f53db188d9f2d
Parents: 6ea2034
Author: Alex Rudyy <or...@apache.org>
Authored: Mon Feb 26 15:46:52 2018 +0000
Committer: Alex Rudyy <or...@apache.org>
Committed: Mon Feb 26 16:40:48 2018 +0000
----------------------------------------------------------------------
.../management/plugin/csv/Assertions.java | 38 +
.../server/management/plugin/csv/CSVFormat.java | 1983 ++++++++++++++++++
.../server/management/plugin/csv/CSVParser.java | 624 ++++++
.../management/plugin/csv/CSVPrinter.java | 356 ++++
.../server/management/plugin/csv/CSVRecord.java | 276 +++
.../server/management/plugin/csv/Constants.java | 82 +
.../plugin/csv/ExtendedBufferedReader.java | 191 ++
.../server/management/plugin/csv/Lexer.java | 461 ++++
.../server/management/plugin/csv/QuoteMode.java | 50 +
.../server/management/plugin/csv/Token.java | 73 +
10 files changed, 4134 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/8758e7c8/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/Assertions.java
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/Assertions.java b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/Assertions.java
new file mode 100644
index 0000000..63e8d17
--- /dev/null
+++ b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/Assertions.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.qpid.server.management.plugin.csv;
+
+import java.util.Objects;
+
+/**
+ * Utility class for input parameter validation.
+ *
+ * TODO Replace usage with {@link Objects} when we switch to Java 7.
+ */
+final class Assertions {
+
+ private Assertions() {
+ // can not be instantiated
+ }
+
+ public static void notNull(final Object parameter, final String parameterName) {
+ if (parameter == null) {
+ throw new IllegalArgumentException("Parameter '" + parameterName + "' must not be null!");
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/8758e7c8/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVFormat.java
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVFormat.java b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVFormat.java
new file mode 100644
index 0000000..97a0b6c
--- /dev/null
+++ b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVFormat.java
@@ -0,0 +1,1983 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.qpid.server.management.plugin.csv;
+
+import static org.apache.qpid.server.management.plugin.csv.Constants.*;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.Reader;
+import java.io.Serializable;
+import java.io.StringWriter;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * Specifies the format of a CSV file and parses input.
+ *
+ * <h2>Using predefined formats</h2>
+ *
+ * <p>
+ * You can use one of the predefined formats:
+ * </p>
+ *
+ * <ul>
+ * <li>{@link #DEFAULT}</li>
+ * <li>{@link #EXCEL}</li>
+ * <li>{@link #MYSQL}</li>
+ * <li>{@link #RFC4180}</li>
+ * <li>{@link #TDF}</li>
+ * </ul>
+ *
+ * <p>
+ * For example:
+ * </p>
+ *
+ * <pre>
+ * CSVParser parser = CSVFormat.EXCEL.parse(reader);
+ * </pre>
+ *
+ * <p>
+ * The {@link CSVParser} provides static methods to parse other input types, for example:
+ * </p>
+ *
+ * <pre>
+ * CSVParser parser = CSVParser.parse(file, StandardCharsets.US_ASCII, CSVFormat.EXCEL);
+ * </pre>
+ *
+ * <h2>Defining formats</h2>
+ *
+ * <p>
+ * You can extend a format by calling the {@code with} methods. For example:
+ * </p>
+ *
+ * <pre>
+ * CSVFormat.EXCEL.withNullString("N/A").withIgnoreSurroundingSpaces(true);
+ * </pre>
+ *
+ * <h2>Defining column names</h2>
+ *
+ * <p>
+ * To define the column names you want to use to access records, write:
+ * </p>
+ *
+ * <pre>
+ * CSVFormat.EXCEL.withHeader("Col1", "Col2", "Col3");
+ * </pre>
+ *
+ * <p>
+ * Calling {@link #withHeader(String...)} let's you use the given names to address values in a {@link CSVRecord}, and
+ * assumes that your CSV source does not contain a first record that also defines column names.
+ *
+ * If it does, then you are overriding this metadata with your names and you should skip the first record by calling
+ * {@link #withSkipHeaderRecord(boolean)} with {@code true}.
+ * </p>
+ *
+ * <h2>Parsing</h2>
+ *
+ * <p>
+ * You can use a format directly to parse a reader. For example, to parse an Excel file with columns header, write:
+ * </p>
+ *
+ * <pre>
+ * Reader in = ...;
+ * CSVFormat.EXCEL.withHeader("Col1", "Col2", "Col3").parse(in);
+ * </pre>
+ *
+ * <p>
+ * For other input types, like resources, files, and URLs, use the static methods on {@link CSVParser}.
+ * </p>
+ *
+ * <h2>Referencing columns safely</h2>
+ *
+ * <p>
+ * If your source contains a header record, you can simplify your code and safely reference columns, by using
+ * {@link #withHeader(String...)} with no arguments:
+ * </p>
+ *
+ * <pre>
+ * CSVFormat.EXCEL.withHeader();
+ * </pre>
+ *
+ * <p>
+ * This causes the parser to read the first record and use its values as column names.
+ *
+ * Then, call one of the {@link CSVRecord} get method that takes a String column name argument:
+ * </p>
+ *
+ * <pre>
+ * String value = record.get("Col1");
+ * </pre>
+ *
+ * <p>
+ * This makes your code impervious to changes in column order in the CSV file.
+ * </p>
+ *
+ * <h2>Notes</h2>
+ *
+ * <p>
+ * This class is immutable.
+ * </p>
+ */
+public final class CSVFormat implements Serializable {
+
+ /**
+ * Predefines formats.
+ *
+ * @since 1.2
+ */
+ public enum Predefined {
+
+ /**
+ * @see CSVFormat#DEFAULT
+ */
+ Default(CSVFormat.DEFAULT),
+
+ /**
+ * @see CSVFormat#EXCEL
+ */
+ Excel(CSVFormat.EXCEL),
+
+ /**
+ * @see CSVFormat#INFORMIX_UNLOAD
+ * @since 1.3
+ */
+ InformixUnload(CSVFormat.INFORMIX_UNLOAD),
+
+ /**
+ * @see CSVFormat#INFORMIX_UNLOAD_CSV
+ * @since 1.3
+ */
+ InformixUnloadCsv(CSVFormat.INFORMIX_UNLOAD_CSV),
+
+ /**
+ * @see CSVFormat#MYSQL
+ */
+ MySQL(CSVFormat.MYSQL),
+
+ /**
+ * @see CSVFormat#POSTGRESQL_CSV
+ * @since 1.5
+ */
+ PostgreSQLCsv(CSVFormat.POSTGRESQL_CSV),
+
+ /**
+ * @see CSVFormat#POSTGRESQL_CSV
+ */
+ PostgreSQLText(CSVFormat.POSTGRESQL_TEXT),
+
+ /**
+ * @see CSVFormat#RFC4180
+ */
+ RFC4180(CSVFormat.RFC4180),
+
+ /**
+ * @see CSVFormat#TDF
+ */
+ TDF(CSVFormat.TDF);
+
+ private final CSVFormat format;
+
+ Predefined(final CSVFormat format) {
+ this.format = format;
+ }
+
+ /**
+ * Gets the format.
+ *
+ * @return the format.
+ */
+ public CSVFormat getFormat() {
+ return format;
+ }
+ }
+
+ /**
+ * Standard comma separated format, as for {@link #RFC4180} but allowing empty lines.
+ *
+ * <p>
+ * Settings are:
+ * </p>
+ * <ul>
+ * <li>withDelimiter(',')</li>
+ * <li>withQuote('"')</li>
+ * <li>withRecordSeparator("\r\n")</li>
+ * <li>withIgnoreEmptyLines(true)</li>
+ * </ul>
+ *
+ * @see Predefined#Default
+ */
+ public static final CSVFormat DEFAULT = new CSVFormat(COMMA, DOUBLE_QUOTE_CHAR, null, null, null, false, true, CRLF,
+ null, null, null, false, false, false, false, false, false);
+
+ /**
+ * Excel file format (using a comma as the value delimiter). Note that the actual value delimiter used by Excel is
+ * locale dependent, it might be necessary to customize this format to accommodate to your regional settings.
+ *
+ * <p>
+ * For example for parsing or generating a CSV file on a French system the following format will be used:
+ * </p>
+ *
+ * <pre>
+ * CSVFormat fmt = CSVFormat.EXCEL.withDelimiter(';');
+ * </pre>
+ *
+ * <p>
+ * Settings are:
+ * </p>
+ * <ul>
+ * <li>{@link #withDelimiter(char) withDelimiter(',')}</li>
+ * <li>{@link #withQuote(char) withQuote('"')}</li>
+ * <li>{@link #withRecordSeparator(String) withRecordSeparator("\r\n")}</li>
+ * <li>{@link #withIgnoreEmptyLines(boolean) withIgnoreEmptyLines(false)}</li>
+ * <li>{@link #withAllowMissingColumnNames(boolean) withAllowMissingColumnNames(true)}</li>
+ * </ul>
+ * <p>
+ * Note: this is currently like {@link #RFC4180} plus {@link #withAllowMissingColumnNames(boolean)
+ * withAllowMissingColumnNames(true)}.
+ * </p>
+ *
+ * @see Predefined#Excel
+ */
+ // @formatter:off
+ public static final CSVFormat EXCEL = DEFAULT
+ .withIgnoreEmptyLines(false)
+ .withAllowMissingColumnNames();
+ // @formatter:on
+
+ /**
+ * Default Informix CSV UNLOAD format used by the {@code UNLOAD TO file_name} operation.
+ *
+ * <p>
+ * This is a comma-delimited format with a LF character as the line separator. Values are not quoted and special
+ * characters are escaped with {@code '\'}. The default NULL string is {@code "\\N"}.
+ * </p>
+ *
+ * <p>
+ * Settings are:
+ * </p>
+ * <ul>
+ * <li>withDelimiter(',')</li>
+ * <li>withQuote("\"")</li>
+ * <li>withRecordSeparator('\n')</li>
+ * <li>withEscape('\\')</li>
+ * </ul>
+ *
+ * @see Predefined#MySQL
+ * @see <a href=
+ * "http://www.ibm.com/support/knowledgecenter/SSBJG3_2.5.0/com.ibm.gen_busug.doc/c_fgl_InOutSql_UNLOAD.htm">
+ * http://www.ibm.com/support/knowledgecenter/SSBJG3_2.5.0/com.ibm.gen_busug.doc/c_fgl_InOutSql_UNLOAD.htm</a>
+ * @since 1.3
+ */
+ // @formatter:off
+ public static final CSVFormat INFORMIX_UNLOAD = DEFAULT
+ .withDelimiter(PIPE)
+ .withEscape(BACKSLASH)
+ .withQuote(DOUBLE_QUOTE_CHAR)
+ .withRecordSeparator(LF);
+ // @formatter:on
+
+ /**
+ * Default Informix CSV UNLOAD format used by the {@code UNLOAD TO file_name} operation (escaping is disabled.)
+ *
+ * <p>
+ * This is a comma-delimited format with a LF character as the line separator. Values are not quoted and special
+ * characters are escaped with {@code '\'}. The default NULL string is {@code "\\N"}.
+ * </p>
+ *
+ * <p>
+ * Settings are:
+ * </p>
+ * <ul>
+ * <li>withDelimiter(',')</li>
+ * <li>withQuote("\"")</li>
+ * <li>withRecordSeparator('\n')</li>
+ * </ul>
+ *
+ * @see Predefined#MySQL
+ * @see <a href=
+ * "http://www.ibm.com/support/knowledgecenter/SSBJG3_2.5.0/com.ibm.gen_busug.doc/c_fgl_InOutSql_UNLOAD.htm">
+ * http://www.ibm.com/support/knowledgecenter/SSBJG3_2.5.0/com.ibm.gen_busug.doc/c_fgl_InOutSql_UNLOAD.htm</a>
+ * @since 1.3
+ */
+ // @formatter:off
+ public static final CSVFormat INFORMIX_UNLOAD_CSV = DEFAULT
+ .withDelimiter(COMMA)
+ .withQuote(DOUBLE_QUOTE_CHAR)
+ .withRecordSeparator(LF);
+ // @formatter:on
+
+ /**
+ * Default MySQL format used by the {@code SELECT INTO OUTFILE} and {@code LOAD DATA INFILE} operations.
+ *
+ * <p>
+ * This is a tab-delimited format with a LF character as the line separator. Values are not quoted and special
+ * characters are escaped with {@code '\'}. The default NULL string is {@code "\\N"}.
+ * </p>
+ *
+ * <p>
+ * Settings are:
+ * </p>
+ * <ul>
+ * <li>withDelimiter('\t')</li>
+ * <li>withQuote(null)</li>
+ * <li>withRecordSeparator('\n')</li>
+ * <li>withIgnoreEmptyLines(false)</li>
+ * <li>withEscape('\\')</li>
+ * <li>withNullString("\\N")</li>
+ * <li>withQuoteMode(QuoteMode.ALL_NON_NULL)</li>
+ * </ul>
+ *
+ * @see Predefined#MySQL
+ * @see <a href="http://dev.mysql.com/doc/refman/5.1/en/load-data.html"> http://dev.mysql.com/doc/refman/5.1/en/load
+ * -data.html</a>
+ */
+ // @formatter:off
+ public static final CSVFormat MYSQL = DEFAULT
+ .withDelimiter(TAB)
+ .withEscape(BACKSLASH)
+ .withIgnoreEmptyLines(false)
+ .withQuote(null)
+ .withRecordSeparator(LF)
+ .withNullString("\\N")
+ .withQuoteMode(QuoteMode.ALL_NON_NULL);
+ // @formatter:off
+
+ /**
+ * Default PostgreSQL CSV format used by the {@code COPY} operation.
+ *
+ * <p>
+ * This is a comma-delimited format with a LF character as the line separator. Values are double quoted and special
+ * characters are escaped with {@code '"'}. The default NULL string is {@code ""}.
+ * </p>
+ *
+ * <p>
+ * Settings are:
+ * </p>
+ * <ul>
+ * <li>withDelimiter(',')</li>
+ * <li>withQuote('"')</li>
+ * <li>withRecordSeparator('\n')</li>
+ * <li>withIgnoreEmptyLines(false)</li>
+ * <li>withEscape('\\')</li>
+ * <li>withNullString("")</li>
+ * <li>withQuoteMode(QuoteMode.ALL_NON_NULL)</li>
+ * </ul>
+ *
+ * @see Predefined#MySQL
+ * @see <a href="http://dev.mysql.com/doc/refman/5.1/en/load-data.html"> http://dev.mysql.com/doc/refman/5.1/en/load
+ * -data.html</a>
+ * @since 1.5
+ */
+ // @formatter:off
+ public static final CSVFormat POSTGRESQL_CSV = DEFAULT
+ .withDelimiter(COMMA)
+ .withEscape(DOUBLE_QUOTE_CHAR)
+ .withIgnoreEmptyLines(false)
+ .withQuote(DOUBLE_QUOTE_CHAR)
+ .withRecordSeparator(LF)
+ .withNullString(EMPTY)
+ .withQuoteMode(QuoteMode.ALL_NON_NULL);
+ // @formatter:off
+
+ /**
+ * Default PostgreSQL text format used by the {@code COPY} operation.
+ *
+ * <p>
+ * This is a tab-delimited format with a LF character as the line separator. Values are double quoted and special
+ * characters are escaped with {@code '"'}. The default NULL string is {@code "\\N"}.
+ * </p>
+ *
+ * <p>
+ * Settings are:
+ * </p>
+ * <ul>
+ * <li>withDelimiter('\t')</li>
+ * <li>withQuote('"')</li>
+ * <li>withRecordSeparator('\n')</li>
+ * <li>withIgnoreEmptyLines(false)</li>
+ * <li>withEscape('\\')</li>
+ * <li>withNullString("\\N")</li>
+ * <li>withQuoteMode(QuoteMode.ALL_NON_NULL)</li>
+ * </ul>
+ *
+ * @see Predefined#MySQL
+ * @see <a href="http://dev.mysql.com/doc/refman/5.1/en/load-data.html"> http://dev.mysql.com/doc/refman/5.1/en/load
+ * -data.html</a>
+ * @since 1.5
+ */
+ // @formatter:off
+ public static final CSVFormat POSTGRESQL_TEXT = DEFAULT
+ .withDelimiter(TAB)
+ .withEscape(DOUBLE_QUOTE_CHAR)
+ .withIgnoreEmptyLines(false)
+ .withQuote(DOUBLE_QUOTE_CHAR)
+ .withRecordSeparator(LF)
+ .withNullString("\\N")
+ .withQuoteMode(QuoteMode.ALL_NON_NULL);
+ // @formatter:off
+
+ /**
+ * Comma separated format as defined by <a href="http://tools.ietf.org/html/rfc4180">RFC 4180</a>.
+ *
+ * <p>
+ * Settings are:
+ * </p>
+ * <ul>
+ * <li>withDelimiter(',')</li>
+ * <li>withQuote('"')</li>
+ * <li>withRecordSeparator("\r\n")</li>
+ * <li>withIgnoreEmptyLines(false)</li>
+ * </ul>
+ *
+ * @see Predefined#RFC4180
+ */
+ public static final CSVFormat RFC4180 = DEFAULT.withIgnoreEmptyLines(false);
+
+ private static final long serialVersionUID = 1L;
+
+ /**
+ * Tab-delimited format.
+ *
+ * <p>
+ * Settings are:
+ * </p>
+ * <ul>
+ * <li>withDelimiter('\t')</li>
+ * <li>withQuote('"')</li>
+ * <li>withRecordSeparator("\r\n")</li>
+ * <li>withIgnoreSurroundingSpaces(true)</li>
+ * </ul>
+ *
+ * @see Predefined#TDF
+ */
+ // @formatter:off
+ public static final CSVFormat TDF = DEFAULT
+ .withDelimiter(TAB)
+ .withIgnoreSurroundingSpaces();
+ // @formatter:on
+
+ /**
+ * Returns true if the given character is a line break character.
+ *
+ * @param c
+ * the character to check
+ *
+ * @return true if <code>c</code> is a line break character
+ */
+ private static boolean isLineBreak(final char c) {
+ return c == LF || c == CR;
+ }
+
+ /**
+ * Returns true if the given character is a line break character.
+ *
+ * @param c
+ * the character to check, may be null
+ *
+ * @return true if <code>c</code> is a line break character (and not null)
+ */
+ private static boolean isLineBreak(final Character c) {
+ return c != null && isLineBreak(c.charValue());
+ }
+
+ /**
+ * Creates a new CSV format with the specified delimiter.
+ *
+ * <p>
+ * Use this method if you want to create a CSVFormat from scratch. All fields but the delimiter will be initialized
+ * with null/false.
+ * </p>
+ *
+ * @param delimiter
+ * the char used for value separation, must not be a line break character
+ * @return a new CSV format.
+ * @throws IllegalArgumentException
+ * if the delimiter is a line break character
+ *
+ * @see #DEFAULT
+ * @see #RFC4180
+ * @see #MYSQL
+ * @see #EXCEL
+ * @see #TDF
+ */
+ public static CSVFormat newFormat(final char delimiter) {
+ return new CSVFormat(delimiter, null, null, null, null, false, false, null, null, null, null, false, false,
+ false, false, false, false);
+ }
+
+ /**
+ * Gets one of the predefined formats from {@link CSVFormat.Predefined}.
+ *
+ * @param format
+ * name
+ * @return one of the predefined formats
+ * @since 1.2
+ */
+ public static CSVFormat valueOf(final String format) {
+ return CSVFormat.Predefined.valueOf(format).getFormat();
+ }
+
+ private final boolean allowMissingColumnNames;
+
+ private final Character commentMarker; // null if commenting is disabled
+
+ private final char delimiter;
+
+ private final Character escapeCharacter; // null if escaping is disabled
+
+ private final String[] header; // array of header column names
+
+ private final String[] headerComments; // array of header comment lines
+
+ private final boolean ignoreEmptyLines;
+
+ private final boolean ignoreHeaderCase; // should ignore header names case
+
+ private final boolean ignoreSurroundingSpaces; // Should leading/trailing spaces be ignored around values?
+
+ private final String nullString; // the string to be used for null values
+
+ private final Character quoteCharacter; // null if quoting is disabled
+
+ private final QuoteMode quoteMode;
+
+ private final String recordSeparator; // for outputs
+
+ private final boolean skipHeaderRecord;
+
+ private final boolean trailingDelimiter;
+
+ private final boolean trim;
+
+ private final boolean autoFlush;
+
+ /**
+ * Creates a customized CSV format.
+ *
+ * @param delimiter
+ * the char used for value separation, must not be a line break character
+ * @param quoteChar
+ * the Character used as value encapsulation marker, may be {@code null} to disable
+ * @param quoteMode
+ * the quote mode
+ * @param commentStart
+ * the Character used for comment identification, may be {@code null} to disable
+ * @param escape
+ * the Character used to escape special characters in values, may be {@code null} to disable
+ * @param ignoreSurroundingSpaces
+ * {@code true} when whitespaces enclosing values should be ignored
+ * @param ignoreEmptyLines
+ * {@code true} when the parser should skip empty lines
+ * @param recordSeparator
+ * the line separator to use for output
+ * @param nullString
+ * the line separator to use for output
+ * @param headerComments
+ * the comments to be printed by the Printer before the actual CSV data
+ * @param header
+ * the header
+ * @param skipHeaderRecord
+ * TODO
+ * @param allowMissingColumnNames
+ * TODO
+ * @param ignoreHeaderCase
+ * TODO
+ * @param trim
+ * TODO
+ * @param trailingDelimiter
+ * TODO
+ * @param autoFlush
+ * @throws IllegalArgumentException
+ * if the delimiter is a line break character
+ */
+ private CSVFormat(final char delimiter, final Character quoteChar, final QuoteMode quoteMode,
+ final Character commentStart, final Character escape, final boolean ignoreSurroundingSpaces,
+ final boolean ignoreEmptyLines, final String recordSeparator, final String nullString,
+ final Object[] headerComments, final String[] header, final boolean skipHeaderRecord,
+ final boolean allowMissingColumnNames, final boolean ignoreHeaderCase, final boolean trim,
+ final boolean trailingDelimiter, final boolean autoFlush) {
+ this.delimiter = delimiter;
+ this.quoteCharacter = quoteChar;
+ this.quoteMode = quoteMode;
+ this.commentMarker = commentStart;
+ this.escapeCharacter = escape;
+ this.ignoreSurroundingSpaces = ignoreSurroundingSpaces;
+ this.allowMissingColumnNames = allowMissingColumnNames;
+ this.ignoreEmptyLines = ignoreEmptyLines;
+ this.recordSeparator = recordSeparator;
+ this.nullString = nullString;
+ this.headerComments = toStringArray(headerComments);
+ this.header = header == null ? null : header.clone();
+ this.skipHeaderRecord = skipHeaderRecord;
+ this.ignoreHeaderCase = ignoreHeaderCase;
+ this.trailingDelimiter = trailingDelimiter;
+ this.trim = trim;
+ this.autoFlush = autoFlush;
+ validate();
+ }
+
+ @Override
+ public boolean equals(final Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (getClass() != obj.getClass()) {
+ return false;
+ }
+
+ final CSVFormat other = (CSVFormat) obj;
+ if (delimiter != other.delimiter) {
+ return false;
+ }
+ if (quoteMode != other.quoteMode) {
+ return false;
+ }
+ if (quoteCharacter == null) {
+ if (other.quoteCharacter != null) {
+ return false;
+ }
+ } else if (!quoteCharacter.equals(other.quoteCharacter)) {
+ return false;
+ }
+ if (commentMarker == null) {
+ if (other.commentMarker != null) {
+ return false;
+ }
+ } else if (!commentMarker.equals(other.commentMarker)) {
+ return false;
+ }
+ if (escapeCharacter == null) {
+ if (other.escapeCharacter != null) {
+ return false;
+ }
+ } else if (!escapeCharacter.equals(other.escapeCharacter)) {
+ return false;
+ }
+ if (nullString == null) {
+ if (other.nullString != null) {
+ return false;
+ }
+ } else if (!nullString.equals(other.nullString)) {
+ return false;
+ }
+ if (!Arrays.equals(header, other.header)) {
+ return false;
+ }
+ if (ignoreSurroundingSpaces != other.ignoreSurroundingSpaces) {
+ return false;
+ }
+ if (ignoreEmptyLines != other.ignoreEmptyLines) {
+ return false;
+ }
+ if (skipHeaderRecord != other.skipHeaderRecord) {
+ return false;
+ }
+ if (recordSeparator == null) {
+ if (other.recordSeparator != null) {
+ return false;
+ }
+ } else if (!recordSeparator.equals(other.recordSeparator)) {
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Formats the specified values.
+ *
+ * @param values
+ * the values to format
+ * @return the formatted values
+ */
+ public String format(final Object... values) {
+ final StringWriter out = new StringWriter();
+ try (final CSVPrinter csvPrinter = new CSVPrinter(out, this)) {
+ csvPrinter.printRecord(values);
+ return out.toString().trim();
+ } catch (final IOException e) {
+ // should not happen because a StringWriter does not do IO.
+ throw new IllegalStateException(e);
+ }
+ }
+
+ /**
+ * Specifies whether missing column names are allowed when parsing the header line.
+ *
+ * @return {@code true} if missing column names are allowed when parsing the header line, {@code false} to throw an
+ * {@link IllegalArgumentException}.
+ */
+ public boolean getAllowMissingColumnNames() {
+ return allowMissingColumnNames;
+ }
+
+ /**
+ * Returns whether to flush on close.
+ *
+ * @return whether to flush on close.
+ * @since 1.6
+ */
+ public boolean getAutoFlush() {
+ return autoFlush;
+ }
+
+ /**
+ * Returns the character marking the start of a line comment.
+ *
+ * @return the comment start marker, may be {@code null}
+ */
+ public Character getCommentMarker() {
+ return commentMarker;
+ }
+
+ /**
+ * Returns the character delimiting the values (typically ';', ',' or '\t').
+ *
+ * @return the delimiter character
+ */
+ public char getDelimiter() {
+ return delimiter;
+ }
+
+ /**
+ * Returns the escape character.
+ *
+ * @return the escape character, may be {@code null}
+ */
+ public Character getEscapeCharacter() {
+ return escapeCharacter;
+ }
+
+ /**
+ * Returns a copy of the header array.
+ *
+ * @return a copy of the header array; {@code null} if disabled, the empty array if to be read from the file
+ */
+ public String[] getHeader() {
+ return header != null ? header.clone() : null;
+ }
+
+ /**
+ * Returns a copy of the header comment array.
+ *
+ * @return a copy of the header comment array; {@code null} if disabled.
+ */
+ public String[] getHeaderComments() {
+ return headerComments != null ? headerComments.clone() : null;
+ }
+
+ /**
+ * Specifies whether empty lines between records are ignored when parsing input.
+ *
+ * @return {@code true} if empty lines between records are ignored, {@code false} if they are turned into empty
+ * records.
+ */
+ public boolean getIgnoreEmptyLines() {
+ return ignoreEmptyLines;
+ }
+
+ /**
+ * Specifies whether header names will be accessed ignoring case.
+ *
+ * @return {@code true} if header names cases are ignored, {@code false} if they are case sensitive.
+ * @since 1.3
+ */
+ public boolean getIgnoreHeaderCase() {
+ return ignoreHeaderCase;
+ }
+
+ /**
+ * Specifies whether spaces around values are ignored when parsing input.
+ *
+ * @return {@code true} if spaces around values are ignored, {@code false} if they are treated as part of the value.
+ */
+ public boolean getIgnoreSurroundingSpaces() {
+ return ignoreSurroundingSpaces;
+ }
+
+ /**
+ * Gets the String to convert to and from {@code null}.
+ * <ul>
+ * <li><strong>Reading:</strong> Converts strings equal to the given {@code nullString} to {@code null} when reading
+ * records.</li>
+ * <li><strong>Writing:</strong> Writes {@code null} as the given {@code nullString} when writing records.</li>
+ * </ul>
+ *
+ * @return the String to convert to and from {@code null}. No substitution occurs if {@code null}
+ */
+ public String getNullString() {
+ return nullString;
+ }
+
+ /**
+ * Returns the character used to encapsulate values containing special characters.
+ *
+ * @return the quoteChar character, may be {@code null}
+ */
+ public Character getQuoteCharacter() {
+ return quoteCharacter;
+ }
+
+ /**
+ * Returns the quote policy output fields.
+ *
+ * @return the quote policy
+ */
+ public QuoteMode getQuoteMode() {
+ return quoteMode;
+ }
+
+ /**
+ * Returns the record separator delimiting output records.
+ *
+ * @return the record separator
+ */
+ public String getRecordSeparator() {
+ return recordSeparator;
+ }
+
+ /**
+ * Returns whether to skip the header record.
+ *
+ * @return whether to skip the header record.
+ */
+ public boolean getSkipHeaderRecord() {
+ return skipHeaderRecord;
+ }
+
+ /**
+ * Returns whether to add a trailing delimiter.
+ *
+ * @return whether to add a trailing delimiter.
+ * @since 1.3
+ */
+ public boolean getTrailingDelimiter() {
+ return trailingDelimiter;
+ }
+
+ /**
+ * Returns whether to trim leading and trailing blanks.
+ *
+ * @return whether to trim leading and trailing blanks.
+ */
+ public boolean getTrim() {
+ return trim;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+
+ result = prime * result + delimiter;
+ result = prime * result + ((quoteMode == null) ? 0 : quoteMode.hashCode());
+ result = prime * result + ((quoteCharacter == null) ? 0 : quoteCharacter.hashCode());
+ result = prime * result + ((commentMarker == null) ? 0 : commentMarker.hashCode());
+ result = prime * result + ((escapeCharacter == null) ? 0 : escapeCharacter.hashCode());
+ result = prime * result + ((nullString == null) ? 0 : nullString.hashCode());
+ result = prime * result + (ignoreSurroundingSpaces ? 1231 : 1237);
+ result = prime * result + (ignoreHeaderCase ? 1231 : 1237);
+ result = prime * result + (ignoreEmptyLines ? 1231 : 1237);
+ result = prime * result + (skipHeaderRecord ? 1231 : 1237);
+ result = prime * result + ((recordSeparator == null) ? 0 : recordSeparator.hashCode());
+ result = prime * result + Arrays.hashCode(header);
+ return result;
+ }
+
+ /**
+ * Specifies whether comments are supported by this format.
+ *
+ * Note that the comment introducer character is only recognized at the start of a line.
+ *
+ * @return {@code true} is comments are supported, {@code false} otherwise
+ */
+ public boolean isCommentMarkerSet() {
+ return commentMarker != null;
+ }
+
+ /**
+ * Returns whether escape are being processed.
+ *
+ * @return {@code true} if escapes are processed
+ */
+ public boolean isEscapeCharacterSet() {
+ return escapeCharacter != null;
+ }
+
+ /**
+ * Returns whether a nullString has been defined.
+ *
+ * @return {@code true} if a nullString is defined
+ */
+ public boolean isNullStringSet() {
+ return nullString != null;
+ }
+
+ /**
+ * Returns whether a quoteChar has been defined.
+ *
+ * @return {@code true} if a quoteChar is defined
+ */
+ public boolean isQuoteCharacterSet() {
+ return quoteCharacter != null;
+ }
+
+ /**
+ * Parses the specified content.
+ *
+ * <p>
+ * See also the various static parse methods on {@link CSVParser}.
+ * </p>
+ *
+ * @param in
+ * the input stream
+ * @return a parser over a stream of {@link CSVRecord}s.
+ * @throws IOException
+ * If an I/O error occurs
+ */
+ public CSVParser parse(final Reader in) throws IOException {
+ return new CSVParser(in, this);
+ }
+
+ /**
+ * Prints to the specified output.
+ *
+ * <p>
+ * See also {@link CSVPrinter}.
+ * </p>
+ *
+ * @param out
+ * the output.
+ * @return a printer to an output.
+ * @throws IOException
+ * thrown if the optional header cannot be printed.
+ */
+ public CSVPrinter print(final Appendable out) throws IOException {
+ return new CSVPrinter(out, this);
+ }
+
+ /**
+ * Prints to the specified output.
+ *
+ * <p>
+ * See also {@link CSVPrinter}.
+ * </p>
+ *
+ * @param out
+ * the output.
+ * @param charset
+ * A charset.
+ * @return a printer to an output.
+ * @throws IOException
+ * thrown if the optional header cannot be printed.
+ * @since 1.5
+ */
+ @SuppressWarnings("resource")
+ public CSVPrinter print(final File out, final Charset charset) throws IOException {
+ // The writer will be closed when close() is called.
+ return new CSVPrinter(new OutputStreamWriter(new FileOutputStream(out), charset), this);
+ }
+
+ /**
+ * Prints the {@code value} as the next value on the line to {@code out}. The value will be escaped or encapsulated
+ * as needed. Useful when one wants to avoid creating CSVPrinters.
+ *
+ * @param value
+ * value to output.
+ * @param out
+ * where to print the value.
+ * @param newRecord
+ * if this a new record.
+ * @throws IOException
+ * If an I/O error occurs.
+ * @since 1.4
+ */
+ public void print(final Object value, final Appendable out, final boolean newRecord) throws IOException {
+ // null values are considered empty
+ // Only call CharSequence.toString() if you have to, helps GC-free use cases.
+ CharSequence charSequence;
+ if (value == null) {
+ // https://issues.apache.org/jira/browse/CSV-203
+ if (null == nullString) {
+ charSequence = EMPTY;
+ } else {
+ if (QuoteMode.ALL == quoteMode) {
+ charSequence = quoteCharacter + nullString + quoteCharacter;
+ } else {
+ charSequence = nullString;
+ }
+ }
+ } else {
+ charSequence = value instanceof CharSequence ? (CharSequence) value : value.toString();
+ }
+ charSequence = getTrim() ? trim(charSequence) : charSequence;
+ this.print(value, charSequence, 0, charSequence.length(), out, newRecord);
+ }
+
+ private void print(final Object object, final CharSequence value, final int offset, final int len,
+ final Appendable out, final boolean newRecord) throws IOException {
+ if (!newRecord) {
+ out.append(getDelimiter());
+ }
+ if (object == null) {
+ out.append(value);
+ } else if (isQuoteCharacterSet()) {
+ // the original object is needed so can check for Number
+ printAndQuote(object, value, offset, len, out, newRecord);
+ } else if (isEscapeCharacterSet()) {
+ printAndEscape(value, offset, len, out);
+ } else {
+ out.append(value, offset, offset + len);
+ }
+ }
+
+ /**
+ * Prints to the specified output.
+ *
+ * <p>
+ * See also {@link CSVPrinter}.
+ * </p>
+ *
+ * @param out
+ * the output.
+ * @param charset
+ * A charset.
+ * @return a printer to an output.
+ * @throws IOException
+ * thrown if the optional header cannot be printed.
+ * @since 1.5
+ */
+ public CSVPrinter print(final Path out, final Charset charset) throws IOException {
+ return print(Files.newBufferedWriter(out, charset));
+ }
+
+ /*
+ * Note: must only be called if escaping is enabled, otherwise will generate NPE
+ */
+ private void printAndEscape(final CharSequence value, final int offset, final int len, final Appendable out)
+ throws IOException {
+ int start = offset;
+ int pos = offset;
+ final int end = offset + len;
+
+ final char delim = getDelimiter();
+ final char escape = getEscapeCharacter().charValue();
+
+ while (pos < end) {
+ char c = value.charAt(pos);
+ if (c == CR || c == LF || c == delim || c == escape) {
+ // write out segment up until this char
+ if (pos > start) {
+ out.append(value, start, pos);
+ }
+ if (c == LF) {
+ c = 'n';
+ } else if (c == CR) {
+ c = 'r';
+ }
+
+ out.append(escape);
+ out.append(c);
+
+ start = pos + 1; // start on the current char after this one
+ }
+
+ pos++;
+ }
+
+ // write last segment
+ if (pos > start) {
+ out.append(value, start, pos);
+ }
+ }
+
+ /*
+ * Note: must only be called if quoting is enabled, otherwise will generate NPE
+ */
+ // the original object is needed so can check for Number
+ private void printAndQuote(final Object object, final CharSequence value, final int offset, final int len,
+ final Appendable out, final boolean newRecord) throws IOException {
+ boolean quote = false;
+ int start = offset;
+ int pos = offset;
+ final int end = offset + len;
+
+ final char delimChar = getDelimiter();
+ final char quoteChar = getQuoteCharacter().charValue();
+
+ QuoteMode quoteModePolicy = getQuoteMode();
+ if (quoteModePolicy == null) {
+ quoteModePolicy = QuoteMode.MINIMAL;
+ }
+ switch (quoteModePolicy) {
+ case ALL:
+ case ALL_NON_NULL:
+ quote = true;
+ break;
+ case NON_NUMERIC:
+ quote = !(object instanceof Number);
+ break;
+ case NONE:
+ // Use the existing escaping code
+ printAndEscape(value, offset, len, out);
+ return;
+ case MINIMAL:
+ if (len <= 0) {
+ // always quote an empty token that is the first
+ // on the line, as it may be the only thing on the
+ // line. If it were not quoted in that case,
+ // an empty line has no tokens.
+ if (newRecord) {
+ quote = true;
+ }
+ } else {
+ char c = value.charAt(pos);
+
+ if (c <= COMMENT) {
+ // Some other chars at the start of a value caused the parser to fail, so for now
+ // encapsulate if we start in anything less than '#'. We are being conservative
+ // by including the default comment char too.
+ quote = true;
+ } else {
+ while (pos < end) {
+ c = value.charAt(pos);
+ if (c == LF || c == CR || c == quoteChar || c == delimChar) {
+ quote = true;
+ break;
+ }
+ pos++;
+ }
+
+ if (!quote) {
+ pos = end - 1;
+ c = value.charAt(pos);
+ // Some other chars at the end caused the parser to fail, so for now
+ // encapsulate if we end in anything less than ' '
+ if (c <= SP) {
+ quote = true;
+ }
+ }
+ }
+ }
+
+ if (!quote) {
+ // no encapsulation needed - write out the original value
+ out.append(value, start, end);
+ return;
+ }
+ break;
+ default:
+ throw new IllegalStateException("Unexpected Quote value: " + quoteModePolicy);
+ }
+
+ if (!quote) {
+ // no encapsulation needed - write out the original value
+ out.append(value, start, end);
+ return;
+ }
+
+ // we hit something that needed encapsulation
+ out.append(quoteChar);
+
+ // Pick up where we left off: pos should be positioned on the first character that caused
+ // the need for encapsulation.
+ while (pos < end) {
+ final char c = value.charAt(pos);
+ if (c == quoteChar) {
+ // write out the chunk up until this point
+
+ // add 1 to the length to write out the encapsulator also
+ out.append(value, start, pos + 1);
+ // put the next starting position on the encapsulator so we will
+ // write it out again with the next string (effectively doubling it)
+ start = pos;
+ }
+ pos++;
+ }
+
+ // write the last segment
+ out.append(value, start, pos);
+ out.append(quoteChar);
+ }
+
+ /**
+ * Prints to the {@link System#out}.
+ *
+ * <p>
+ * See also {@link CSVPrinter}.
+ * </p>
+ *
+ * @return a printer to {@link System#out}.
+ * @throws IOException
+ * thrown if the optional header cannot be printed.
+ * @since 1.5
+ */
+ public CSVPrinter printer() throws IOException {
+ return new CSVPrinter(System.out, this);
+ }
+
+ /**
+ * Outputs the trailing delimiter (if set) followed by the record separator (if set).
+ *
+ * @param out
+ * where to write
+ * @throws IOException
+ * If an I/O error occurs
+ * @since 1.4
+ */
+ public void println(final Appendable out) throws IOException {
+ if (getTrailingDelimiter()) {
+ out.append(getDelimiter());
+ }
+ if (recordSeparator != null) {
+ out.append(recordSeparator);
+ }
+ }
+
+ /**
+ * Prints the given {@code values} to {@code out} as a single record of delimiter separated values followed by the
+ * record separator.
+ *
+ * <p>
+ * The values will be quoted if needed. Quotes and new-line characters will be escaped. This method adds the record
+ * separator to the output after printing the record, so there is no need to call {@link #println(Appendable)}.
+ * </p>
+ *
+ * @param out
+ * where to write.
+ * @param values
+ * values to output.
+ * @throws IOException
+ * If an I/O error occurs.
+ * @since 1.4
+ */
+ public void printRecord(final Appendable out, final Object... values) throws IOException {
+ for (int i = 0; i < values.length; i++) {
+ print(values[i], out, i == 0);
+ }
+ println(out);
+ }
+
+ @Override
+ public String toString() {
+ final StringBuilder sb = new StringBuilder();
+ sb.append("Delimiter=<").append(delimiter).append('>');
+ if (isEscapeCharacterSet()) {
+ sb.append(' ');
+ sb.append("Escape=<").append(escapeCharacter).append('>');
+ }
+ if (isQuoteCharacterSet()) {
+ sb.append(' ');
+ sb.append("QuoteChar=<").append(quoteCharacter).append('>');
+ }
+ if (isCommentMarkerSet()) {
+ sb.append(' ');
+ sb.append("CommentStart=<").append(commentMarker).append('>');
+ }
+ if (isNullStringSet()) {
+ sb.append(' ');
+ sb.append("NullString=<").append(nullString).append('>');
+ }
+ if (recordSeparator != null) {
+ sb.append(' ');
+ sb.append("RecordSeparator=<").append(recordSeparator).append('>');
+ }
+ if (getIgnoreEmptyLines()) {
+ sb.append(" EmptyLines:ignored");
+ }
+ if (getIgnoreSurroundingSpaces()) {
+ sb.append(" SurroundingSpaces:ignored");
+ }
+ if (getIgnoreHeaderCase()) {
+ sb.append(" IgnoreHeaderCase:ignored");
+ }
+ sb.append(" SkipHeaderRecord:").append(skipHeaderRecord);
+ if (headerComments != null) {
+ sb.append(' ');
+ sb.append("HeaderComments:").append(Arrays.toString(headerComments));
+ }
+ if (header != null) {
+ sb.append(' ');
+ sb.append("Header:").append(Arrays.toString(header));
+ }
+ return sb.toString();
+ }
+
+ private String[] toStringArray(final Object[] values) {
+ if (values == null) {
+ return null;
+ }
+ final String[] strings = new String[values.length];
+ for (int i = 0; i < values.length; i++) {
+ final Object value = values[i];
+ strings[i] = value == null ? null : value.toString();
+ }
+ return strings;
+ }
+
+ private CharSequence trim(final CharSequence charSequence) {
+ if (charSequence instanceof String) {
+ return ((String) charSequence).trim();
+ }
+ final int count = charSequence.length();
+ int len = count;
+ int pos = 0;
+
+ while (pos < len && charSequence.charAt(pos) <= SP) {
+ pos++;
+ }
+ while (pos < len && charSequence.charAt(len - 1) <= SP) {
+ len--;
+ }
+ return pos > 0 || len < count ? charSequence.subSequence(pos, len) : charSequence;
+ }
+
+ /**
+ * Verifies the consistency of the parameters and throws an IllegalArgumentException if necessary.
+ *
+ * @throws IllegalArgumentException
+ */
+ private void validate() throws IllegalArgumentException {
+ if (isLineBreak(delimiter)) {
+ throw new IllegalArgumentException("The delimiter cannot be a line break");
+ }
+
+ if (quoteCharacter != null && delimiter == quoteCharacter.charValue()) {
+ throw new IllegalArgumentException(
+ "The quoteChar character and the delimiter cannot be the same ('" + quoteCharacter + "')");
+ }
+
+ if (escapeCharacter != null && delimiter == escapeCharacter.charValue()) {
+ throw new IllegalArgumentException(
+ "The escape character and the delimiter cannot be the same ('" + escapeCharacter + "')");
+ }
+
+ if (commentMarker != null && delimiter == commentMarker.charValue()) {
+ throw new IllegalArgumentException(
+ "The comment start character and the delimiter cannot be the same ('" + commentMarker + "')");
+ }
+
+ if (quoteCharacter != null && quoteCharacter.equals(commentMarker)) {
+ throw new IllegalArgumentException(
+ "The comment start character and the quoteChar cannot be the same ('" + commentMarker + "')");
+ }
+
+ if (escapeCharacter != null && escapeCharacter.equals(commentMarker)) {
+ throw new IllegalArgumentException(
+ "The comment start and the escape character cannot be the same ('" + commentMarker + "')");
+ }
+
+ if (escapeCharacter == null && quoteMode == QuoteMode.NONE) {
+ throw new IllegalArgumentException("No quotes mode set but no escape character is set");
+ }
+
+ // validate header
+ if (header != null) {
+ final Set<String> dupCheck = new HashSet<>();
+ for (final String hdr : header) {
+ if (!dupCheck.add(hdr)) {
+ throw new IllegalArgumentException(
+ "The header contains a duplicate entry: '" + hdr + "' in " + Arrays.toString(header));
+ }
+ }
+ }
+ }
+
+ /**
+ * Returns a new {@code CSVFormat} with the missing column names behavior of the format set to {@code true}
+ *
+ * @return A new CSVFormat that is equal to this but with the specified missing column names behavior.
+ * @see #withAllowMissingColumnNames(boolean)
+ * @since 1.1
+ */
+ public CSVFormat withAllowMissingColumnNames() {
+ return this.withAllowMissingColumnNames(true);
+ }
+
+ /**
+ * Returns a new {@code CSVFormat} with the missing column names behavior of the format set to the given value.
+ *
+ * @param allowMissingColumnNames
+ * the missing column names behavior, {@code true} to allow missing column names in the header line,
+ * {@code false} to cause an {@link IllegalArgumentException} to be thrown.
+ * @return A new CSVFormat that is equal to this but with the specified missing column names behavior.
+ */
+ public CSVFormat withAllowMissingColumnNames(final boolean allowMissingColumnNames) {
+ return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
+ skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
+ }
+
+ /**
+ * Returns a new {@code CSVFormat} with whether to flush on close.
+ *
+ * @param autoFlush
+ * whether to flush on close.
+ *
+ * @return A new CSVFormat that is equal to this but with the specified autoFlush setting.
+ * @since 1.6
+ */
+ public CSVFormat withAutoFlush(final boolean autoFlush) {
+ return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
+ skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
+ }
+
+ /**
+ * Returns a new {@code CSVFormat} with the comment start marker of the format set to the specified character.
+ *
+ * Note that the comment start character is only recognized at the start of a line.
+ *
+ * @param commentMarker
+ * the comment start marker
+ * @return A new CSVFormat that is equal to this one but with the specified character as the comment start marker
+ * @throws IllegalArgumentException
+ * thrown if the specified character is a line break
+ */
+ public CSVFormat withCommentMarker(final char commentMarker) {
+ return withCommentMarker(Character.valueOf(commentMarker));
+ }
+
+ /**
+ * Returns a new {@code CSVFormat} with the comment start marker of the format set to the specified character.
+ *
+ * Note that the comment start character is only recognized at the start of a line.
+ *
+ * @param commentMarker
+ * the comment start marker, use {@code null} to disable
+ * @return A new CSVFormat that is equal to this one but with the specified character as the comment start marker
+ * @throws IllegalArgumentException
+ * thrown if the specified character is a line break
+ */
+ public CSVFormat withCommentMarker(final Character commentMarker) {
+ if (isLineBreak(commentMarker)) {
+ throw new IllegalArgumentException("The comment start marker character cannot be a line break");
+ }
+ return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
+ skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
+ }
+
+ /**
+ * Returns a new {@code CSVFormat} with the delimiter of the format set to the specified character.
+ *
+ * @param delimiter
+ * the delimiter character
+ * @return A new CSVFormat that is equal to this with the specified character as delimiter
+ * @throws IllegalArgumentException
+ * thrown if the specified character is a line break
+ */
+ public CSVFormat withDelimiter(final char delimiter) {
+ if (isLineBreak(delimiter)) {
+ throw new IllegalArgumentException("The delimiter cannot be a line break");
+ }
+ return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
+ skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
+ }
+
+ /**
+ * Returns a new {@code CSVFormat} with the escape character of the format set to the specified character.
+ *
+ * @param escape
+ * the escape character
+ * @return A new CSVFormat that is equal to his but with the specified character as the escape character
+ * @throws IllegalArgumentException
+ * thrown if the specified character is a line break
+ */
+ public CSVFormat withEscape(final char escape) {
+ return withEscape(Character.valueOf(escape));
+ }
+
+ /**
+ * Returns a new {@code CSVFormat} with the escape character of the format set to the specified character.
+ *
+ * @param escape
+ * the escape character, use {@code null} to disable
+ * @return A new CSVFormat that is equal to this but with the specified character as the escape character
+ * @throws IllegalArgumentException
+ * thrown if the specified character is a line break
+ */
+ public CSVFormat withEscape(final Character escape) {
+ if (isLineBreak(escape)) {
+ throw new IllegalArgumentException("The escape character cannot be a line break");
+ }
+ return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escape, ignoreSurroundingSpaces,
+ ignoreEmptyLines, recordSeparator, nullString, headerComments, header, skipHeaderRecord,
+ allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
+ }
+
+ /**
+ * Returns a new {@code CSVFormat} using the first record as header.
+ *
+ * <p>
+ * Calling this method is equivalent to calling:
+ * </p>
+ *
+ * <pre>
+ * CSVFormat format = aFormat.withHeader().withSkipHeaderRecord();
+ * </pre>
+ *
+ * @return A new CSVFormat that is equal to this but using the first record as header.
+ * @see #withSkipHeaderRecord(boolean)
+ * @see #withHeader(String...)
+ * @since 1.3
+ */
+ public CSVFormat withFirstRecordAsHeader() {
+ return withHeader().withSkipHeaderRecord();
+ }
+
+ /**
+ * Returns a new {@code CSVFormat} with the header of the format defined by the enum class.
+ *
+ * <p>
+ * Example:
+ * </p>
+ * <pre>
+ * public enum Header {
+ * Name, Email, Phone
+ * }
+ *
+ * CSVFormat format = aformat.withHeader(Header.class);
+ * </pre>
+ * <p>
+ * The header is also used by the {@link CSVPrinter}.
+ * </p>
+ *
+ * @param headerEnum
+ * the enum defining the header, {@code null} if disabled, empty if parsed automatically, user specified
+ * otherwise.
+ *
+ * @return A new CSVFormat that is equal to this but with the specified header
+ * @see #withHeader(String...)
+ * @see #withSkipHeaderRecord(boolean)
+ * @since 1.3
+ */
+ public CSVFormat withHeader(final Class<? extends Enum<?>> headerEnum) {
+ String[] header = null;
+ if (headerEnum != null) {
+ final Enum<?>[] enumValues = headerEnum.getEnumConstants();
+ header = new String[enumValues.length];
+ for (int i = 0; i < enumValues.length; i++) {
+ header[i] = enumValues[i].name();
+ }
+ }
+ return withHeader(header);
+ }
+
+ /**
+ * Returns a new {@code CSVFormat} with the header of the format set from the result set metadata. The header can
+ * either be parsed automatically from the input file with:
+ *
+ * <pre>
+ * CSVFormat format = aformat.withHeader();
+ * </pre>
+ *
+ * or specified manually with:
+ *
+ * <pre>
+ * CSVFormat format = aformat.withHeader(resultSet);
+ * </pre>
+ * <p>
+ * The header is also used by the {@link CSVPrinter}.
+ * </p>
+ *
+ * @param resultSet
+ * the resultSet for the header, {@code null} if disabled, empty if parsed automatically, user specified
+ * otherwise.
+ *
+ * @return A new CSVFormat that is equal to this but with the specified header
+ * @throws SQLException
+ * SQLException if a database access error occurs or this method is called on a closed result set.
+ * @since 1.1
+ */
+ public CSVFormat withHeader(final ResultSet resultSet) throws SQLException {
+ return withHeader(resultSet != null ? resultSet.getMetaData() : null);
+ }
+
+ /**
+ * Returns a new {@code CSVFormat} with the header of the format set from the result set metadata. The header can
+ * either be parsed automatically from the input file with:
+ *
+ * <pre>
+ * CSVFormat format = aformat.withHeader();
+ * </pre>
+ *
+ * or specified manually with:
+ *
+ * <pre>
+ * CSVFormat format = aformat.withHeader(metaData);
+ * </pre>
+ * <p>
+ * The header is also used by the {@link CSVPrinter}.
+ * </p>
+ *
+ * @param metaData
+ * the metaData for the header, {@code null} if disabled, empty if parsed automatically, user specified
+ * otherwise.
+ *
+ * @return A new CSVFormat that is equal to this but with the specified header
+ * @throws SQLException
+ * SQLException if a database access error occurs or this method is called on a closed result set.
+ * @since 1.1
+ */
+ public CSVFormat withHeader(final ResultSetMetaData metaData) throws SQLException {
+ String[] labels = null;
+ if (metaData != null) {
+ final int columnCount = metaData.getColumnCount();
+ labels = new String[columnCount];
+ for (int i = 0; i < columnCount; i++) {
+ labels[i] = metaData.getColumnLabel(i + 1);
+ }
+ }
+ return withHeader(labels);
+ }
+
+ /**
+ * Returns a new {@code CSVFormat} with the header of the format set to the given values. The header can either be
+ * parsed automatically from the input file with:
+ *
+ * <pre>
+ * CSVFormat format = aformat.withHeader();
+ * </pre>
+ *
+ * or specified manually with:
+ *
+ * <pre>
+ * CSVFormat format = aformat.withHeader("name", "email", "phone");
+ * </pre>
+ * <p>
+ * The header is also used by the {@link CSVPrinter}.
+ * </p>
+ *
+ * @param header
+ * the header, {@code null} if disabled, empty if parsed automatically, user specified otherwise.
+ *
+ * @return A new CSVFormat that is equal to this but with the specified header
+ * @see #withSkipHeaderRecord(boolean)
+ */
+ public CSVFormat withHeader(final String... header) {
+ return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
+ skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
+ }
+
+ /**
+ * Returns a new {@code CSVFormat} with the header comments of the format set to the given values. The comments will
+ * be printed first, before the headers. This setting is ignored by the parser.
+ *
+ * <pre>
+ * CSVFormat format = aformat.withHeaderComments("Generated by Apache Commons CSV 1.1.", new Date());
+ * </pre>
+ *
+ * @param headerComments
+ * the headerComments which will be printed by the Printer before the actual CSV data.
+ *
+ * @return A new CSVFormat that is equal to this but with the specified header
+ * @see #withSkipHeaderRecord(boolean)
+ * @since 1.1
+ */
+ public CSVFormat withHeaderComments(final Object... headerComments) {
+ return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
+ skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
+ }
+
+ /**
+ * Returns a new {@code CSVFormat} with the empty line skipping behavior of the format set to {@code true}.
+ *
+ * @return A new CSVFormat that is equal to this but with the specified empty line skipping behavior.
+ * @since {@link #withIgnoreEmptyLines(boolean)}
+ * @since 1.1
+ */
+ public CSVFormat withIgnoreEmptyLines() {
+ return this.withIgnoreEmptyLines(true);
+ }
+
+ /**
+ * Returns a new {@code CSVFormat} with the empty line skipping behavior of the format set to the given value.
+ *
+ * @param ignoreEmptyLines
+ * the empty line skipping behavior, {@code true} to ignore the empty lines between the records,
+ * {@code false} to translate empty lines to empty records.
+ * @return A new CSVFormat that is equal to this but with the specified empty line skipping behavior.
+ */
+ public CSVFormat withIgnoreEmptyLines(final boolean ignoreEmptyLines) {
+ return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
+ skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
+ }
+
+ /**
+ * Returns a new {@code CSVFormat} with the header ignore case behavior set to {@code true}.
+ *
+ * @return A new CSVFormat that will ignore case header name.
+ * @see #withIgnoreHeaderCase(boolean)
+ * @since 1.3
+ */
+ public CSVFormat withIgnoreHeaderCase() {
+ return this.withIgnoreHeaderCase(true);
+ }
+
+ /**
+ * Returns a new {@code CSVFormat} with whether header names should be accessed ignoring case.
+ *
+ * @param ignoreHeaderCase
+ * the case mapping behavior, {@code true} to access name/values, {@code false} to leave the mapping as
+ * is.
+ * @return A new CSVFormat that will ignore case header name if specified as {@code true}
+ * @since 1.3
+ */
+ public CSVFormat withIgnoreHeaderCase(final boolean ignoreHeaderCase) {
+ return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
+ skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
+ }
+
+ /**
+ * Returns a new {@code CSVFormat} with the trimming behavior of the format set to {@code true}.
+ *
+ * @return A new CSVFormat that is equal to this but with the specified trimming behavior.
+ * @see #withIgnoreSurroundingSpaces(boolean)
+ * @since 1.1
+ */
+ public CSVFormat withIgnoreSurroundingSpaces() {
+ return this.withIgnoreSurroundingSpaces(true);
+ }
+
+ /**
+ * Returns a new {@code CSVFormat} with the trimming behavior of the format set to the given value.
+ *
+ * @param ignoreSurroundingSpaces
+ * the trimming behavior, {@code true} to remove the surrounding spaces, {@code false} to leave the
+ * spaces as is.
+ * @return A new CSVFormat that is equal to this but with the specified trimming behavior.
+ */
+ public CSVFormat withIgnoreSurroundingSpaces(final boolean ignoreSurroundingSpaces) {
+ return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
+ skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
+ }
+
+ /**
+ * Returns a new {@code CSVFormat} with conversions to and from null for strings on input and output.
+ * <ul>
+ * <li><strong>Reading:</strong> Converts strings equal to the given {@code nullString} to {@code null} when reading
+ * records.</li>
+ * <li><strong>Writing:</strong> Writes {@code null} as the given {@code nullString} when writing records.</li>
+ * </ul>
+ *
+ * @param nullString
+ * the String to convert to and from {@code null}. No substitution occurs if {@code null}
+ *
+ * @return A new CSVFormat that is equal to this but with the specified null conversion string.
+ */
+ public CSVFormat withNullString(final String nullString) {
+ return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
+ skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
+ }
+
+ /**
+ * Returns a new {@code CSVFormat} with the quoteChar of the format set to the specified character.
+ *
+ * @param quoteChar
+ * the quoteChar character
+ * @return A new CSVFormat that is equal to this but with the specified character as quoteChar
+ * @throws IllegalArgumentException
+ * thrown if the specified character is a line break
+ */
+ public CSVFormat withQuote(final char quoteChar) {
+ return withQuote(Character.valueOf(quoteChar));
+ }
+
+ /**
+ * Returns a new {@code CSVFormat} with the quoteChar of the format set to the specified character.
+ *
+ * @param quoteChar
+ * the quoteChar character, use {@code null} to disable
+ * @return A new CSVFormat that is equal to this but with the specified character as quoteChar
+ * @throws IllegalArgumentException
+ * thrown if the specified character is a line break
+ */
+ public CSVFormat withQuote(final Character quoteChar) {
+ if (isLineBreak(quoteChar)) {
+ throw new IllegalArgumentException("The quoteChar cannot be a line break");
+ }
+ return new CSVFormat(delimiter, quoteChar, quoteMode, commentMarker, escapeCharacter, ignoreSurroundingSpaces,
+ ignoreEmptyLines, recordSeparator, nullString, headerComments, header, skipHeaderRecord,
+ allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
+ }
+
+ /**
+ * Returns a new {@code CSVFormat} with the output quote policy of the format set to the specified value.
+ *
+ * @param quoteModePolicy
+ * the quote policy to use for output.
+ *
+ * @return A new CSVFormat that is equal to this but with the specified quote policy
+ */
+ public CSVFormat withQuoteMode(final QuoteMode quoteModePolicy) {
+ return new CSVFormat(delimiter, quoteCharacter, quoteModePolicy, commentMarker, escapeCharacter,
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
+ skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
+ }
+
+ /**
+ * Returns a new {@code CSVFormat} with the record separator of the format set to the specified character.
+ *
+ * <p>
+ * <strong>Note:</strong> This setting is only used during printing and does not affect parsing. Parsing currently
+ * only works for inputs with '\n', '\r' and "\r\n"
+ * </p>
+ *
+ * @param recordSeparator
+ * the record separator to use for output.
+ *
+ * @return A new CSVFormat that is equal to this but with the specified output record separator
+ */
+ public CSVFormat withRecordSeparator(final char recordSeparator) {
+ return withRecordSeparator(String.valueOf(recordSeparator));
+ }
+
+ /**
+ * Returns a new {@code CSVFormat} with the record separator of the format set to the specified String.
+ *
+ * <p>
+ * <strong>Note:</strong> This setting is only used during printing and does not affect parsing. Parsing currently
+ * only works for inputs with '\n', '\r' and "\r\n"
+ * </p>
+ *
+ * @param recordSeparator
+ * the record separator to use for output.
+ *
+ * @return A new CSVFormat that is equal to this but with the specified output record separator
+ * @throws IllegalArgumentException
+ * if recordSeparator is none of CR, LF or CRLF
+ */
+ public CSVFormat withRecordSeparator(final String recordSeparator) {
+ return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
+ skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
+ }
+
+ /**
+ * Returns a new {@code CSVFormat} with skipping the header record set to {@code true}.
+ *
+ * @return A new CSVFormat that is equal to this but with the the specified skipHeaderRecord setting.
+ * @see #withSkipHeaderRecord(boolean)
+ * @see #withHeader(String...)
+ * @since 1.1
+ */
+ public CSVFormat withSkipHeaderRecord() {
+ return this.withSkipHeaderRecord(true);
+ }
+
+ /**
+ * Returns a new {@code CSVFormat} with whether to skip the header record.
+ *
+ * @param skipHeaderRecord
+ * whether to skip the header record.
+ *
+ * @return A new CSVFormat that is equal to this but with the the specified skipHeaderRecord setting.
+ * @see #withHeader(String...)
+ */
+ public CSVFormat withSkipHeaderRecord(final boolean skipHeaderRecord) {
+ return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
+ skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
+ }
+
+ /**
+ * Returns a new {@code CSVFormat} with the record separator of the format set to the operating system's line
+ * separator string, typically CR+LF on Windows and LF on Linux.
+ *
+ * <p>
+ * <strong>Note:</strong> This setting is only used during printing and does not affect parsing. Parsing currently
+ * only works for inputs with '\n', '\r' and "\r\n"
+ * </p>
+ *
+ * @return A new CSVFormat that is equal to this but with the operating system's line separator stringr
+ * @since 1.6
+ */
+ public CSVFormat withSystemRecordSeparator() {
+ return withRecordSeparator(System.getProperty("line.separator"));
+ }
+
+ /**
+ * Returns a new {@code CSVFormat} to add a trailing delimiter.
+ *
+ * @return A new CSVFormat that is equal to this but with the trailing delimiter setting.
+ * @since 1.3
+ */
+ public CSVFormat withTrailingDelimiter() {
+ return withTrailingDelimiter(true);
+ }
+
+ /**
+ * Returns a new {@code CSVFormat} with whether to add a trailing delimiter.
+ *
+ * @param trailingDelimiter
+ * whether to add a trailing delimiter.
+ *
+ * @return A new CSVFormat that is equal to this but with the specified trailing delimiter setting.
+ * @since 1.3
+ */
+ public CSVFormat withTrailingDelimiter(final boolean trailingDelimiter) {
+ return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
+ skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
+ }
+
+ /**
+ * Returns a new {@code CSVFormat} to trim leading and trailing blanks.
+ *
+ * @return A new CSVFormat that is equal to this but with the trim setting on.
+ * @since 1.3
+ */
+ public CSVFormat withTrim() {
+ return withTrim(true);
+ }
+
+ /**
+ * Returns a new {@code CSVFormat} with whether to trim leading and trailing blanks.
+ *
+ * @param trim
+ * whether to trim leading and trailing blanks.
+ *
+ * @return A new CSVFormat that is equal to this but with the specified trim setting.
+ * @since 1.3
+ */
+ public CSVFormat withTrim(final boolean trim) {
+ return new CSVFormat(delimiter, quoteCharacter, quoteMode, commentMarker, escapeCharacter,
+ ignoreSurroundingSpaces, ignoreEmptyLines, recordSeparator, nullString, headerComments, header,
+ skipHeaderRecord, allowMissingColumnNames, ignoreHeaderCase, trim, trailingDelimiter, autoFlush);
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org
[4/6] qpid-broker-j git commit: QPID-8103: [Broker-J] Leave
minimalistic implementation of CSV format
Posted by or...@apache.org.
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/8d0e68fc/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVParser.java
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVParser.java b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVParser.java
deleted file mode 100644
index cebf253..0000000
--- a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVParser.java
+++ /dev/null
@@ -1,624 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.qpid.server.management.plugin.csv;
-
-import static org.apache.qpid.server.management.plugin.csv.Token.Type.TOKEN;
-
-import java.io.Closeable;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.Reader;
-import java.io.StringReader;
-import java.net.URL;
-import java.nio.charset.Charset;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Iterator;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.NoSuchElementException;
-import java.util.TreeMap;
-
-/**
- * Parses CSV files according to the specified format.
- *
- * Because CSV appears in many different dialects, the parser supports many formats by allowing the
- * specification of a {@link CSVFormat}.
- *
- * The parser works record wise. It is not possible to go back, once a record has been parsed from the input stream.
- *
- * <h2>Creating instances</h2>
- * <p>
- * There are several static factory methods that can be used to create instances for various types of resources:
- * </p>
- * <ul>
- * <li>{@link #parse(File, Charset, CSVFormat)}</li>
- * <li>{@link #parse(String, CSVFormat)}</li>
- * <li>{@link #parse(URL, Charset, CSVFormat)}</li>
- * </ul>
- * <p>
- * Alternatively parsers can also be created by passing a {@link Reader} directly to the sole constructor.
- *
- * For those who like fluent APIs, parsers can be created using {@link CSVFormat#parse(Reader)} as a shortcut:
- * </p>
- * <pre>
- * for(CSVRecord record : CSVFormat.EXCEL.parse(in)) {
- * ...
- * }
- * </pre>
- *
- * <h2>Parsing record wise</h2>
- * <p>
- * To parse a CSV input from a file, you write:
- * </p>
- *
- * <pre>
- * File csvData = new File("/path/to/csv");
- * CSVParser parser = CSVParser.parse(csvData, CSVFormat.RFC4180);
- * for (CSVRecord csvRecord : parser) {
- * ...
- * }
- * </pre>
- *
- * <p>
- * This will read the parse the contents of the file using the
- * <a href="http://tools.ietf.org/html/rfc4180" target="_blank">RFC 4180</a> format.
- * </p>
- *
- * <p>
- * To parse CSV input in a format like Excel, you write:
- * </p>
- *
- * <pre>
- * CSVParser parser = CSVParser.parse(csvData, CSVFormat.EXCEL);
- * for (CSVRecord csvRecord : parser) {
- * ...
- * }
- * </pre>
- *
- * <p>
- * If the predefined formats don't match the format at hands, custom formats can be defined. More information about
- * customising CSVFormats is available in {@link CSVFormat CSVFormat JavaDoc}.
- * </p>
- *
- * <h2>Parsing into memory</h2>
- * <p>
- * If parsing record wise is not desired, the contents of the input can be read completely into memory.
- * </p>
- *
- * <pre>
- * Reader in = new StringReader("a;b\nc;d");
- * CSVParser parser = new CSVParser(in, CSVFormat.EXCEL);
- * List<CSVRecord> list = parser.getRecords();
- * </pre>
- *
- * <p>
- * There are two constraints that have to be kept in mind:
- * </p>
- *
- * <ol>
- * <li>Parsing into memory starts at the current position of the parser. If you have already parsed records from
- * the input, those records will not end up in the in memory representation of your CSV data.</li>
- * <li>Parsing into memory may consume a lot of system resources depending on the input. For example if you're
- * parsing a 150MB file of CSV data the contents will be read completely into memory.</li>
- * </ol>
- *
- * <h2>Notes</h2>
- * <p>
- * Internal parser state is completely covered by the format and the reader-state.
- * </p>
- *
- * @see <a href="package-summary.html">package documentation for more details</a>
- */
-public final class CSVParser implements Iterable<CSVRecord>, Closeable {
-
- /**
- * Creates a parser for the given {@link File}.
- *
- * @param file
- * a CSV file. Must not be null.
- * @param charset
- * A Charset
- * @param format
- * the CSVFormat used for CSV parsing. Must not be null.
- * @return a new parser
- * @throws IllegalArgumentException
- * If the parameters of the format are inconsistent or if either file or format are null.
- * @throws IOException
- * If an I/O error occurs
- */
- @SuppressWarnings("resource")
- public static CSVParser parse(final File file, final Charset charset, final CSVFormat format) throws IOException {
- Assertions.notNull(file, "file");
- Assertions.notNull(format, "format");
- return new CSVParser(new InputStreamReader(new FileInputStream(file), charset), format);
- }
-
- /**
- * Creates a CSV parser using the given {@link CSVFormat}.
- *
- * <p>
- * If you do not read all records from the given {@code reader}, you should call {@link #close()} on the parser,
- * unless you close the {@code reader}.
- * </p>
- *
- * @param inputStream
- * an InputStream containing CSV-formatted input. Must not be null.
- * @param charset
- * a Charset.
- * @param format
- * the CSVFormat used for CSV parsing. Must not be null.
- * @return a new CSVParser configured with the given reader and format.
- * @throws IllegalArgumentException
- * If the parameters of the format are inconsistent or if either reader or format are null.
- * @throws IOException
- * If there is a problem reading the header or skipping the first record
- * @since 1.5
- */
- @SuppressWarnings("resource")
- public static CSVParser parse(final InputStream inputStream, final Charset charset, final CSVFormat format)
- throws IOException {
- Assertions.notNull(inputStream, "inputStream");
- Assertions.notNull(format, "format");
- return parse(new InputStreamReader(inputStream, charset), format);
- }
-
- /**
- * Creates a parser for the given {@link Path}.
- *
- * @param path
- * a CSV file. Must not be null.
- * @param charset
- * A Charset
- * @param format
- * the CSVFormat used for CSV parsing. Must not be null.
- * @return a new parser
- * @throws IllegalArgumentException
- * If the parameters of the format are inconsistent or if either file or format are null.
- * @throws IOException
- * If an I/O error occurs
- * @since 1.5
- */
- public static CSVParser parse(final Path path, final Charset charset, final CSVFormat format) throws IOException {
- Assertions.notNull(path, "path");
- Assertions.notNull(format, "format");
- return parse(Files.newBufferedReader(path, charset), format);
- }
-
- /**
- * Creates a CSV parser using the given {@link CSVFormat}
- *
- * <p>
- * If you do not read all records from the given {@code reader}, you should call {@link #close()} on the parser,
- * unless you close the {@code reader}.
- * </p>
- *
- * @param reader
- * a Reader containing CSV-formatted input. Must not be null.
- * @param format
- * the CSVFormat used for CSV parsing. Must not be null.
- * @return a new CSVParser configured with the given reader and format.
- * @throws IllegalArgumentException
- * If the parameters of the format are inconsistent or if either reader or format are null.
- * @throws IOException
- * If there is a problem reading the header or skipping the first record
- * @since 1.5
- */
- public static CSVParser parse(final Reader reader, final CSVFormat format) throws IOException {
- return new CSVParser(reader, format);
- }
-
- /**
- * Creates a parser for the given {@link String}.
- *
- * @param string
- * a CSV string. Must not be null.
- * @param format
- * the CSVFormat used for CSV parsing. Must not be null.
- * @return a new parser
- * @throws IllegalArgumentException
- * If the parameters of the format are inconsistent or if either string or format are null.
- * @throws IOException
- * If an I/O error occurs
- */
- public static CSVParser parse(final String string, final CSVFormat format) throws IOException {
- Assertions.notNull(string, "string");
- Assertions.notNull(format, "format");
-
- return new CSVParser(new StringReader(string), format);
- }
-
- /**
- * Creates a parser for the given URL.
- *
- * <p>
- * If you do not read all records from the given {@code url}, you should call {@link #close()} on the parser, unless
- * you close the {@code url}.
- * </p>
- *
- * @param url
- * a URL. Must not be null.
- * @param charset
- * the charset for the resource. Must not be null.
- * @param format
- * the CSVFormat used for CSV parsing. Must not be null.
- * @return a new parser
- * @throws IllegalArgumentException
- * If the parameters of the format are inconsistent or if either url, charset or format are null.
- * @throws IOException
- * If an I/O error occurs
- */
- public static CSVParser parse(final URL url, final Charset charset, final CSVFormat format) throws IOException {
- Assertions.notNull(url, "url");
- Assertions.notNull(charset, "charset");
- Assertions.notNull(format, "format");
-
- return new CSVParser(new InputStreamReader(url.openStream(), charset), format);
- }
-
- // the following objects are shared to reduce garbage
-
- private final CSVFormat format;
-
- /** A mapping of column names to column indices */
- private final Map<String, Integer> headerMap;
-
- private final Lexer lexer;
-
- /** A record buffer for getRecord(). Grows as necessary and is reused. */
- private final List<String> recordList = new ArrayList<>();
-
- /**
- * The next record number to assign.
- */
- private long recordNumber;
-
- /**
- * Lexer offset when the parser does not start parsing at the beginning of the source. Usually used in combination
- * with {@link #recordNumber}.
- */
- private final long characterOffset;
-
- private final Token reusableToken = new Token();
-
- /**
- * Customized CSV parser using the given {@link CSVFormat}
- *
- * <p>
- * If you do not read all records from the given {@code reader}, you should call {@link #close()} on the parser,
- * unless you close the {@code reader}.
- * </p>
- *
- * @param reader
- * a Reader containing CSV-formatted input. Must not be null.
- * @param format
- * the CSVFormat used for CSV parsing. Must not be null.
- * @throws IllegalArgumentException
- * If the parameters of the format are inconsistent or if either reader or format are null.
- * @throws IOException
- * If there is a problem reading the header or skipping the first record
- */
- public CSVParser(final Reader reader, final CSVFormat format) throws IOException {
- this(reader, format, 0, 1);
- }
-
- /**
- * Customized CSV parser using the given {@link CSVFormat}
- *
- * <p>
- * If you do not read all records from the given {@code reader}, you should call {@link #close()} on the parser,
- * unless you close the {@code reader}.
- * </p>
- *
- * @param reader
- * a Reader containing CSV-formatted input. Must not be null.
- * @param format
- * the CSVFormat used for CSV parsing. Must not be null.
- * @param characterOffset
- * Lexer offset when the parser does not start parsing at the beginning of the source.
- * @param recordNumber
- * The next record number to assign
- * @throws IllegalArgumentException
- * If the parameters of the format are inconsistent or if either reader or format are null.
- * @throws IOException
- * If there is a problem reading the header or skipping the first record
- * @since 1.1
- */
- @SuppressWarnings("resource")
- public CSVParser(final Reader reader, final CSVFormat format, final long characterOffset, final long recordNumber)
- throws IOException {
- Assertions.notNull(reader, "reader");
- Assertions.notNull(format, "format");
-
- this.format = format;
- this.lexer = new Lexer(format, new ExtendedBufferedReader(reader));
- this.headerMap = this.initializeHeader();
- this.characterOffset = characterOffset;
- this.recordNumber = recordNumber - 1;
- }
-
- private void addRecordValue(final boolean lastRecord) {
- final String input = this.reusableToken.content.toString();
- final String inputClean = this.format.getTrim() ? input.trim() : input;
- if (lastRecord && inputClean.isEmpty() && this.format.getTrailingDelimiter()) {
- return;
- }
- final String nullString = this.format.getNullString();
- this.recordList.add(inputClean.equals(nullString) ? null : inputClean);
- }
-
- /**
- * Closes resources.
- *
- * @throws IOException
- * If an I/O error occurs
- */
- @Override
- public void close() throws IOException {
- if (this.lexer != null) {
- this.lexer.close();
- }
- }
-
- /**
- * Returns the current line number in the input stream.
- *
- * <p>
- * <strong>ATTENTION:</strong> If your CSV input has multi-line values, the returned number does not correspond to
- * the record number.
- * </p>
- *
- * @return current line number
- */
- public long getCurrentLineNumber() {
- return this.lexer.getCurrentLineNumber();
- }
-
- /**
- * Gets the first end-of-line string encountered.
- *
- * @return the first end-of-line string
- * @since 1.5
- */
- public String getFirstEndOfLine() {
- return lexer.getFirstEol();
- }
-
- /**
- * Returns a copy of the header map that iterates in column order.
- * <p>
- * The map keys are column names. The map values are 0-based indices.
- * </p>
- * @return a copy of the header map that iterates in column order.
- */
- public Map<String, Integer> getHeaderMap() {
- return this.headerMap == null ? null : new LinkedHashMap<>(this.headerMap);
- }
-
- /**
- * Returns the current record number in the input stream.
- *
- * <p>
- * <strong>ATTENTION:</strong> If your CSV input has multi-line values, the returned number does not correspond to
- * the line number.
- * </p>
- *
- * @return current record number
- */
- public long getRecordNumber() {
- return this.recordNumber;
- }
-
- /**
- * Parses the CSV input according to the given format and returns the content as a list of
- * {@link CSVRecord CSVRecords}.
- *
- * <p>
- * The returned content starts at the current parse-position in the stream.
- * </p>
- *
- * @return list of {@link CSVRecord CSVRecords}, may be empty
- * @throws IOException
- * on parse error or input read-failure
- */
- public List<CSVRecord> getRecords() throws IOException {
- CSVRecord rec;
- final List<CSVRecord> records = new ArrayList<>();
- while ((rec = this.nextRecord()) != null) {
- records.add(rec);
- }
- return records;
- }
-
- /**
- * Initializes the name to index mapping if the format defines a header.
- *
- * @return null if the format has no header.
- * @throws IOException if there is a problem reading the header or skipping the first record
- */
- private Map<String, Integer> initializeHeader() throws IOException {
- Map<String, Integer> hdrMap = null;
- final String[] formatHeader = this.format.getHeader();
- if (formatHeader != null) {
- hdrMap = this.format.getIgnoreHeaderCase() ?
- new TreeMap<String, Integer>(String.CASE_INSENSITIVE_ORDER) :
- new LinkedHashMap<String, Integer>();
-
- String[] headerRecord = null;
- if (formatHeader.length == 0) {
- // read the header from the first line of the file
- final CSVRecord nextRecord = this.nextRecord();
- if (nextRecord != null) {
- headerRecord = nextRecord.values();
- }
- } else {
- if (this.format.getSkipHeaderRecord()) {
- this.nextRecord();
- }
- headerRecord = formatHeader;
- }
-
- // build the name to index mappings
- if (headerRecord != null) {
- for (int i = 0; i < headerRecord.length; i++) {
- final String header = headerRecord[i];
- final boolean containsHeader = hdrMap.containsKey(header);
- final boolean emptyHeader = header == null || header.trim().isEmpty();
- if (containsHeader && (!emptyHeader || !this.format.getAllowMissingColumnNames())) {
- throw new IllegalArgumentException("The header contains a duplicate name: \"" + header +
- "\" in " + Arrays.toString(headerRecord));
- }
- hdrMap.put(header, Integer.valueOf(i));
- }
- }
- }
- return hdrMap;
- }
-
- /**
- * Gets whether this parser is closed.
- *
- * @return whether this parser is closed.
- */
- public boolean isClosed() {
- return this.lexer.isClosed();
- }
-
- /**
- * Returns an iterator on the records.
- *
- * <p>
- * An {@link IOException} caught during the iteration are re-thrown as an
- * {@link IllegalStateException}.
- * </p>
- * <p>
- * If the parser is closed a call to {@link Iterator#next()} will throw a
- * {@link NoSuchElementException}.
- * </p>
- */
- @Override
- public Iterator<CSVRecord> iterator() {
- return new Iterator<CSVRecord>() {
- private CSVRecord current;
-
- private CSVRecord getNextRecord() {
- try {
- return CSVParser.this.nextRecord();
- } catch (final IOException e) {
- throw new IllegalStateException(
- e.getClass().getSimpleName() + " reading next record: " + e.toString(), e);
- }
- }
-
- @Override
- public boolean hasNext() {
- if (CSVParser.this.isClosed()) {
- return false;
- }
- if (this.current == null) {
- this.current = this.getNextRecord();
- }
-
- return this.current != null;
- }
-
- @Override
- public CSVRecord next() {
- if (CSVParser.this.isClosed()) {
- throw new NoSuchElementException("CSVParser has been closed");
- }
- CSVRecord next = this.current;
- this.current = null;
-
- if (next == null) {
- // hasNext() wasn't called before
- next = this.getNextRecord();
- if (next == null) {
- throw new NoSuchElementException("No more CSV records available");
- }
- }
-
- return next;
- }
-
- @Override
- public void remove() {
- throw new UnsupportedOperationException();
- }
- };
- }
-
- /**
- * Parses the next record from the current point in the stream.
- *
- * @return the record as an array of values, or {@code null} if the end of the stream has been reached
- * @throws IOException
- * on parse error or input read-failure
- */
- CSVRecord nextRecord() throws IOException {
- CSVRecord result = null;
- this.recordList.clear();
- StringBuilder sb = null;
- final long startCharPosition = lexer.getCharacterPosition() + this.characterOffset;
- do {
- this.reusableToken.reset();
- this.lexer.nextToken(this.reusableToken);
- switch (this.reusableToken.type) {
- case TOKEN:
- this.addRecordValue(false);
- break;
- case EORECORD:
- this.addRecordValue(true);
- break;
- case EOF:
- if (this.reusableToken.isReady) {
- this.addRecordValue(true);
- }
- break;
- case INVALID:
- throw new IOException("(line " + this.getCurrentLineNumber() + ") invalid parse sequence");
- case COMMENT: // Ignored currently
- if (sb == null) { // first comment for this record
- sb = new StringBuilder();
- } else {
- sb.append(Constants.LF);
- }
- sb.append(this.reusableToken.content);
- this.reusableToken.type = TOKEN; // Read another token
- break;
- default:
- throw new IllegalStateException("Unexpected Token type: " + this.reusableToken.type);
- }
- } while (this.reusableToken.type == TOKEN);
-
- if (!this.recordList.isEmpty()) {
- this.recordNumber++;
- final String comment = sb == null ? null : sb.toString();
- result = new CSVRecord(this.recordList.toArray(new String[this.recordList.size()]), this.headerMap, comment,
- this.recordNumber, startCharPosition);
- }
- return result;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/8d0e68fc/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVPrinter.java
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVPrinter.java b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVPrinter.java
deleted file mode 100644
index 494e445..0000000
--- a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVPrinter.java
+++ /dev/null
@@ -1,356 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.qpid.server.management.plugin.csv;
-
-import static org.apache.qpid.server.management.plugin.csv.Constants.CR;
-import static org.apache.qpid.server.management.plugin.csv.Constants.LF;
-import static org.apache.qpid.server.management.plugin.csv.Constants.SP;
-
-import java.io.Closeable;
-import java.io.Flushable;
-import java.io.IOException;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-
-/**
- * Prints values in a CSV format.
- */
-public final class CSVPrinter implements Flushable, Closeable {
-
- /** The place that the values get written. */
- private final Appendable out;
- private final CSVFormat format;
-
- /** True if we just began a new record. */
- private boolean newRecord = true;
-
- /**
- * Creates a printer that will print values to the given stream following the CSVFormat.
- * <p>
- * Currently, only a pure encapsulation format or a pure escaping format is supported. Hybrid formats (encapsulation
- * and escaping with a different character) are not supported.
- * </p>
- *
- * @param out
- * stream to which to print. Must not be null.
- * @param format
- * the CSV format. Must not be null.
- * @throws IOException
- * thrown if the optional header cannot be printed.
- * @throws IllegalArgumentException
- * thrown if the parameters of the format are inconsistent or if either out or format are null.
- */
- public CSVPrinter(final Appendable out, final CSVFormat format) throws IOException {
- Assertions.notNull(out, "out");
- Assertions.notNull(format, "format");
-
- this.out = out;
- this.format = format;
- // TODO: Is it a good idea to do this here instead of on the first call to a print method?
- // It seems a pain to have to track whether the header has already been printed or not.
- if (format.getHeaderComments() != null) {
- for (final String line : format.getHeaderComments()) {
- if (line != null) {
- this.printComment(line);
- }
- }
- }
- if (format.getHeader() != null && !format.getSkipHeaderRecord()) {
- this.printRecord((Object[]) format.getHeader());
- }
- }
-
- // ======================================================
- // printing implementation
- // ======================================================
-
- @Override
- public void close() throws IOException {
- close(false);
- }
-
- /**
- * Closes the underlying stream with an optional flush first.
- * @param flush whether to flush before the actual close.
- *
- * @throws IOException
- * If an I/O error occurs
- * @since 1.6
- */
- public void close(final boolean flush) throws IOException {
- if (flush || format.getAutoFlush()) {
- if (out instanceof Flushable) {
- ((Flushable) out).flush();
- }
- }
- if (out instanceof Closeable) {
- ((Closeable) out).close();
- }
- }
-
- /**
- * Flushes the underlying stream.
- *
- * @throws IOException
- * If an I/O error occurs
- */
- @Override
- public void flush() throws IOException {
- if (out instanceof Flushable) {
- ((Flushable) out).flush();
- }
- }
-
- /**
- * Gets the target Appendable.
- *
- * @return the target Appendable.
- */
- public Appendable getOut() {
- return this.out;
- }
-
- /**
- * Prints the string as the next value on the line. The value will be escaped or encapsulated as needed.
- *
- * @param value
- * value to be output.
- * @throws IOException
- * If an I/O error occurs
- */
- public void print(final Object value) throws IOException {
- format.print(value, out, newRecord);
- newRecord = false;
- }
-
- /**
- * Prints a comment on a new line among the delimiter separated values.
- *
- * <p>
- * Comments will always begin on a new line and occupy a least one full line. The character specified to start
- * comments and a space will be inserted at the beginning of each new line in the comment.
- * </p>
- *
- * If comments are disabled in the current CSV format this method does nothing.
- *
- * @param comment
- * the comment to output
- * @throws IOException
- * If an I/O error occurs
- */
- public void printComment(final String comment) throws IOException {
- if (!format.isCommentMarkerSet()) {
- return;
- }
- if (!newRecord) {
- println();
- }
- out.append(format.getCommentMarker().charValue());
- out.append(SP);
- for (int i = 0; i < comment.length(); i++) {
- final char c = comment.charAt(i);
- switch (c) {
- case CR:
- if (i + 1 < comment.length() && comment.charAt(i + 1) == LF) {
- i++;
- }
- //$FALL-THROUGH$ break intentionally excluded.
- case LF:
- println();
- out.append(format.getCommentMarker().charValue());
- out.append(SP);
- break;
- default:
- out.append(c);
- break;
- }
- }
- println();
- }
-
- /**
- * Outputs the record separator.
- *
- * @throws IOException
- * If an I/O error occurs
- */
- public void println() throws IOException {
- format.println(out);
- newRecord = true;
- }
-
- /**
- * Prints the given values a single record of delimiter separated values followed by the record separator.
- *
- * <p>
- * The values will be quoted if needed. Quotes and newLine characters will be escaped. This method adds the record
- * separator to the output after printing the record, so there is no need to call {@link #println()}.
- * </p>
- *
- * @param values
- * values to output.
- * @throws IOException
- * If an I/O error occurs
- */
- public void printRecord(final Iterable<?> values) throws IOException {
- for (final Object value : values) {
- print(value);
- }
- println();
- }
-
- /**
- * Prints the given values a single record of delimiter separated values followed by the record separator.
- *
- * <p>
- * The values will be quoted if needed. Quotes and newLine characters will be escaped. This method adds the record
- * separator to the output after printing the record, so there is no need to call {@link #println()}.
- * </p>
- *
- * @param values
- * values to output.
- * @throws IOException
- * If an I/O error occurs
- */
- public void printRecord(final Object... values) throws IOException {
- format.printRecord(out, values);
- newRecord = true;
- }
-
- /**
- * Prints all the objects in the given collection handling nested collections/arrays as records.
- *
- * <p>
- * If the given collection only contains simple objects, this method will print a single record like
- * {@link #printRecord(Iterable)}. If the given collections contains nested collections/arrays those nested elements
- * will each be printed as records using {@link #printRecord(Object...)}.
- * </p>
- *
- * <p>
- * Given the following data structure:
- * </p>
- *
- * <pre>
- * <code>
- * List<String[]> data = ...
- * data.add(new String[]{ "A", "B", "C" });
- * data.add(new String[]{ "1", "2", "3" });
- * data.add(new String[]{ "A1", "B2", "C3" });
- * </code>
- * </pre>
- *
- * <p>
- * Calling this method will print:
- * </p>
- *
- * <pre>
- * <code>
- * A, B, C
- * 1, 2, 3
- * A1, B2, C3
- * </code>
- * </pre>
- *
- * @param values
- * the values to print.
- * @throws IOException
- * If an I/O error occurs
- */
- public void printRecords(final Iterable<?> values) throws IOException {
- for (final Object value : values) {
- if (value instanceof Object[]) {
- this.printRecord((Object[]) value);
- } else if (value instanceof Iterable) {
- this.printRecord((Iterable<?>) value);
- } else {
- this.printRecord(value);
- }
- }
- }
-
- /**
- * Prints all the objects in the given array handling nested collections/arrays as records.
- *
- * <p>
- * If the given array only contains simple objects, this method will print a single record like
- * {@link #printRecord(Object...)}. If the given collections contains nested collections/arrays those nested
- * elements will each be printed as records using {@link #printRecord(Object...)}.
- * </p>
- *
- * <p>
- * Given the following data structure:
- * </p>
- *
- * <pre>
- * <code>
- * String[][] data = new String[3][]
- * data[0] = String[]{ "A", "B", "C" };
- * data[1] = new String[]{ "1", "2", "3" };
- * data[2] = new String[]{ "A1", "B2", "C3" };
- * </code>
- * </pre>
- *
- * <p>
- * Calling this method will print:
- * </p>
- *
- * <pre>
- * <code>
- * A, B, C
- * 1, 2, 3
- * A1, B2, C3
- * </code>
- * </pre>
- *
- * @param values
- * the values to print.
- * @throws IOException
- * If an I/O error occurs
- */
- public void printRecords(final Object... values) throws IOException {
- for (final Object value : values) {
- if (value instanceof Object[]) {
- this.printRecord((Object[]) value);
- } else if (value instanceof Iterable) {
- this.printRecord((Iterable<?>) value);
- } else {
- this.printRecord(value);
- }
- }
- }
-
- /**
- * Prints all the objects in the given JDBC result set.
- *
- * @param resultSet
- * result set the values to print.
- * @throws IOException
- * If an I/O error occurs
- * @throws SQLException
- * if a database access error occurs
- */
- public void printRecords(final ResultSet resultSet) throws SQLException, IOException {
- final int columnCount = resultSet.getMetaData().getColumnCount();
- while (resultSet.next()) {
- for (int i = 1; i <= columnCount; i++) {
- print(resultSet.getObject(i));
- }
- println();
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/8d0e68fc/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVRecord.java
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVRecord.java b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVRecord.java
deleted file mode 100644
index e36bfbb..0000000
--- a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/CSVRecord.java
+++ /dev/null
@@ -1,276 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.qpid.server.management.plugin.csv;
-
-import java.io.Serializable;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-
-/**
- * A CSV record parsed from a CSV file.
- */
-public final class CSVRecord implements Serializable, Iterable<String> {
-
- private static final String[] EMPTY_STRING_ARRAY = new String[0];
-
- private static final long serialVersionUID = 1L;
-
- private final long characterPosition;
-
- /** The accumulated comments (if any) */
- private final String comment;
-
- /** The column name to index mapping. */
- private final Map<String, Integer> mapping;
-
- /** The record number. */
- private final long recordNumber;
-
- /** The values of the record */
- private final String[] values;
-
- CSVRecord(final String[] values, final Map<String, Integer> mapping, final String comment, final long recordNumber,
- final long characterPosition) {
- this.recordNumber = recordNumber;
- this.values = values != null ? values : EMPTY_STRING_ARRAY;
- this.mapping = mapping;
- this.comment = comment;
- this.characterPosition = characterPosition;
- }
-
- /**
- * Returns a value by {@link Enum}.
- *
- * @param e
- * an enum
- * @return the String at the given enum String
- */
- public String get(final Enum<?> e) {
- return get(e.toString());
- }
-
- /**
- * Returns a value by index.
- *
- * @param i
- * a column index (0-based)
- * @return the String at the given index
- */
- public String get(final int i) {
- return values[i];
- }
-
- /**
- * Returns a value by name.
- *
- * @param name
- * the name of the column to be retrieved.
- * @return the column value, maybe null depending on {@link CSVFormat#getNullString()}.
- * @throws IllegalStateException
- * if no header mapping was provided
- * @throws IllegalArgumentException
- * if {@code name} is not mapped or if the record is inconsistent
- * @see #isConsistent()
- * @see CSVFormat#withNullString(String)
- */
- public String get(final String name) {
- if (mapping == null) {
- throw new IllegalStateException(
- "No header mapping was specified, the record values can't be accessed by name");
- }
- final Integer index = mapping.get(name);
- if (index == null) {
- throw new IllegalArgumentException(String.format("Mapping for %s not found, expected one of %s", name,
- mapping.keySet()));
- }
- try {
- return values[index.intValue()];
- } catch (final ArrayIndexOutOfBoundsException e) {
- throw new IllegalArgumentException(String.format(
- "Index for header '%s' is %d but CSVRecord only has %d values!", name, index,
- Integer.valueOf(values.length)));
- }
- }
-
- /**
- * Returns the start position of this record as a character position in the source stream. This may or may not
- * correspond to the byte position depending on the character set.
- *
- * @return the position of this record in the source stream.
- */
- public long getCharacterPosition() {
- return characterPosition;
- }
-
- /**
- * Returns the comment for this record, if any.
- * Note that comments are attached to the following record.
- * If there is no following record (i.e. the comment is at EOF)
- * the comment will be ignored.
- *
- * @return the comment for this record, or null if no comment for this record is available.
- */
- public String getComment() {
- return comment;
- }
-
- /**
- * Returns the number of this record in the parsed CSV file.
- *
- * <p>
- * <strong>ATTENTION:</strong> If your CSV input has multi-line values, the returned number does not correspond to
- * the current line number of the parser that created this record.
- * </p>
- *
- * @return the number of this record.
- * @see CSVParser#getCurrentLineNumber()
- */
- public long getRecordNumber() {
- return recordNumber;
- }
-
- /**
- * Tells whether the record size matches the header size.
- *
- * <p>
- * Returns true if the sizes for this record match and false if not. Some programs can export files that fail this
- * test but still produce parsable files.
- * </p>
- *
- * @return true of this record is valid, false if not
- */
- public boolean isConsistent() {
- return mapping == null || mapping.size() == values.length;
- }
-
- /**
- * Checks whether this record has a comment, false otherwise.
- * Note that comments are attached to the following record.
- * If there is no following record (i.e. the comment is at EOF)
- * the comment will be ignored.
- *
- * @return true if this record has a comment, false otherwise
- * @since 1.3
- */
- public boolean hasComment() {
- return comment != null;
- }
-
- /**
- * Checks whether a given column is mapped, i.e. its name has been defined to the parser.
- *
- * @param name
- * the name of the column to be retrieved.
- * @return whether a given column is mapped.
- */
- public boolean isMapped(final String name) {
- return mapping != null && mapping.containsKey(name);
- }
-
- /**
- * Checks whether a given columns is mapped and has a value.
- *
- * @param name
- * the name of the column to be retrieved.
- * @return whether a given columns is mapped and has a value
- */
- public boolean isSet(final String name) {
- return isMapped(name) && mapping.get(name).intValue() < values.length;
- }
-
- /**
- * Returns an iterator over the values of this record.
- *
- * @return an iterator over the values of this record.
- */
- @Override
- public Iterator<String> iterator() {
- return toList().iterator();
- }
-
- /**
- * Puts all values of this record into the given Map.
- *
- * @param map
- * The Map to populate.
- * @return the given map.
- */
- <M extends Map<String, String>> M putIn(final M map) {
- if (mapping == null) {
- return map;
- }
- for (final Entry<String, Integer> entry : mapping.entrySet()) {
- final int col = entry.getValue().intValue();
- if (col < values.length) {
- map.put(entry.getKey(), values[col]);
- }
- }
- return map;
- }
-
- /**
- * Returns the number of values in this record.
- *
- * @return the number of values.
- */
- public int size() {
- return values.length;
- }
-
- /**
- * Converts the values to a List.
- *
- * TODO: Maybe make this public?
- *
- * @return a new List
- */
- private List<String> toList() {
- return Arrays.asList(values);
- }
-
- /**
- * Copies this record into a new Map. The new map is not connect
- *
- * @return A new Map. The map is empty if the record has no headers.
- */
- public Map<String, String> toMap() {
- return putIn(new HashMap<String, String>(values.length));
- }
-
- /**
- * Returns a string representation of the contents of this record. The result is constructed by comment, mapping,
- * recordNumber and by passing the internal values array to {@link Arrays#toString(Object[])}.
- *
- * @return a String representation of this record.
- */
- @Override
- public String toString() {
- return "CSVRecord [comment=" + comment + ", mapping=" + mapping +
- ", recordNumber=" + recordNumber + ", values=" +
- Arrays.toString(values) + "]";
- }
-
- String[] values() {
- return values;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/8d0e68fc/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/Constants.java
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/Constants.java b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/Constants.java
deleted file mode 100644
index 37ec6ae..0000000
--- a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/Constants.java
+++ /dev/null
@@ -1,82 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.qpid.server.management.plugin.csv;
-
-/**
- * Constants for this package.
- */
-final class Constants {
-
- static final char BACKSLASH = '\\';
-
- static final char BACKSPACE = '\b';
-
- static final char COMMA = ',';
-
- /**
- * Starts a comment, the remainder of the line is the comment.
- */
- static final char COMMENT = '#';
-
- static final char CR = '\r';
-
- /** RFC 4180 defines line breaks as CRLF */
- static final String CRLF = "\r\n";
-
- static final Character DOUBLE_QUOTE_CHAR = Character.valueOf('"');
-
- static final String EMPTY = "";
-
- /** The end of stream symbol */
- static final int END_OF_STREAM = -1;
-
- static final char FF = '\f';
-
- static final char LF = '\n';
-
- /**
- * Unicode line separator.
- */
- static final String LINE_SEPARATOR = "\u2028";
-
- /**
- * Unicode next line.
- */
- static final String NEXT_LINE = "\u0085";
-
- /**
- * Unicode paragraph separator.
- */
- static final String PARAGRAPH_SEPARATOR = "\u2029";
-
- static final char PIPE = '|';
-
- /** ASCII record separator */
- static final char RS = 30;
-
- static final char SP = ' ';
-
- static final char TAB = '\t';
-
- /** Undefined state for the lookahead char */
- static final int UNDEFINED = -2;
-
- /** ASCII unit separator */
- static final char US = 31;
-
-}
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/8d0e68fc/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/ExtendedBufferedReader.java
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/ExtendedBufferedReader.java b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/ExtendedBufferedReader.java
deleted file mode 100644
index 47f8a2e..0000000
--- a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/ExtendedBufferedReader.java
+++ /dev/null
@@ -1,191 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.qpid.server.management.plugin.csv;
-
-import static org.apache.qpid.server.management.plugin.csv.Constants.CR;
-import static org.apache.qpid.server.management.plugin.csv.Constants.END_OF_STREAM;
-import static org.apache.qpid.server.management.plugin.csv.Constants.LF;
-import static org.apache.qpid.server.management.plugin.csv.Constants.UNDEFINED;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.Reader;
-
-/**
- * A special buffered reader which supports sophisticated read access.
- * <p>
- * In particular the reader supports a look-ahead option, which allows you to see the next char returned by
- * {@link #read()}. This reader also tracks how many characters have been read with {@link #getPosition()}.
- * </p>
- */
-final class ExtendedBufferedReader extends BufferedReader {
-
- /** The last char returned */
- private int lastChar = UNDEFINED;
-
- /** The count of EOLs (CR/LF/CRLF) seen so far */
- private long eolCounter;
-
- /** The position, which is number of characters read so far */
- private long position;
-
- private boolean closed;
-
- /**
- * Created extended buffered reader using default buffer-size
- */
- ExtendedBufferedReader(final Reader reader) {
- super(reader);
- }
-
- @Override
- public int read() throws IOException {
- final int current = super.read();
- if (current == CR || current == LF && lastChar != CR) {
- eolCounter++;
- }
- lastChar = current;
- this.position++;
- return lastChar;
- }
-
- /**
- * Returns the last character that was read as an integer (0 to 65535). This will be the last character returned by
- * any of the read methods. This will not include a character read using the {@link #lookAhead()} method. If no
- * character has been read then this will return {@link Constants#UNDEFINED}. If the end of the stream was reached
- * on the last read then this will return {@link Constants#END_OF_STREAM}.
- *
- * @return the last character that was read
- */
- int getLastChar() {
- return lastChar;
- }
-
- @Override
- public int read(final char[] buf, final int offset, final int length) throws IOException {
- if (length == 0) {
- return 0;
- }
-
- final int len = super.read(buf, offset, length);
-
- if (len > 0) {
-
- for (int i = offset; i < offset + len; i++) {
- final char ch = buf[i];
- if (ch == LF) {
- if (CR != (i > 0 ? buf[i - 1] : lastChar)) {
- eolCounter++;
- }
- } else if (ch == CR) {
- eolCounter++;
- }
- }
-
- lastChar = buf[offset + len - 1];
-
- } else if (len == -1) {
- lastChar = END_OF_STREAM;
- }
-
- position += len;
- return len;
- }
-
- /**
- * Calls {@link BufferedReader#readLine()} which drops the line terminator(s). This method should only be called
- * when processing a comment, otherwise information can be lost.
- * <p>
- * Increments {@link #eolCounter}
- * <p>
- * Sets {@link #lastChar} to {@link Constants#END_OF_STREAM} at EOF, otherwise to LF
- *
- * @return the line that was read, or null if reached EOF.
- */
- @Override
- public String readLine() throws IOException {
- final String line = super.readLine();
-
- if (line != null) {
- lastChar = LF; // needed for detecting start of line
- eolCounter++;
- } else {
- lastChar = END_OF_STREAM;
- }
-
- return line;
- }
-
- /**
- * Returns the next character in the current reader without consuming it. So the next call to {@link #read()} will
- * still return this value. Does not affect line number or last character.
- *
- * @return the next character
- *
- * @throws IOException
- * if there is an error in reading
- */
- int lookAhead() throws IOException {
- super.mark(1);
- final int c = super.read();
- super.reset();
-
- return c;
- }
-
- /**
- * Returns the current line number
- *
- * @return the current line number
- */
- long getCurrentLineNumber() {
- // Check if we are at EOL or EOF or just starting
- if (lastChar == CR || lastChar == LF || lastChar == UNDEFINED || lastChar == END_OF_STREAM) {
- return eolCounter; // counter is accurate
- }
- return eolCounter + 1; // Allow for counter being incremented only at EOL
- }
-
- /**
- * Gets the character position in the reader.
- *
- * @return the current position in the reader (counting characters, not bytes since this is a Reader)
- */
- long getPosition() {
- return this.position;
- }
-
- public boolean isClosed() {
- return closed;
- }
-
- /**
- * Closes the stream.
- *
- * @throws IOException
- * If an I/O error occurs
- */
- @Override
- public void close() throws IOException {
- // Set ivars before calling super close() in case close() throws an IOException.
- closed = true;
- lastChar = END_OF_STREAM;
- super.close();
- }
-
-}
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/8d0e68fc/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/Lexer.java
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/Lexer.java b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/Lexer.java
deleted file mode 100644
index 95a3ff0..0000000
--- a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/Lexer.java
+++ /dev/null
@@ -1,461 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.qpid.server.management.plugin.csv;
-
-import static org.apache.qpid.server.management.plugin.csv.Constants.BACKSPACE;
-import static org.apache.qpid.server.management.plugin.csv.Constants.CR;
-import static org.apache.qpid.server.management.plugin.csv.Constants.END_OF_STREAM;
-import static org.apache.qpid.server.management.plugin.csv.Constants.FF;
-import static org.apache.qpid.server.management.plugin.csv.Constants.LF;
-import static org.apache.qpid.server.management.plugin.csv.Constants.TAB;
-import static org.apache.qpid.server.management.plugin.csv.Constants.UNDEFINED;
-import static org.apache.qpid.server.management.plugin.csv.Token.Type.COMMENT;
-import static org.apache.qpid.server.management.plugin.csv.Token.Type.EOF;
-import static org.apache.qpid.server.management.plugin.csv.Token.Type.EORECORD;
-import static org.apache.qpid.server.management.plugin.csv.Token.Type.INVALID;
-import static org.apache.qpid.server.management.plugin.csv.Token.Type.TOKEN;
-
-import java.io.Closeable;
-import java.io.IOException;
-
-/**
- * Lexical analyzer.
- */
-final class Lexer implements Closeable {
-
- private static final String CR_STRING = Character.toString(Constants.CR);
- private static final String LF_STRING = Character.toString(Constants.LF);
-
- /**
- * Constant char to use for disabling comments, escapes and encapsulation. The value -2 is used because it
- * won't be confused with an EOF signal (-1), and because the Unicode value {@code FFFE} would be encoded as two
- * chars (using surrogates) and thus there should never be a collision with a real text char.
- */
- private static final char DISABLED = '\ufffe';
-
- private final char delimiter;
- private final char escape;
- private final char quoteChar;
- private final char commentStart;
-
- private final boolean ignoreSurroundingSpaces;
- private final boolean ignoreEmptyLines;
-
- /** The input stream */
- private final ExtendedBufferedReader reader;
- private String firstEol;
-
- String getFirstEol(){
- return firstEol;
- }
-
- Lexer(final CSVFormat format, final ExtendedBufferedReader reader) {
- this.reader = reader;
- this.delimiter = format.getDelimiter();
- this.escape = mapNullToDisabled(format.getEscapeCharacter());
- this.quoteChar = mapNullToDisabled(format.getQuoteCharacter());
- this.commentStart = mapNullToDisabled(format.getCommentMarker());
- this.ignoreSurroundingSpaces = format.getIgnoreSurroundingSpaces();
- this.ignoreEmptyLines = format.getIgnoreEmptyLines();
- }
-
- /**
- * Returns the next token.
- * <p>
- * A token corresponds to a term, a record change or an end-of-file indicator.
- * </p>
- *
- * @param token
- * an existing Token object to reuse. The caller is responsible to initialize the Token.
- * @return the next token found
- * @throws IOException
- * on stream access error
- */
- Token nextToken(final Token token) throws IOException {
-
- // get the last read char (required for empty line detection)
- int lastChar = reader.getLastChar();
-
- // read the next char and set eol
- int c = reader.read();
- /*
- * Note: The following call will swallow LF if c == CR. But we don't need to know if the last char was CR or LF
- * - they are equivalent here.
- */
- boolean eol = readEndOfLine(c);
-
- // empty line detection: eol AND (last char was EOL or beginning)
- if (ignoreEmptyLines) {
- while (eol && isStartOfLine(lastChar)) {
- // go on char ahead ...
- lastChar = c;
- c = reader.read();
- eol = readEndOfLine(c);
- // reached end of file without any content (empty line at the end)
- if (isEndOfFile(c)) {
- token.type = EOF;
- // don't set token.isReady here because no content
- return token;
- }
- }
- }
-
- // did we reach eof during the last iteration already ? EOF
- if (isEndOfFile(lastChar) || !isDelimiter(lastChar) && isEndOfFile(c)) {
- token.type = EOF;
- // don't set token.isReady here because no content
- return token;
- }
-
- if (isStartOfLine(lastChar) && isCommentStart(c)) {
- final String line = reader.readLine();
- if (line == null) {
- token.type = EOF;
- // don't set token.isReady here because no content
- return token;
- }
- final String comment = line.trim();
- token.content.append(comment);
- token.type = COMMENT;
- return token;
- }
-
- // important: make sure a new char gets consumed in each iteration
- while (token.type == INVALID) {
- // ignore whitespaces at beginning of a token
- if (ignoreSurroundingSpaces) {
- while (isWhitespace(c) && !eol) {
- c = reader.read();
- eol = readEndOfLine(c);
- }
- }
-
- // ok, start of token reached: encapsulated, or token
- if (isDelimiter(c)) {
- // empty token return TOKEN("")
- token.type = TOKEN;
- } else if (eol) {
- // empty token return EORECORD("")
- // noop: token.content.append("");
- token.type = EORECORD;
- } else if (isQuoteChar(c)) {
- // consume encapsulated token
- parseEncapsulatedToken(token);
- } else if (isEndOfFile(c)) {
- // end of file return EOF()
- // noop: token.content.append("");
- token.type = EOF;
- token.isReady = true; // there is data at EOF
- } else {
- // next token must be a simple token
- // add removed blanks when not ignoring whitespace chars...
- parseSimpleToken(token, c);
- }
- }
- return token;
- }
-
- /**
- * Parses a simple token.
- * <p/>
- * Simple token are tokens which are not surrounded by encapsulators. A simple token might contain escaped
- * delimiters (as \, or \;). The token is finished when one of the following conditions become true:
- * <ul>
- * <li>end of line has been reached (EORECORD)</li>
- * <li>end of stream has been reached (EOF)</li>
- * <li>an unescaped delimiter has been reached (TOKEN)</li>
- * </ul>
- *
- * @param token
- * the current token
- * @param ch
- * the current character
- * @return the filled token
- * @throws IOException
- * on stream access error
- */
- private Token parseSimpleToken(final Token token, int ch) throws IOException {
- // Faster to use while(true)+break than while(token.type == INVALID)
- while (true) {
- if (readEndOfLine(ch)) {
- token.type = EORECORD;
- break;
- } else if (isEndOfFile(ch)) {
- token.type = EOF;
- token.isReady = true; // There is data at EOF
- break;
- } else if (isDelimiter(ch)) {
- token.type = TOKEN;
- break;
- } else if (isEscape(ch)) {
- final int unescaped = readEscape();
- if (unescaped == END_OF_STREAM) { // unexpected char after escape
- token.content.append((char) ch).append((char) reader.getLastChar());
- } else {
- token.content.append((char) unescaped);
- }
- ch = reader.read(); // continue
- } else {
- token.content.append((char) ch);
- ch = reader.read(); // continue
- }
- }
-
- if (ignoreSurroundingSpaces) {
- trimTrailingSpaces(token.content);
- }
-
- return token;
- }
-
- /**
- * Parses an encapsulated token.
- * <p/>
- * Encapsulated tokens are surrounded by the given encapsulating-string. The encapsulator itself might be included
- * in the token using a doubling syntax (as "", '') or using escaping (as in \", \'). Whitespaces before and after
- * an encapsulated token are ignored. The token is finished when one of the following conditions become true:
- * <ul>
- * <li>an unescaped encapsulator has been reached, and is followed by optional whitespace then:</li>
- * <ul>
- * <li>delimiter (TOKEN)</li>
- * <li>end of line (EORECORD)</li>
- * </ul>
- * <li>end of stream has been reached (EOF)</li> </ul>
- *
- * @param token
- * the current token
- * @return a valid token object
- * @throws IOException
- * on invalid state: EOF before closing encapsulator or invalid character before delimiter or EOL
- */
- private Token parseEncapsulatedToken(final Token token) throws IOException {
- // save current line number in case needed for IOE
- final long startLineNumber = getCurrentLineNumber();
- int c;
- while (true) {
- c = reader.read();
-
- if (isEscape(c)) {
- final int unescaped = readEscape();
- if (unescaped == END_OF_STREAM) { // unexpected char after escape
- token.content.append((char) c).append((char) reader.getLastChar());
- } else {
- token.content.append((char) unescaped);
- }
- } else if (isQuoteChar(c)) {
- if (isQuoteChar(reader.lookAhead())) {
- // double or escaped encapsulator -> add single encapsulator to token
- c = reader.read();
- token.content.append((char) c);
- } else {
- // token finish mark (encapsulator) reached: ignore whitespace till delimiter
- while (true) {
- c = reader.read();
- if (isDelimiter(c)) {
- token.type = TOKEN;
- return token;
- } else if (isEndOfFile(c)) {
- token.type = EOF;
- token.isReady = true; // There is data at EOF
- return token;
- } else if (readEndOfLine(c)) {
- token.type = EORECORD;
- return token;
- } else if (!isWhitespace(c)) {
- // error invalid char between token and next delimiter
- throw new IOException("(line " + getCurrentLineNumber() +
- ") invalid char between encapsulated token and delimiter");
- }
- }
- }
- } else if (isEndOfFile(c)) {
- // error condition (end of file before end of token)
- throw new IOException("(startline " + startLineNumber +
- ") EOF reached before encapsulated token finished");
- } else {
- // consume character
- token.content.append((char) c);
- }
- }
- }
-
- private char mapNullToDisabled(final Character c) {
- return c == null ? DISABLED : c.charValue();
- }
-
- /**
- * Returns the current line number
- *
- * @return the current line number
- */
- long getCurrentLineNumber() {
- return reader.getCurrentLineNumber();
- }
-
- /**
- * Returns the current character position
- *
- * @return the current character position
- */
- long getCharacterPosition() {
- return reader.getPosition();
- }
-
- // TODO escape handling needs more work
- /**
- * Handle an escape sequence.
- * The current character must be the escape character.
- * On return, the next character is available by calling {@link ExtendedBufferedReader#getLastChar()}
- * on the input stream.
- *
- * @return the unescaped character (as an int) or {@link Constants#END_OF_STREAM} if char following the escape is
- * invalid.
- * @throws IOException if there is a problem reading the stream or the end of stream is detected:
- * the escape character is not allowed at end of stream
- */
- int readEscape() throws IOException {
- // the escape char has just been read (normally a backslash)
- final int ch = reader.read();
- switch (ch) {
- case 'r':
- return CR;
- case 'n':
- return LF;
- case 't':
- return TAB;
- case 'b':
- return BACKSPACE;
- case 'f':
- return FF;
- case CR:
- case LF:
- case FF: // TODO is this correct?
- case TAB: // TODO is this correct? Do tabs need to be escaped?
- case BACKSPACE: // TODO is this correct?
- return ch;
- case END_OF_STREAM:
- throw new IOException("EOF whilst processing escape sequence");
- default:
- // Now check for meta-characters
- if (isMetaChar(ch)) {
- return ch;
- }
- // indicate unexpected char - available from in.getLastChar()
- return END_OF_STREAM;
- }
- }
-
- void trimTrailingSpaces(final StringBuilder buffer) {
- int length = buffer.length();
- while (length > 0 && Character.isWhitespace(buffer.charAt(length - 1))) {
- length = length - 1;
- }
- if (length != buffer.length()) {
- buffer.setLength(length);
- }
- }
-
- /**
- * Greedily accepts \n, \r and \r\n This checker consumes silently the second control-character...
- *
- * @return true if the given or next character is a line-terminator
- */
- boolean readEndOfLine(int ch) throws IOException {
- // check if we have \r\n...
- if (ch == CR && reader.lookAhead() == LF) {
- // note: does not change ch outside of this method!
- ch = reader.read();
- // Save the EOL state
- if (firstEol == null) {
- this.firstEol = Constants.CRLF;
- }
- }
- // save EOL state here.
- if (firstEol == null) {
- if (ch == LF) {
- this.firstEol = LF_STRING;
- } else if (ch == CR) {
- this.firstEol = CR_STRING;
- }
- }
-
- return ch == LF || ch == CR;
- }
-
- boolean isClosed() {
- return reader.isClosed();
- }
-
- /**
- * @return true if the given char is a whitespace character
- */
- boolean isWhitespace(final int ch) {
- return !isDelimiter(ch) && Character.isWhitespace((char) ch);
- }
-
- /**
- * Checks if the current character represents the start of a line: a CR, LF or is at the start of the file.
- *
- * @param ch the character to check
- * @return true if the character is at the start of a line.
- */
- boolean isStartOfLine(final int ch) {
- return ch == LF || ch == CR || ch == UNDEFINED;
- }
-
- /**
- * @return true if the given character indicates end of file
- */
- boolean isEndOfFile(final int ch) {
- return ch == END_OF_STREAM;
- }
-
- boolean isDelimiter(final int ch) {
- return ch == delimiter;
- }
-
- boolean isEscape(final int ch) {
- return ch == escape;
- }
-
- boolean isQuoteChar(final int ch) {
- return ch == quoteChar;
- }
-
- boolean isCommentStart(final int ch) {
- return ch == commentStart;
- }
-
- private boolean isMetaChar(final int ch) {
- return ch == delimiter ||
- ch == escape ||
- ch == quoteChar ||
- ch == commentStart;
- }
-
- /**
- * Closes resources.
- *
- * @throws IOException
- * If an I/O error occurs
- */
- @Override
- public void close() throws IOException {
- reader.close();
- }
-}
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/8d0e68fc/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/QuoteMode.java
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/QuoteMode.java b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/QuoteMode.java
deleted file mode 100644
index 25c6b31..0000000
--- a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/QuoteMode.java
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.qpid.server.management.plugin.csv;
-
-/**
- * Defines quoting behavior when printing.
- */
-public enum QuoteMode {
-
- /**
- * Quotes all fields.
- */
- ALL,
-
- /**
- * Quotes all non-null fields.
- */
- ALL_NON_NULL,
-
- /**
- * Quotes fields which contain special characters such as a the field delimiter, quote character or any of the
- * characters in the line separator string.
- */
- MINIMAL,
-
- /**
- * Quotes all non-numeric fields.
- */
- NON_NUMERIC,
-
- /**
- * Never quotes fields. When the delimiter occurs in data, the printer prefixes it with the escape character. If the
- * escape character is not set, format validation throws an exception.
- */
- NONE
-}
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/8d0e68fc/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/Token.java
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/Token.java b/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/Token.java
deleted file mode 100644
index c3c94e1..0000000
--- a/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/csv/Token.java
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.qpid.server.management.plugin.csv;
-
-import static org.apache.qpid.server.management.plugin.csv.Token.Type.INVALID;
-
-/**
- * Internal token representation.
- * <p/>
- * It is used as contract between the lexer and the parser.
- */
-final class Token {
-
- /** length of the initial token (content-)buffer */
- private static final int INITIAL_TOKEN_LENGTH = 50;
-
- enum Type {
- /** Token has no valid content, i.e. is in its initialized state. */
- INVALID,
-
- /** Token with content, at beginning or in the middle of a line. */
- TOKEN,
-
- /** Token (which can have content) when the end of file is reached. */
- EOF,
-
- /** Token with content when the end of a line is reached. */
- EORECORD,
-
- /** Token is a comment line. */
- COMMENT
- }
-
- /** Token type */
- Token.Type type = INVALID;
-
- /** The content buffer. */
- final StringBuilder content = new StringBuilder(INITIAL_TOKEN_LENGTH);
-
- /** Token ready flag: indicates a valid token with content (ready for the parser). */
- boolean isReady;
-
- void reset() {
- content.setLength(0);
- type = INVALID;
- isReady = false;
- }
-
- /**
- * Eases IDE debugging.
- *
- * @return a string helpful for debugging.
- */
- @Override
- public String toString() {
- return type.name() + " [" + content.toString() + "]";
- }
-}
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/8d0e68fc/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/csv/CSVFormatTest.java
----------------------------------------------------------------------
diff --git a/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/csv/CSVFormatTest.java b/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/csv/CSVFormatTest.java
new file mode 100644
index 0000000..4e08315
--- /dev/null
+++ b/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/csv/CSVFormatTest.java
@@ -0,0 +1,84 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.qpid.server.management.plugin.csv;
+
+import java.io.StringWriter;
+import java.util.Arrays;
+
+import org.apache.qpid.test.utils.QpidTestCase;
+
+public class CSVFormatTest extends QpidTestCase
+{
+
+ public void testPrintRecord() throws Exception
+ {
+ CSVFormat csvFormat = new CSVFormat();
+ final StringWriter out = new StringWriter();
+ csvFormat.printRecord(out, Arrays.asList("test", 1, true, "\"quoted\" test"));
+ assertEquals("Unexpected format",
+ String.format("%s,%d,%b,%s%s", "test", 1, true, "\"\"\"quoted\"\" test\"", "\r\n"),
+ out.toString());
+ }
+
+ public void testPrintRecords() throws Exception
+ {
+ CSVFormat csvFormat = new CSVFormat();
+ final StringWriter out = new StringWriter();
+ csvFormat.printRecords(out, Arrays.asList(Arrays.asList("test", 1, true, "\"quoted\" test"),
+ Arrays.asList("delimeter,test", 1.0f, false,
+ "quote\" in the middle")));
+ assertEquals("Unexpected format",
+ String.format("%s,%d,%b,%s%s%s,%s,%b,%s%s", "test", 1, true, "\"\"\"quoted\"\" test\"", "\r\n",
+ "\"delimeter,test\"", "1.0", false, "\"quote\"\" in the middle\"", "\r\n"),
+ out.toString());
+ }
+
+ public void testPrintln() throws Exception
+ {
+ CSVFormat csvFormat = new CSVFormat();
+ final StringWriter out = new StringWriter();
+ csvFormat.println(out);
+ assertEquals("Unexpected new line", "\r\n", out.toString());
+ }
+
+ public void testPrint() throws Exception
+ {
+ CSVFormat csvFormat = new CSVFormat();
+ final StringWriter out = new StringWriter();
+ csvFormat.print(out, "test", true);
+ csvFormat.print(out, 1, false);
+ csvFormat.print(out, true, false);
+ csvFormat.print(out, "\"quoted\" test", false);
+ assertEquals("Unexpected format ",
+ String.format("%s,%d,%b,%s", "test", 1, true, "\"\"\"quoted\"\" test\""),
+ out.toString());
+ }
+
+ public void testPrintComments() throws Exception
+ {
+ CSVFormat csvFormat = new CSVFormat();
+ final StringWriter out = new StringWriter();
+ csvFormat.printComments(out, "comment1", "comment2");
+ assertEquals("Unexpected format of comments",
+ String.format("# %s%s# %s%s", "comment1", "\r\n", "comment2", "\r\n"),
+ out.toString());
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org