Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / conversion / ConversionProtoConverter.java
blobad44d2e642cf0f5d7e79c67611385f489f92aad5
1 // Copyright 2011 Google Inc. All Rights Reserved.
3 package com.google.appengine.api.conversion;
5 import com.google.appengine.api.conversion.ConversionServicePb.ConversionInput;
6 import com.google.common.base.Converter;
7 import com.google.common.base.Preconditions;
8 import com.google.common.base.StringUtil;
10 /**
11 * A reversible converter between {@link Conversion} and its corresponding
12 * protocol buffer.
15 class ConversionProtoConverter extends Converter<Conversion, ConversionInput> {
17 private static final String IMAGE_WIDTH_FLAG = "imageWidth";
18 private static final String FIRST_PAGE_FLAG = "firstPage";
19 private static final String LAST_PAGE_FLAG = "lastPage";
20 private static final String INPUT_LANGUAGE_FLAG = "input_language_hint";
22 private final DocumentProtoConverter documentProtoConverter = new DocumentProtoConverter();
24 /**
25 * Converts a {@link Conversion} to its corresponding protocol buffer.
27 * @throws IllegalArgumentException if output mime type or additional asset
28 * name is null, empty or comprises only whitespace characters
30 @Override
31 public ConversionInput doForward(Conversion conversion) {
32 ConversionHelper.validateInputDoc(conversion.getInputDoc());
33 Preconditions.checkArgument(!StringUtil.isEmptyOrWhitespace(conversion.getOutputMimeType()),
34 "Output mime type should not be null, empty or comprises only whitespace characters");
36 ConversionInput.Builder builder = ConversionInput.newBuilder();
37 builder.setInput(documentProtoConverter.convert(conversion.getInputDoc()))
38 .setOutputMimeType(conversion.getOutputMimeType());
39 ConversionOptions options = conversion.getOptions();
40 builder.addFlag(createAuxData(IMAGE_WIDTH_FLAG, Integer.toString(options.getImageWidth())));
41 builder.addFlag(createAuxData(FIRST_PAGE_FLAG, Integer.toString(options.getFirstPage())));
42 builder.addFlag(createAuxData(LAST_PAGE_FLAG, Integer.toString(options.getLastPage())));
44 if (options.getOcrInputLanguage() != null) {
45 builder.addFlag(createAuxData(INPUT_LANGUAGE_FLAG, options.getOcrInputLanguage()));
47 return builder.build();
50 @Override
51 public Conversion doBackward(ConversionInput conversionPb) {
52 return new Conversion(
53 documentProtoConverter.reverse(conversionPb.getInput()), conversionPb.getOutputMimeType());
56 /**
57 * Returns a converter-specific auxiliary flag with specified key and value.
59 private static ConversionInput.AuxData.Builder createAuxData(String key, String value) {
60 return ConversionInput.AuxData.newBuilder()
61 .setKey(key)
62 .setValue(value);