|
| 1 | +/* |
| 2 | + * Copyright IBM Corp. 2017, 2018 |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.ibm.g11n.pipeline.tools.cli; |
| 17 | + |
| 18 | +import java.io.File; |
| 19 | +import java.io.FileOutputStream; |
| 20 | +import java.io.IOException; |
| 21 | + |
| 22 | +import com.beust.jcommander.Parameter; |
| 23 | +import com.beust.jcommander.Parameters; |
| 24 | +import com.ibm.g11n.pipeline.client.ServiceException; |
| 25 | + |
| 26 | +/** |
| 27 | + * Exports document content from a translatable document. |
| 28 | + * |
| 29 | + * @author John Emmons |
| 30 | + */ |
| 31 | +@Parameters(commandDescription = "Exports document content from a translatable document.") |
| 32 | +final class ExportDocumentCmd extends DocumentCmd { |
| 33 | + @Parameter( |
| 34 | + names = {"-l", "--language"}, |
| 35 | + description = "Language ID", |
| 36 | + required = true) |
| 37 | + private String languageId; |
| 38 | + |
| 39 | + @Parameter( |
| 40 | + names = {"-f", "--file"}, |
| 41 | + description = "File name to be exported", |
| 42 | + required = true) |
| 43 | + private String fileName; |
| 44 | + |
| 45 | + |
| 46 | + @Override |
| 47 | + protected void _execute() { |
| 48 | + byte[] content; |
| 49 | + try { |
| 50 | + content = getClient().getDocumentContent(type, documentId, languageId); |
| 51 | + } catch (ServiceException e) { |
| 52 | + throw new RuntimeException(e); |
| 53 | + } |
| 54 | + |
| 55 | + File f = new File(fileName); |
| 56 | + try { |
| 57 | + FileOutputStream fos = new FileOutputStream(f); |
| 58 | + fos.write(content); |
| 59 | + fos.flush(); |
| 60 | + fos.close(); |
| 61 | + } catch (IOException e) { |
| 62 | + throw new RuntimeException("Failed to write the document content to " + fileName + ": " + e.getMessage(), e); |
| 63 | + } |
| 64 | + |
| 65 | + System.out.println("Document content from document:" + documentId |
| 66 | + + ", language: " + languageId + " was successfully saved to file " |
| 67 | + + fileName); |
| 68 | + } |
| 69 | +} |
0 commit comments