001 /** 002 * Copyright 2010 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 /** 017 * 018 */ 019 package org.kuali.student.common.assembly.data; 020 021 import java.util.Iterator; 022 023 import org.kuali.student.common.assembly.data.Data.Key; 024 /** 025 * @author wilj 026 * 027 */ 028 public class DefaultPathParser implements PathParser { 029 030 private static final String PATH_SEPARATOR = "/"; 031 032 @Override 033 public String format(final QueryPath path) { 034 final StringBuilder sb = new StringBuilder(); 035 for (final Iterator<Key> itr = path.iterator(); itr.hasNext();) { 036 final Key key = itr.next(); 037 if (key == null) { 038 // note, this branch should ideally never be hit 039 // but if we throw an exception here, it breaks the eclipse debugger when trying to introspect 040 sb.append("null"); 041 } else { 042 sb.append(key.toString()); 043 } 044 if (itr.hasNext()) { 045 sb.append(PATH_SEPARATOR); 046 } 047 } 048 return sb.toString(); 049 } 050 051 @Override 052 public String getWildCard() { 053 return "*"; 054 } 055 056 @Override 057 public QueryPath parse(final String path) { 058 final QueryPath result = new QueryPath(); 059 final String[] elements = path.split(PATH_SEPARATOR); 060 for (String element : elements) { 061 element = element.trim(); 062 if (!element.isEmpty()) { 063 Integer index = null; 064 try { 065 index = Integer.valueOf(element); 066 } catch (final Exception e) { 067 // do nothing 068 } 069 if (index == null) { 070 result.add(new Data.StringKey(element)); 071 } else { 072 result.add(new Data.IntegerKey(index)); 073 } 074 } 075 } 076 return result; 077 } 078 079 @Override 080 public String getPathSeparator() { 081 return PATH_SEPARATOR; 082 } 083 }