Search in sources :

Example 1 with Attribute

use of nu.xom.Attribute in project android-selector-chapek by inmite.

the class SelectorGenerator method addState.

private static void addState(Element item, String state, boolean value) {
    Attribute attribute = new Attribute(state, String.valueOf(value));
    attribute.setNamespace(NS, SCHEMA);
    item.addAttribute(attribute);
}
Also used : Attribute(nu.xom.Attribute)

Example 2 with Attribute

use of nu.xom.Attribute in project android-selector-chapek by inmite.

the class SelectorGenerator method generate.

public static void generate(VirtualFile newFile, List<SelectorDetector.Result> detectorResults) {
    Log.d("generating XML:");
    Element root = new Element("selector");
    root.addNamespaceDeclaration(NS, SCHEMA);
    List<String> allStatesWithoutNormal = new ArrayList<String>();
    for (SelectorDetector.Result result : detectorResults) {
        for (String state : result.states) {
            if (!state.equals(Constants.NORMAL) && !allStatesWithoutNormal.contains(state)) {
                allStatesWithoutNormal.add(state);
            }
        }
    }
    for (SelectorDetector.Result result : detectorResults) {
        Log.d("fileName=" + result.drawableName + ", states:" + result.states);
        Element item = new Element("item");
        Attribute attribute = new Attribute("drawable", "@drawable/" + result.drawableName);
        attribute.setNamespace(NS, SCHEMA);
        item.addAttribute(attribute);
        for (String state : allStatesWithoutNormal) {
            boolean defaultValue = Constants.sMapping.get(state).defaultValue;
            addState(item, Constants.sMapping.get(state).attributeName, result.states.contains(state) ? (!defaultValue) : defaultValue);
        }
        Log.d("row=" + item.toXML());
        root.appendChild(item);
    }
    Document doc = new Document(root);
    OutputStream os = null;
    try {
        os = newFile.getOutputStream(null);
        Serializer serializer = new Serializer(os);
        serializer.setIndent(4);
        serializer.write(doc);
    } catch (IOException e) {
        Log.e(e);
    } finally {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                Log.e(e);
            }
        }
    }
}
Also used : Attribute(nu.xom.Attribute) Element(nu.xom.Element) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(nu.xom.Document) Serializer(nu.xom.Serializer)

Example 3 with Attribute

use of nu.xom.Attribute in project wildfly by wildfly.

the class PermissionUtils method createPermissionsXmlAsset.

public static Asset createPermissionsXmlAsset(Permission... permissions) {
    final Element permissionsElement = new Element("permissions");
    permissionsElement.setNamespaceURI("http://xmlns.jcp.org/xml/ns/javaee");
    permissionsElement.addAttribute(new Attribute("version", "7"));
    for (Permission permission : permissions) {
        final Element permissionElement = new Element("permission");
        final Element classNameElement = new Element("class-name");
        final Element nameElement = new Element("name");
        classNameElement.appendChild(permission.getClass().getName());
        nameElement.appendChild(permission.getName());
        permissionElement.appendChild(classNameElement);
        permissionElement.appendChild(nameElement);
        final String actions = permission.getActions();
        if (actions != null && !actions.isEmpty()) {
            final Element actionsElement = new Element("actions");
            actionsElement.appendChild(actions);
            permissionElement.appendChild(actionsElement);
        }
        permissionsElement.appendChild(permissionElement);
    }
    Document document = new Document(permissionsElement);
    try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
        final NiceSerializer serializer = new NiceSerializer(stream);
        serializer.setIndent(4);
        serializer.setLineSeparator("\n");
        serializer.write(document);
        serializer.flush();
        return new StringAsset(stream.toString("UTF-8"));
    } catch (IOException e) {
        throw new IllegalStateException("Generating permissions.xml failed", e);
    }
}
Also used : StringAsset(org.jboss.shrinkwrap.api.asset.StringAsset) Attribute(nu.xom.Attribute) Element(nu.xom.Element) Permission(java.security.Permission) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Document(nu.xom.Document)

Aggregations

Attribute (nu.xom.Attribute)3 IOException (java.io.IOException)2 Document (nu.xom.Document)2 Element (nu.xom.Element)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1 Permission (java.security.Permission)1 ArrayList (java.util.ArrayList)1 Serializer (nu.xom.Serializer)1 StringAsset (org.jboss.shrinkwrap.api.asset.StringAsset)1