Skip to content

Package: FileGenerationExporter

FileGenerationExporter

nameinstructionbranchcomplexitylinemethod
FileGenerationExporter()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
askOverwriteAllowed(String)
M: 39 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%
export(Collection, UserInteraction)
M: 24 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
export(GenerationInfo)
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
export(String, URI)
M: 36 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
handleFileSystem(String, URI)
M: 20 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
handlePlatform(String, URI)
M: 49 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
prepareStructure(IContainer)
M: 27 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
wrapGeneration(GenerationInfo)
M: 17 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
writeToFileSystemFile(String, File)
M: 19 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2015 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Stefan Dirix - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.emf2web.exporter;
15:
16: import java.io.BufferedWriter;
17: import java.io.ByteArrayInputStream;
18: import java.io.File;
19: import java.io.FileOutputStream;
20: import java.io.IOException;
21: import java.io.OutputStreamWriter;
22: import java.io.Writer;
23: import java.text.MessageFormat;
24: import java.util.Collection;
25:
26: import org.eclipse.core.resources.IContainer;
27: import org.eclipse.core.resources.IFile;
28: import org.eclipse.core.resources.IFolder;
29: import org.eclipse.core.resources.IResource;
30: import org.eclipse.core.resources.IWorkspaceRoot;
31: import org.eclipse.core.resources.ResourcesPlugin;
32: import org.eclipse.core.runtime.CoreException;
33: import org.eclipse.core.runtime.Path;
34: import org.eclipse.emf.common.util.URI;
35: import org.eclipse.emf.ecp.emf2web.controller.xtend.GenerationInfo;
36: import org.eclipse.emf.ecp.emf2web.internal.messages.Messages;
37:
38: /**
39: * Exporter for saving the generated content to file.
40: *
41: * @author Stefan Dirix
42: *
43: */
44: public class FileGenerationExporter implements GenerationExporter {
45:
46:         private UserInteraction userInteraction;
47:
48:         private boolean doCancel;
49:
50:         @Override
51:         public void export(Collection<? extends GenerationInfo> generationInfos, UserInteraction userInteraction)
52:                 throws IOException {
53:                 this.userInteraction = userInteraction;
54:                 doCancel = false;
55:•                for (final GenerationInfo generationInfo : generationInfos) {
56:•                        if (!doCancel) {
57:                                 export(generationInfo);
58:                         }
59:                 }
60:         }
61:
62:         /**
63:          * Export the given {@link GenerationInfo}.
64:          *
65:          * @param generationInfo
66:          * The {@link GenerationInfo} to export.
67:          * @throws IOException
68:          * If something went wrong during export.
69:          */
70:         protected void export(GenerationInfo generationInfo) throws IOException {
71:                 final String exportString = wrapGeneration(generationInfo);
72:                 final URI location = generationInfo.getLocation();
73:                 export(exportString, location);
74:         }
75:
76:         /**
77:          * Wraps the generated content if needed.
78:          *
79:          * @param generationInfo
80:          * The {@link GenerationInfo} containing the information.
81:          * @return
82:          *                 The (maybe wrapped) generated content.
83:          */
84:         protected String wrapGeneration(GenerationInfo generationInfo) {
85:•                if (generationInfo.getWrapper() == null || !generationInfo.isWrap()) {
86:                         return generationInfo.getGeneratedString();
87:                 }
88:                 return generationInfo.getWrapper().wrap(generationInfo.getGeneratedString(), generationInfo.getType());
89:         }
90:
91:         /**
92:          * Exports the given string to the given {@code location}.
93:          *
94:          * @param exportString
95:          * The string to export.
96:          * @param location
97:          * The {@link URI} depicting the location to export to.
98:          * @throws IOException
99:          * If something went wrong during export.
100:          */
101:         protected void export(String exportString, URI location) throws IOException {
102:•                if (location.isPlatform()) {
103:                         try {
104:                                 handlePlatform(exportString, location);
105:                         } catch (final CoreException ex) {
106:                                 throw new IOException(ex);
107:                         }
108:•                } else if (location.isFile()) {
109:                         handleFileSystem(exportString, location);
110:                 } else {
111:                         throw new IOException(MessageFormat.format(Messages.FileGenerationExporter_URI_Error, location.toString()));
112:                 }
113:         }
114:
115:         private void handlePlatform(String exportString, URI location) throws CoreException {
116:                 final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
117:                 final IFile file = root.getFile(new Path(location.toPlatformString(true)));
118:
119:•                if (file.exists() && !askOverwriteAllowed(file.getLocationURI().toString())) {
120:                         return;
121:                 }
122:
123:•                if (file.exists()) {
124:                         file.setContents(new ByteArrayInputStream(exportString.getBytes()), IResource.KEEP_HISTORY, null);
125:                 } else {
126:                         prepareStructure(file.getParent());
127:                         file.create(new ByteArrayInputStream(exportString.getBytes()), IResource.NONE, null);
128:                 }
129:         }
130:
131:         private void prepareStructure(IContainer container) throws CoreException {
132:                 final IContainer parent = container.getParent();
133:•                if (parent instanceof IFolder) {
134:                         prepareStructure(parent);
135:                 }
136:•                if (IFolder.class.isInstance(container) && !container.exists()) {
137:                         final IFolder folder = IFolder.class.cast(container);
138:                         folder.create(false, true, null);
139:                 }
140:         }
141:
142:         private void handleFileSystem(String exportString, URI location) throws IOException {
143:                 final File file = new File(location.toFileString());
144:•                if (file.exists() && !askOverwriteAllowed(file.getAbsolutePath())) {
145:                         return;
146:                 }
147:                 writeToFileSystemFile(exportString, file);
148:         }
149:
150:         private void writeToFileSystemFile(String exportString, File file) throws IOException {
151:                 final Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "utf-8")); //$NON-NLS-1$
152:                 try {
153:                         writer.write(exportString);
154:                 } finally {
155:                         writer.close();
156:                 }
157:         }
158:
159:         /**
160:          * Ask the user if a file shall be overwritten.
161:          *
162:          * @param fileName
163:          * The name of the file displayed to the user.
164:          * @return
165:          *                 {@code true} if the file shall be overwritten, {@code false} otherwise.
166:          */
167:         protected boolean askOverwriteAllowed(String fileName) {
168:                 final String title = Messages.FileGenerationExporter_OverwriteWarning;
169:                 final String question = MessageFormat.format(Messages.FileGenerationExporter_OverwriteWarningMessage,
170:                         fileName);
171:                 final String toggleQuestion = Messages.FileGenerationExporter_OverwriteWarningToggle;
172:                 final int result = userInteraction.askQuestion(title, question, toggleQuestion);
173:•                if (result == UserInteraction.CANCEL) {
174:                         doCancel = true;
175:                         return false;
176:•                } else if (result == UserInteraction.NO) {
177:                         return false;
178:•                } else if (result == UserInteraction.YES) {
179:                         return true;
180:                 }
181:
182:                 // should not occur
183:                 return false;
184:         }
185: }