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

COVERAGE SUMMARY FOR SOURCE FILE [AppendFile.java]

nameclass, %method, %block, %line, %
AppendFile.java100% (1/1)100% (7/7)98%  (237/241)97%  (36/37)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class AppendFile100% (1/1)100% (7/7)98%  (237/241)97%  (36/37)
appendToFile (String, String, OsFamily): Statement 100% (1/1)93%  (39/42)80%  (4/5)
AppendFile (String, Iterable, String): void 100% (1/1)97%  (28/29)99%  (6/6)
AppendFile (String, Iterable): void 100% (1/1)100% (6/6)100% (2/2)
escapeVarTokens (String, OsFamily): String 100% (1/1)100% (69/69)100% (9/9)
functionDependencies (OsFamily): Iterable 100% (1/1)100% (2/2)100% (1/1)
hereFile (String, StringBuilder): void 100% (1/1)100% (39/39)100% (5/5)
render (OsFamily): String 100% (1/1)100% (54/54)100% (9/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;
29 
30import com.google.common.collect.ImmutableList;
31import com.google.common.collect.Iterables;
32import com.google.common.collect.Lists;
33import com.google.common.collect.Maps;
34 
35/**
36 * Creates a run script
37 * 
38 * @author Adrian Cole
39 */
40public class AppendFile implements Statement {
41   public static final String MARKER = "END_OF_FILE";
42 
43   final String path;
44   final Iterable<String> lines;
45   final String marker;
46 
47   public AppendFile(String path, Iterable<String> lines) {
48      this(path, lines, MARKER);
49   }
50 
51   public AppendFile(String path, Iterable<String> lines, String marker) {
52      this.path = checkNotNull(path, "path");
53      this.lines = checkNotNull(lines, "lines");
54      this.marker = checkNotNull(marker, "marker");
55      checkState(Iterables.size(lines) > 0, "you must pass something to execute");
56   }
57 
58   public static String escapeVarTokens(String toEscape, OsFamily family) {
59      Map<String, String> inputToEscape = Maps.newHashMap();
60      for (ShellToken token : ImmutableList.of(ShellToken.VARL, ShellToken.VARR)) {
61         if (!token.to(family).equals("")) {
62            String tokenS = "{" + token.toString().toLowerCase() + "}";
63            inputToEscape.put(tokenS, "{escvar}" + tokenS);
64         }
65      }
66      for (Entry<String, String> entry : inputToEscape.entrySet()) {
67         toEscape = toEscape.replace(entry.getKey(), entry.getValue());
68      }
69      return toEscape;
70   }
71 
72   @Override
73   public Iterable<String> functionDependencies(OsFamily family) {
74      return Collections.emptyList();
75   }
76 
77   @Override
78   public String render(OsFamily family) {
79      List<Statement> statements = Lists.newArrayList();
80      if (family == OsFamily.UNIX) {
81         StringBuilder builder = new StringBuilder();
82         hereFile(path, builder);
83         statements.add(interpret(builder.toString()));
84      } else {
85         for (String line : lines) {
86            statements.add(appendToFile(line, path, family));
87         }
88      }
89      return new StatementList(statements).render(family);
90   }
91 
92   protected void hereFile(String path, StringBuilder builder) {
93      builder.append("cat >> ").append(path).append(" <<'").append(marker).append("'\n");
94      for (String line : lines) {
95         builder.append(line).append("\n");
96      }
97      builder.append(marker).append("\n");
98   }
99 
100   protected Statement appendToFile(String line, String path, OsFamily family) {
101      String quote = "";
102      if (!ShellToken.VQ.to(family).equals("")) {
103         quote = "'";
104      } else {
105         line = escapeVarTokens(line, family);
106      }
107      return interpret(String.format("echo %s%s%s >>%s{lf}", quote, line, quote, path));
108   }
109 
110}

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