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
;
11 * A reversible converter between {@link Conversion} and its corresponding
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();
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
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();
51 public Conversion
doBackward(ConversionInput conversionPb
) {
52 return new Conversion(
53 documentProtoConverter
.reverse(conversionPb
.getInput()), conversionPb
.getOutputMimeType());
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()