1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package io.wcm.tooling.commons.packmgr.unpack;
21
22 import java.io.InputStream;
23 import java.math.BigDecimal;
24 import java.util.Calendar;
25 import java.util.List;
26
27 import javax.jcr.Binary;
28 import javax.jcr.PropertyType;
29 import javax.jcr.RepositoryException;
30 import javax.jcr.Value;
31
32 import org.apache.jackrabbit.spi.Name;
33 import org.apache.jackrabbit.vault.util.DocViewProperty2;
34
35 final class DocViewUtil {
36
37 private DocViewUtil() {
38
39 }
40
41
42
43
44
45
46 static List<String> parseValues(String value) {
47 try {
48 DocViewProperty2 prop = DocViewProperty2.parse(DUMMY_NAME, value);
49 return prop.getStringValues();
50 }
51 catch (RepositoryException ex) {
52 throw new IllegalArgumentException("Unable to parse values: " + value, ex);
53 }
54 }
55
56
57
58
59
60
61 static String formatValues(List<String> values) {
62 return formatValues(values, PropertyType.STRING);
63 }
64
65
66
67
68
69
70
71 static String formatValues(List<String> values, int propertyType) {
72 Value[] valueObjects = values.stream()
73 .map(value -> new MockValue(value, propertyType))
74 .toArray(size -> new Value[size]);
75 try {
76 return DocViewProperty2.fromValues(DUMMY_NAME, valueObjects, propertyType, true, false, false).formatValue();
77 }
78 catch (RepositoryException ex) {
79 throw new IllegalArgumentException("Unable to format values: " + valueObjects, ex);
80 }
81 }
82
83
84 private static final Name DUMMY_NAME = new Name() {
85
86 private static final long serialVersionUID = 1L;
87
88 @Override
89 public String getLocalName() {
90 return "dummy";
91 }
92
93 @Override
94 public String getNamespaceURI() {
95 return NS_DEFAULT_URI;
96 }
97
98 @Override
99 public int compareTo(Object o) {
100 throw new UnsupportedOperationException();
101 }
102 };
103
104 private static class MockValue implements Value {
105
106 private final String value;
107 private final int type;
108
109 MockValue(String value, int type) {
110 this.value = value;
111 this.type = type;
112 }
113
114 @Override
115 public String getString() {
116 return value;
117 }
118
119 @Override
120 public int getType() {
121 return type;
122 }
123
124
125
126
127 @Override
128 public InputStream getStream() {
129 throw new UnsupportedOperationException();
130 }
131
132 @Override
133 public Binary getBinary() {
134 throw new UnsupportedOperationException();
135 }
136
137 @Override
138 public long getLong() {
139 throw new UnsupportedOperationException();
140 }
141
142 @Override
143 public double getDouble() {
144 throw new UnsupportedOperationException();
145 }
146
147 @Override
148 public BigDecimal getDecimal() {
149 throw new UnsupportedOperationException();
150 }
151
152 @Override
153 public Calendar getDate() {
154 throw new UnsupportedOperationException();
155 }
156
157 @Override
158 public boolean getBoolean() {
159 throw new UnsupportedOperationException();
160 }
161
162 }
163
164 }