summaryrefslogtreecommitdiffstats
path: root/branches/sca-java-1.x/modules/domain-search/src/main/java/org/apache/tuscany/sca/domain/search/impl/ParentField.java
blob: e4b84881fdb49b3582989210b604d6e18a3e156a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package org.apache.tuscany.sca.domain.search.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ParentField implements Serializable {

	private static final long serialVersionUID = -2090538050273088026L;

	final private List<ParentFieldElement> elements;

	public ParentField(String parentFieldValue) {
		int length = parentFieldValue.length();

		if (length == 0) {
			this.elements = Collections.emptyList();
			
			return;
			
		}
		
		this.elements = new ArrayList<ParentFieldElement>();

		ParentFieldElement element = null;
		StringBuilder sb = new StringBuilder();

		for (int i = 0; i < length ; i++) {
			char c = parentFieldValue.charAt(i);

			if (c == DomainPathAnalyzer.PATH_SEPARATOR || c == DomainPathAnalyzer.PATH_START) {

				if (sb.length() > 0 || element != null) {

					if (element == null) {
						element = new ParentFieldElement();
					}
					
					if (element.type == null) {
						element.type = "";
					}
					
					if (element.uri == null) {
						element.uri = element.name = sb.toString();
						
					} else {
						element.name = sb.toString();
					}
					
					sb.setLength(0);
					this.elements.add(element);
					element = null;

				}

			} else if (c == DomainPathAnalyzer.TYPE_SEPARATOR) {

				if (element == null) {
					element = new ParentFieldElement();
				}
				
				element.type = sb.toString();
				
				sb.setLength(0);

			} else if (c == DomainPathAnalyzer.URI_SEPARATOR) {
				
				if (element == null) {
					element = new ParentFieldElement();
				}
				
				element.uri = sb.toString();
				
				sb.setLength(0);
				
			} else {
				sb.append(c);
			}

		}
		
		if (sb.length() > 0 || element != null) {

			if (element == null) {
				element = new ParentFieldElement();
			}
			
			if (element.type == null) {
				element.type = "";
			}
			
			if (element.uri == null) {
				element.uri = element.name = sb.toString();
				
			} else {
				element.name = sb.toString();
			}
			
			sb.setLength(0);
			this.elements.add(element);
			element = null;

		}

	}
	
	public static String getURIPath(ParentField parentField) {
		return getURIPath(parentField, System.getProperty("file.separator"));
	}
	
	public static String getURIPath(ParentField parentField, String pathSeparator) {
		StringBuilder sb = new StringBuilder();
		sb.append(pathSeparator);
		int elementsCount = parentField.getElementsCount();
		
		for (int i = 0 ; i < elementsCount ; i++) {
			sb.append(parentField.getElementName(i));
			sb.append(pathSeparator);
			
		}
		
		if (sb.length() > pathSeparator.length()) {
			sb.setLength(sb.length() - pathSeparator.length());
		}
		
		return sb.toString();
		
	}

	public static int getParentElementsCount(String parent) {
		int length = parent.length();

		if (length == 0) {
			return 0;
		}

		boolean pathSeparatorBefore = true;
		int count = 1;

		for (int i = 0; i < length - 1; i++) {
			char c = parent.charAt(i);

			if (c == DomainPathAnalyzer.PATH_SEPARATOR && !pathSeparatorBefore) {
				pathSeparatorBefore = true;
				count++;

			} else {
				pathSeparatorBefore = false;
			}

		}

		return count;

	}
	
	public int getElementsCount() {
		return this.elements.size();
	}
	
	public String getElementType(int index) {
		return this.elements.get(index).type;
	}
	
	public String getElementURI(int index) {
		return this.elements.get(index).uri;
	}
	
	public String getElementName(int index) {
		return this.elements.get(index).name;
	}

	final private static class ParentFieldElement {

		String type;

		String uri;

		String name;

	}

}