use of com.jcabi.xml.XML in project jcabi-github by jcabi.
the class MkGist method read.
@Override
public String read(final String file) throws IOException {
final List<XML> files = this.storage.xml().nodes(String.format("%s/files/file[filename='%s']", this.xpath(), file));
if (files.isEmpty()) {
throw new IOException(String.format("Couldn't find file with the name %s.", file));
}
final List<String> contents = files.get(0).xpath("raw_content/text()");
String content = "";
if (!contents.isEmpty()) {
content = contents.get(0);
}
return content;
}
use of com.jcabi.xml.XML in project jcabi-github by jcabi.
the class MkAssignees method iterate.
@Override
public Iterable<User> iterate() {
try {
final Set<User> assignees = new HashSet<User>();
assignees.add(new MkUser(this.storage, this.self));
final Iterable<User> collaborators = new MkIterable<User>(this.storage, String.format("%s/user", this.xpath()), new MkIterable.Mapping<User>() {
@Override
public User map(final XML xml) {
try {
return new MkUser(MkAssignees.this.storage, xml.xpath("login/text()").get(0));
} catch (final IOException ex) {
throw new IllegalStateException(ex);
}
}
});
for (final User collab : collaborators) {
assignees.add(collab);
}
return assignees;
} catch (final IOException ex) {
throw new IllegalStateException(ex);
}
}
use of com.jcabi.xml.XML in project jcabi-github by jcabi.
the class JsonPatchTest method patchesXml.
/**
* JsonPatch can patch an XML.
* @throws Exception If some problem inside
*/
@Test
public void patchesXml() throws Exception {
final MkStorage storage = new MkStorage.InFile();
new JsonPatch(storage).patch("/github", Json.createObjectBuilder().add("name", "hi you!").add("number", 1).build());
final XML xml = storage.xml();
MatcherAssert.assertThat(xml.xpath("/github/name/text()").get(0), Matchers.describedAs(xml.toString(), Matchers.endsWith("you!")));
}
use of com.jcabi.xml.XML in project wring by yegor256.
the class XePrint method text.
/**
* Render text via XPath.
* @param pattern Pattern to use
* @return Plain text
*/
public String text(final CharSequence pattern) {
final XML xml = new XMLDocument(new Xembler(this.dirs).domQuietly());
final Pattern ptn = Pattern.compile("\\{([^}]+)}");
final Matcher mtr = ptn.matcher(pattern);
final StringBuffer out = new StringBuffer(pattern.length());
while (mtr.find()) {
mtr.appendReplacement(out, xml.xpath(mtr.group(1)).get(0).replace("\\", "\\\\").replace("$", "\\$"));
}
mtr.appendTail(out);
return out.toString();
}
use of com.jcabi.xml.XML in project jcabi-github by jcabi.
the class JsonNodeTest method convertsXmlToJson.
/**
* JsonNode can text XML to JSON.
* @throws Exception If some problem inside
*/
@Test
public void convertsXmlToJson() throws Exception {
final XML xml = new XMLDocument("<user><name>Jeff</name><dept><title>IT</title></dept></user>");
final JsonObject json = new JsonNode(xml.nodes("user").get(0)).json();
MatcherAssert.assertThat(json, Matchers.notNullValue());
MatcherAssert.assertThat(json.getString("name"), Matchers.equalTo("Jeff"));
MatcherAssert.assertThat(json.getJsonObject("dept").getString("title"), Matchers.equalTo("IT"));
}
Aggregations