-
Notifications
You must be signed in to change notification settings - Fork 10
/
ImageHandler.java
60 lines (46 loc) · 2.01 KB
/
ImageHandler.java
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
/*
Copyright 2014 Yahoo! Inc.
Copyrights licensed under the BSD License. See the accompanying LICENSE file for terms.
*/
package com.yahoo.xpathproto;
import org.apache.commons.jxpath.JXPathContext;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.google.protobuf.Message;
import com.yahoo.xpathproto.JXPathCopier;
import com.yahoo.xpathproto.ObjectToProtoHandler;
import com.yahoo.xpathproto.TransformTestProtos;
import com.yahoo.xpathproto.dataobject.Config;
import com.yahoo.xpathproto.dataobject.Context;
public class ImageHandler implements ObjectToProtoHandler {
@Override
public Message.Builder getProtoBuilder(final JXPathContext context, final Context vars, final Config.Entry entry) {
JXPathContext curContext = context;
if (!entry.getPath().isEmpty()) {
curContext = JXPathCopier.getRelativeContext(context, entry.getPath());
}
if (null == curContext) {
return null;
}
return copyObjectToImageAsset(curContext);
}
@Override
public List<Message.Builder> getRepeatedProtoBuilder(
final JXPathContext context, final Context vars, final Config.Entry entry) {
List<Message.Builder> builders = new ArrayList();
Iterator iterator = context.iterate(entry.getPath());
while (iterator.hasNext()) {
Object value = iterator.next();
builders.add(copyObjectToImageAsset(JXPathContext.newContext(value)));
}
return builders;
}
public static TransformTestProtos.ContentImage.Builder copyObjectToImageAsset(final JXPathContext context) {
TransformTestProtos.ContentImage.Builder imageBuilder = TransformTestProtos.ContentImage.newBuilder();
JXPathCopier imageCopier = new JXPathCopier(context, imageBuilder);
imageCopier.copyAsString("content_type", "type").copyAsInteger("width").copyAsInteger("height")
.copyAsString("url");
return imageBuilder.isInitialized() ? imageBuilder : null;
}
}