001/* 002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.common.xmap; 023 024import java.util.ArrayList; 025import java.util.List; 026 027/** 028 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 029 */ 030public class Path { 031 032 public static final String[] EMPTY_SEGMENTS = new String[0]; 033 034 final String path; 035 036 String[] segments; 037 038 String attribute; 039 040 public Path(String path) { 041 this.path = path; 042 parse(path); 043 } 044 045 @Override 046 public String toString() { 047 return path; 048 } 049 050 @Override 051 public boolean equals(Object obj) { 052 if (obj == this) { 053 return true; 054 } 055 if (obj instanceof Path) { 056 return ((Path) obj).path.equals(path); 057 } 058 return false; 059 } 060 061 @Override 062 public int hashCode() { 063 return path.hashCode(); 064 } 065 066 private void parse(String path) { 067 List<String> seg = new ArrayList<String>(); 068 StringBuilder buf = new StringBuilder(); 069 char[] chars = path.toCharArray(); 070 boolean attr = false; 071 for (char c : chars) { 072 switch (c) { 073 case '/': 074 seg.add(buf.toString()); 075 buf.setLength(0); 076 break; 077 case '@': 078 attr = true; 079 seg.add(buf.toString()); 080 buf.setLength(0); 081 break; 082 default: 083 buf.append(c); 084 break; 085 } 086 } 087 if (buf.length() > 0) { 088 if (attr) { 089 attribute = buf.toString(); 090 } else { 091 seg.add(buf.toString()); 092 } 093 } 094 int size = seg.size(); 095 if (size == 1 && seg.get(0).length() == 0) { 096 segments = EMPTY_SEGMENTS; 097 } else { 098 segments = seg.toArray(new String[size]); 099 } 100 } 101 102}