Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2012. JSpringBot. All Rights Reserved.
*
* See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The JSpringBot 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.jspringbot.keyword.csv;
import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.CSVWriter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.CaseInsensitiveMap;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.Validate;
import org.jspringbot.keyword.csv.criteria.*;
import org.jspringbot.syntax.HighlightRobotLogger;
import java.io.*;
import java.util.*;
import static org.jspringbot.keyword.expression.utils.ELFileUtils.resource;
import static org.jspringbot.keyword.expression.utils.ELFileUtils.stream;
public class CSVState {
public static final HighlightRobotLogger LOG = HighlightRobotLogger.getLogger(CSVState.class);
public static final String OR_DISJUNCTION = "OR";
public static final String AND_CONJUNCTION = "AND";
public static int MAX_LOG_LINES = 50;
private List lines;
private String[] headerRow;
private Map headers;
private String name;
private CSVLineCriteria currentCriteria;
private List queryResults;
private Stack appender;
private File openedFile;
public CSVState(String name) {
this.name = name;
}
public CSVState listAsNewState(String name) {
CSVState newState = new CSVState(name);
newState.lines = list();
newState.headers = headers;
return newState;
}
public String getName() {
return name;
}
public void createCriteria() {
appender = new Stack();
currentCriteria = new CSVLineCriteria(lines, headers);
queryResults = null;
appender.push(currentCriteria);
}
public void startDisjunction() {
appender.push(Restrictions.disjunction());
}
public void startConjunction() {
appender.push(Restrictions.conjunction());
}
public void endDisjunction() {
RestrictionAppender disjunction = appender.pop();
if (!DisjunctionRestriction.class.isInstance(disjunction)) {
throw new IllegalStateException("Last started was not an or disjunction.");
}
appender.peek().append((DisjunctionRestriction) disjunction);
}
public void endConjunction() {
RestrictionAppender conjunction = appender.pop();
if (!ConjunctionRestriction.class.isInstance(conjunction)) {
throw new IllegalStateException("Last started was not an and conjunction.");
}
appender.peek().append((ConjunctionRestriction) conjunction);
}
public void addColumnNameEqualsRestriction(String name, String value) {
appender.peek().append(Restrictions.columnNameEquals(name, value));
queryResults = null;
LOG.createAppender()
.append("Add Column Name Equals Restriction:")
.appendProperty("Column Name", name)
.appendProperty("Column Value Equals", value)
.appendProperty("junction", getJunction())
.log();
}
private String getJunction() {
if (DisjunctionRestriction.class.isInstance(appender.peek())) {
return OR_DISJUNCTION;
}
return AND_CONJUNCTION;
}
public void addColumnIndexEqualsRestriction(int index, String value) {
appender.peek().append(Restrictions.columnIndexEquals(index, value));
LOG.createAppender()
.append("Add Column Index Equals Restriction:")
.appendProperty("Column Index", index)
.appendProperty("Column Value Equals", value)
.appendProperty("junction", getJunction())
.log();
}
public int projectCount() {
int count = currentCriteria.count();
LOG.createAppender()
.append("Projected Count:")
.appendProperty("Count", count)
.log();
return count;
}
public List list() {
if (currentCriteria == null) {
throw new IllegalStateException("No csv criteria was created.");
}
if (CollectionUtils.isEmpty(appender)) {
throw new IllegalStateException("No restriction appenders found.");
}
if (appender.size() != 1) {
throw new IllegalStateException("Junction restriction was not properly ended.");
}
if (queryResults == null) {
queryResults = currentCriteria.list();
}
LOG.createAppender()
.append("CSV List Result:")
.appendText(toCSV(queryResults))
.log();
return queryResults;
}
public List