Search in sources :

Example 1 with XML

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;
}
Also used : XML(com.jcabi.xml.XML) IOException(java.io.IOException) ToString(lombok.ToString)

Example 2 with XML

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);
    }
}
Also used : User(com.jcabi.github.User) XML(com.jcabi.xml.XML) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 3 with XML

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!")));
}
Also used : XML(com.jcabi.xml.XML) Test(org.junit.Test)

Example 4 with XML

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();
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) XML(com.jcabi.xml.XML) Xembler(org.xembly.Xembler) XMLDocument(com.jcabi.xml.XMLDocument)

Example 5 with XML

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"));
}
Also used : XML(com.jcabi.xml.XML) JsonObject(javax.json.JsonObject) XMLDocument(com.jcabi.xml.XMLDocument) Test(org.junit.Test)

Aggregations

XML (com.jcabi.xml.XML)9 ToString (lombok.ToString)4 XMLDocument (com.jcabi.xml.XMLDocument)2 IOException (java.io.IOException)2 JsonObject (javax.json.JsonObject)2 Test (org.junit.Test)2 Content (com.jcabi.github.Content)1 Label (com.jcabi.github.Label)1 User (com.jcabi.github.User)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 JsonArrayBuilder (javax.json.JsonArrayBuilder)1 JsonObjectBuilder (javax.json.JsonObjectBuilder)1 JsonValue (javax.json.JsonValue)1 Directives (org.xembly.Directives)1 Xembler (org.xembly.Xembler)1