| 1 | 4 | |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
package com.jcabi.github.mock; |
| 31 | |
|
| 32 | |
import com.jcabi.aspects.Immutable; |
| 33 | |
import com.jcabi.aspects.Loggable; |
| 34 | |
import com.jcabi.github.Gist; |
| 35 | |
import com.jcabi.github.GistComments; |
| 36 | |
import com.jcabi.github.Github; |
| 37 | |
import com.jcabi.xml.XML; |
| 38 | |
import java.io.IOException; |
| 39 | |
import java.util.List; |
| 40 | |
import javax.json.JsonObject; |
| 41 | |
import lombok.EqualsAndHashCode; |
| 42 | |
import lombok.ToString; |
| 43 | |
import org.apache.commons.lang3.StringUtils; |
| 44 | |
import org.xembly.Directives; |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
@Immutable |
| 54 | |
@Loggable(Loggable.DEBUG) |
| 55 | 0 | @ToString |
| 56 | 1 | @EqualsAndHashCode(of = { "storage", "self", "gist" }) |
| 57 | |
@SuppressWarnings("PMD.TooManyMethods") |
| 58 | |
final class MkGist implements Gist { |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
private final transient MkStorage storage; |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
private final transient String self; |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
private final transient String gist; |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
MkGist( |
| 83 | |
final MkStorage stg, |
| 84 | |
final String login, |
| 85 | |
final String name |
| 86 | 11 | ) { |
| 87 | 11 | this.storage = stg; |
| 88 | 11 | this.self = login; |
| 89 | 11 | this.gist = name; |
| 90 | 11 | } |
| 91 | |
|
| 92 | |
@Override |
| 93 | |
public Github github() { |
| 94 | 0 | return new MkGithub(this.storage, this.self); |
| 95 | |
} |
| 96 | |
|
| 97 | |
@Override |
| 98 | |
public String identifier() { |
| 99 | 2 | return this.gist; |
| 100 | |
} |
| 101 | |
|
| 102 | |
@Override |
| 103 | |
public String read( |
| 104 | |
final String file |
| 105 | |
) throws IOException { |
| 106 | 24 | final List<XML> files = this.storage.xml().nodes( |
| 107 | 8 | String.format( |
| 108 | |
"%s/files/file[filename='%s']", |
| 109 | 8 | this.xpath(), file |
| 110 | |
) |
| 111 | |
); |
| 112 | 8 | if (files.isEmpty()) { |
| 113 | 0 | throw new IOException( |
| 114 | 0 | String.format("Couldn't find file with the name %s.", file) |
| 115 | |
); |
| 116 | |
} |
| 117 | 8 | final List<String> contents = files.get(0) |
| 118 | 8 | .xpath("raw_content/text()"); |
| 119 | 8 | String content = ""; |
| 120 | 8 | if (!contents.isEmpty()) { |
| 121 | 6 | content = contents.get(0); |
| 122 | |
} |
| 123 | 8 | return content; |
| 124 | |
} |
| 125 | |
|
| 126 | |
@Override |
| 127 | |
public void write( |
| 128 | |
final String file, |
| 129 | |
final String content |
| 130 | |
) |
| 131 | |
throws IOException { |
| 132 | 12 | this.storage.apply( |
| 133 | |
|
| 134 | 8 | new Directives().xpath(this.xpath()).xpath( |
| 135 | 4 | String.format("files[not(file[filename='%s'])]", file) |
| 136 | 4 | ).add("file").add("filename").set(file).up().add("raw_content") |
| 137 | |
); |
| 138 | 8 | this.storage.apply( |
| 139 | 8 | new Directives().xpath(this.xpath()).xpath( |
| 140 | 4 | String.format( |
| 141 | |
"files/file[filename='%s']/raw_content", |
| 142 | |
file |
| 143 | |
) |
| 144 | 4 | ).set(content) |
| 145 | |
); |
| 146 | 4 | } |
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
@Override |
| 154 | |
public void star() throws IOException { |
| 155 | 6 | this.storage.apply( |
| 156 | |
new Directives() |
| 157 | 2 | .xpath(this.xpath()) |
| 158 | 2 | .attr("starred", Boolean.toString(true)) |
| 159 | |
); |
| 160 | 2 | } |
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
@Override |
| 168 | |
public void unstar() throws IOException { |
| 169 | 3 | this.storage.apply( |
| 170 | |
new Directives() |
| 171 | 1 | .xpath(this.xpath()) |
| 172 | 1 | .attr("starred", Boolean.toString(false)) |
| 173 | |
); |
| 174 | 1 | } |
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
@Override |
| 182 | |
public boolean starred() throws IOException { |
| 183 | 15 | final List<String> xpath = this.storage.xml().xpath( |
| 184 | 5 | String.format("%s/@starred", this.xpath()) |
| 185 | |
); |
| 186 | 8 | return !xpath.isEmpty() && StringUtils.equalsIgnoreCase( |
| 187 | 3 | Boolean.toString(true), |
| 188 | 3 | xpath.get(0) |
| 189 | |
); |
| 190 | |
} |
| 191 | |
|
| 192 | |
@Override |
| 193 | |
public Gist fork() throws IOException { |
| 194 | 2 | this.storage.lock(); |
| 195 | |
final String number; |
| 196 | |
try { |
| 197 | 1 | final XML xml = this.storage.xml(); |
| 198 | 2 | number = Integer.toString( |
| 199 | 1 | 1 + xml.xpath("/github/gists/gist/id/text()").size() |
| 200 | |
); |
| 201 | 1 | final Directives dirs = new Directives().xpath("/github/gists") |
| 202 | 1 | .add("gist") |
| 203 | 1 | .add("id").set(number).up() |
| 204 | 1 | .add("files"); |
| 205 | 2 | final List<XML> files = xml.nodes( |
| 206 | 1 | String.format("%s/files/file", this.xpath()) |
| 207 | |
); |
| 208 | 1 | for (final XML file : files) { |
| 209 | 1 | final String filename = file.xpath("filename/text()").get(0); |
| 210 | |
|
| 211 | 1 | dirs.add("file") |
| 212 | 1 | .add("filename").set(filename).up() |
| 213 | 1 | .add("raw_content").set(this.read(filename)).up().up(); |
| 214 | 1 | } |
| 215 | 1 | this.storage.apply(dirs); |
| 216 | |
} finally { |
| 217 | 1 | this.storage.unlock(); |
| 218 | 1 | } |
| 219 | 1 | return new MkGist(this.storage, this.self, number); |
| 220 | |
} |
| 221 | |
|
| 222 | |
@Override |
| 223 | |
public GistComments comments() throws IOException { |
| 224 | 0 | throw new UnsupportedOperationException(); |
| 225 | |
} |
| 226 | |
|
| 227 | |
@Override |
| 228 | |
public JsonObject json() throws IOException { |
| 229 | 0 | return new JsonNode( |
| 230 | 0 | this.storage.xml().nodes(this.xpath()).get(0) |
| 231 | 0 | ).json(); |
| 232 | |
} |
| 233 | |
|
| 234 | |
@Override |
| 235 | |
public void patch( |
| 236 | |
final JsonObject json |
| 237 | |
) throws IOException { |
| 238 | 0 | new JsonPatch(this.storage).patch(this.xpath(), json); |
| 239 | 0 | } |
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
private String xpath() { |
| 246 | 25 | return String.format( |
| 247 | |
"/github/gists/gist[id='%s']", |
| 248 | |
this.gist |
| 249 | |
); |
| 250 | |
} |
| 251 | |
|
| 252 | |
} |