View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2025 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
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      // static methods only
39    }
40  
41    /**
42     * Parses a multi-value property value in DocView format.
43     * @param value Multi-valued property
44     * @return Property values
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     * Formats multiple values on DocView as a single string.
58     * @param values Values
59     * @return DocView values string
60     */
61    static String formatValues(List<String> values) {
62      return formatValues(values, PropertyType.STRING);
63    }
64  
65    /**
66     * Formats multiple values on DocView as a single string.
67     * @param values Values
68     * @param propertyType Property type from {@link PropertyType}
69     * @return DocView values string
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     // -- unsupported methods --
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 }