001 /**
002 * Copyright 2011 The Kuali Foundation Licensed under the
003 * Educational Community License, Version 2.0 (the "License"); you may
004 * not use this file except in compliance with the License. You may
005 * obtain a copy of the License at
006 *
007 * http://www.osedu.org/licenses/ECL-2.0
008 *
009 * Unless required by applicable law or agreed to in writing,
010 * software distributed under the License is distributed on an "AS IS"
011 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
012 * or implied. See the License for the specific language governing
013 * permissions and limitations under the License.
014 */
015
016 package org.kuali.common.impex.data.impl;
017
018 import org.apache.commons.lang3.StringUtils;
019
020 /**
021 * This class parses a .mpx file and creates an in-memory representation of the data
022 *
023 * @author andrewlubbers
024 */
025 public class MpxParser {
026
027 private static final String QUOTE = "\"";
028 private static final String TOKEN_DELIMITER = QUOTE + "," + QUOTE;
029
030 /**
031 * Split the line up into individual values and remove any .mpx related formatting
032 */
033 public static String[] parseMpxLine(String line) {
034 // Remove trailing/leading quotes
035 String trimmed = trimQuotes(line);
036
037 // Split the line up into individual values
038 String[] values = StringUtils.splitByWholeSeparator(trimmed, TOKEN_DELIMITER);
039
040 // Convert mpx special values (i.e. ${mpx.lf} -> \n )
041 for (int i = 0; i < values.length; i++) {
042 values[i] = ParseUtils.parse(values[i]);
043 }
044
045 // These are the original string values with all of the .mpx related
046 // formatting removed
047 return values;
048 }
049
050 /**
051 * Remove leading and trailing quotes (if any)
052 */
053 public static String trimQuotes(String line) {
054 if (StringUtils.startsWith(line, QUOTE)) {
055 line = StringUtils.substring(line, QUOTE.length());
056 }
057 int length = line.length();
058 if (StringUtils.endsWith(line, QUOTE)) {
059 line = StringUtils.substring(line, 0, length - QUOTE.length());
060 }
061 return line;
062 }
063
064 }