EMMA Coverage Report (generated Mon Oct 17 05:41:20 EDT 2011)
[all classes][org.jclouds.scriptbuilder.domain]

COVERAGE SUMMARY FOR SOURCE FILE [CreateFile.java]

nameclass, %method, %block, %line, %
CreateFile.java0%   (0/1)0%   (0/9)0%   (0/258)0%   (0/39)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CreateFile0%   (0/1)0%   (0/9)0%   (0/258)0%   (0/39)
<static initializer> 0%   (0/1)0%   (0/4)0%   (0/1)
CreateFile (String, Iterable): void 0%   (0/1)0%   (0/6)0%   (0/2)
CreateFile (String, Iterable, String): void 0%   (0/1)0%   (0/29)0%   (0/6)
addSpaceToEnsureWeDontAccidentallyRedirectFd (String): String 0%   (0/1)0%   (0/12)0%   (0/1)
appendToFile (String, String, OsFamily): Statement 0%   (0/1)0%   (0/43)0%   (0/5)
escapeVarTokens (String, OsFamily): String 0%   (0/1)0%   (0/69)0%   (0/9)
functionDependencies (OsFamily): Iterable 0%   (0/1)0%   (0/2)0%   (0/1)
hereFile (String, StringBuilder): void 0%   (0/1)0%   (0/39)0%   (0/5)
render (OsFamily): String 0%   (0/1)0%   (0/54)0%   (0/9)

1/**
2 * Licensed to jclouds, Inc. (jclouds) under one or more
3 * contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  jclouds licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied.  See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.jclouds.scriptbuilder.domain;
20 
21import static com.google.common.base.Preconditions.checkNotNull;
22import static com.google.common.base.Preconditions.checkState;
23import static org.jclouds.scriptbuilder.domain.Statements.interpret;
24 
25import java.util.Collections;
26import java.util.List;
27import java.util.Map;
28import java.util.Map.Entry;
29import java.util.regex.Pattern;
30 
31import com.google.common.collect.ImmutableList;
32import com.google.common.collect.Iterables;
33import com.google.common.collect.Lists;
34import com.google.common.collect.Maps;
35 
36/**
37 * Creates a run script
38 * 
39 * @author Adrian Cole
40 */
41public class CreateFile implements Statement {
42   public static final String MARKER = "END_OF_FILE";
43 
44   final String path;
45   final Iterable<String> lines;
46   final String marker;
47 
48   public CreateFile(String path, Iterable<String> lines) {
49      this(path, lines, MARKER);
50   }
51 
52   public CreateFile(String path, Iterable<String> lines, String marker) {
53      this.path = checkNotNull(path, "path");
54      this.lines = checkNotNull(lines, "lines");
55      this.marker = checkNotNull(marker, "marker");
56      checkState(Iterables.size(lines) > 0, "you must pass something to execute");
57   }
58 
59   public static String escapeVarTokens(String toEscape, OsFamily family) {
60      Map<String, String> inputToEscape = Maps.newHashMap();
61      for (ShellToken token : ImmutableList.of(ShellToken.VARL, ShellToken.VARR)) {
62         if (!token.to(family).equals("")) {
63            String tokenS = "{" + token.toString().toLowerCase() + "}";
64            inputToEscape.put(tokenS, "{escvar}" + tokenS);
65         }
66      }
67      for (Entry<String, String> entry : inputToEscape.entrySet()) {
68         toEscape = toEscape.replace(entry.getKey(), entry.getValue());
69      }
70      return toEscape;
71   }
72 
73   @Override
74   public Iterable<String> functionDependencies(OsFamily family) {
75      return Collections.emptyList();
76   }
77 
78   @Override
79   public String render(OsFamily family) {
80      List<Statement> statements = Lists.newArrayList();
81      if (family == OsFamily.UNIX) {
82         StringBuilder builder = new StringBuilder();
83         hereFile(path, builder);
84         statements.add(interpret(builder.toString()));
85      } else {
86         for (String line : lines) {
87            statements.add(appendToFile(line, path, family));
88         }
89      }
90      return new StatementList(statements).render(family);
91   }
92 
93   private void hereFile(String path, StringBuilder builder) {
94      builder.append("cat > ").append(path).append(" <<'").append(marker).append("'\n");
95      for (String line : lines) {
96         builder.append(line).append("\n");
97      }
98      builder.append(marker).append("\n");
99   }
100 
101   private Statement appendToFile(String line, String path, OsFamily family) {
102      String quote = "";
103      if (!ShellToken.VQ.to(family).equals("")) {
104         quote = "'";
105      } else {
106         line = escapeVarTokens(line, family);
107      }
108      return interpret(addSpaceToEnsureWeDontAccidentallyRedirectFd(String.format("echo %s%s%s>>%s{lf}", quote, line,
109               quote, path)));
110   }
111 
112   public static final Pattern REDIRECT_FD_PATTERN = Pattern.compile(".*[0-2]>>.*");
113 
114   static String addSpaceToEnsureWeDontAccidentallyRedirectFd(String line) {
115      return REDIRECT_FD_PATTERN.matcher(line).matches() ? line.replace(">>", " >>") : line;
116   }
117 
118}

[all classes][org.jclouds.scriptbuilder.domain]
EMMA 2.0.5312 (C) Vladimir Roubtsov