Search in sources :

Example 1 with Text

use of org.w3c.dom.Text in project camel by apache.

the class CMSenderOneMessageImpl method createXml.

private String createXml(final CMMessage message) {
    try {
        final ByteArrayOutputStream xml = new ByteArrayOutputStream();
        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        // Get the DocumentBuilder
        final DocumentBuilder docBuilder = factory.newDocumentBuilder();
        // Create blank DOM Document
        final DOMImplementation impl = docBuilder.getDOMImplementation();
        final Document doc = impl.createDocument(null, "MESSAGES", null);
        // ROOT Element es MESSAGES
        final Element root = doc.getDocumentElement();
        // AUTHENTICATION element
        final Element authenticationElement = doc.createElement("AUTHENTICATION");
        final Element productTokenElement = doc.createElement("PRODUCTTOKEN");
        authenticationElement.appendChild(productTokenElement);
        final Text productTokenValue = doc.createTextNode("" + productToken);
        productTokenElement.appendChild(productTokenValue);
        root.appendChild(authenticationElement);
        // MSG Element
        final Element msgElement = doc.createElement("MSG");
        root.appendChild(msgElement);
        // <FROM>VALUE</FROM>
        final Element fromElement = doc.createElement("FROM");
        fromElement.appendChild(doc.createTextNode(message.getSender()));
        msgElement.appendChild(fromElement);
        // <BODY>VALUE</BODY>
        final Element bodyElement = doc.createElement("BODY");
        bodyElement.appendChild(doc.createTextNode(message.getMessage()));
        msgElement.appendChild(bodyElement);
        // <TO>VALUE</TO>
        final Element toElement = doc.createElement("TO");
        toElement.appendChild(doc.createTextNode(message.getPhoneNumber()));
        msgElement.appendChild(toElement);
        // false
        if (message.isUnicode()) {
            final Element dcsElement = doc.createElement("DCS");
            dcsElement.appendChild(doc.createTextNode("8"));
            msgElement.appendChild(dcsElement);
        }
        // <REFERENCE>VALUE</REFERENCE> -Alfanum
        final String id = message.getIdAsString();
        if (id != null && !id.isEmpty()) {
            final Element refElement = doc.createElement("REFERENCE");
            refElement.appendChild(doc.createTextNode("" + message.getIdAsString()));
            msgElement.appendChild(refElement);
        }
        // <MAXIMUMNUMBEROFMESSAGEPARTS>8</MAXIMUMNUMBEROFMESSAGEPARTS>
        if (message.isMultipart()) {
            final Element minMessagePartsElement = doc.createElement("MINIMUMNUMBEROFMESSAGEPARTS");
            minMessagePartsElement.appendChild(doc.createTextNode("1"));
            msgElement.appendChild(minMessagePartsElement);
            final Element maxMessagePartsElement = doc.createElement("MAXIMUMNUMBEROFMESSAGEPARTS");
            maxMessagePartsElement.appendChild(doc.createTextNode(Integer.toString(message.getMultiparts())));
            msgElement.appendChild(maxMessagePartsElement);
        }
        // Creatate XML as String
        final Transformer aTransformer = TransformerFactory.newInstance().newTransformer();
        aTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
        final Source src = new DOMSource(doc);
        final Result dest = new StreamResult(xml);
        aTransformer.transform(src, dest);
        return xml.toString();
    } catch (final TransformerException e) {
        throw new XMLConstructionException(String.format("Cant serialize CMMessage %s", message), e);
    } catch (final ParserConfigurationException e) {
        throw new XMLConstructionException(String.format("Cant serialize CMMessage %s", message), e);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.w3c.dom.Element) DOMImplementation(org.w3c.dom.DOMImplementation) Text(org.w3c.dom.Text) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) XMLConstructionException(org.apache.camel.component.cm.exceptions.XMLConstructionException) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException)

Example 2 with Text

use of org.w3c.dom.Text in project camel by apache.

the class DomConverter method toString.

private String toString(Node node, Exchange exchange) throws TransformerException {
    String s;
    if (node instanceof Text) {
        Text textnode = (Text) node;
        StringBuilder b = new StringBuilder();
        b.append(textnode.getNodeValue());
        textnode = (Text) textnode.getNextSibling();
        while (textnode != null) {
            b.append(textnode.getNodeValue());
            textnode = (Text) textnode.getNextSibling();
        }
        s = b.toString();
    } else {
        s = xml.toString(node, exchange);
    }
    return s;
}
Also used : Text(org.w3c.dom.Text)

Example 3 with Text

use of org.w3c.dom.Text in project camel by apache.

the class DomConverter method append.

private static void append(StringBuilder buffer, Node node) {
    if (node instanceof Text) {
        Text text = (Text) node;
        buffer.append(text.getTextContent());
    } else if (node instanceof Attr) {
        Attr attribute = (Attr) node;
        buffer.append(attribute.getTextContent());
    } else if (node instanceof Element) {
        Element element = (Element) node;
        append(buffer, element.getChildNodes());
    }
}
Also used : Element(org.w3c.dom.Element) Text(org.w3c.dom.Text) Attr(org.w3c.dom.Attr)

Example 4 with Text

use of org.w3c.dom.Text in project hadoop by apache.

the class AllocationFileLoaderService method loadQueue.

/**
   * Loads a queue from a queue element in the configuration file
   */
private void loadQueue(String parentName, Element element, Map<String, Resource> minQueueResources, Map<String, Resource> maxQueueResources, Map<String, Resource> maxChildQueueResources, Map<String, Integer> queueMaxApps, Map<String, Integer> userMaxApps, Map<String, Float> queueMaxAMShares, Map<String, ResourceWeights> queueWeights, Map<String, SchedulingPolicy> queuePolicies, Map<String, Long> minSharePreemptionTimeouts, Map<String, Long> fairSharePreemptionTimeouts, Map<String, Float> fairSharePreemptionThresholds, Map<String, Map<AccessType, AccessControlList>> queueAcls, Map<String, Map<ReservationACL, AccessControlList>> resAcls, Map<FSQueueType, Set<String>> configuredQueues, Set<String> reservableQueues, Set<String> nonPreemptableQueues) throws AllocationConfigurationException {
    String queueName = CharMatcher.WHITESPACE.trimFrom(element.getAttribute("name"));
    if (queueName.contains(".")) {
        throw new AllocationConfigurationException("Bad fair scheduler config " + "file: queue name (" + queueName + ") shouldn't contain period.");
    }
    if (queueName.isEmpty()) {
        throw new AllocationConfigurationException("Bad fair scheduler config " + "file: queue name shouldn't be empty or " + "consist only of whitespace.");
    }
    if (parentName != null) {
        queueName = parentName + "." + queueName;
    }
    Map<AccessType, AccessControlList> acls = new HashMap<>();
    Map<ReservationACL, AccessControlList> racls = new HashMap<>();
    NodeList fields = element.getChildNodes();
    boolean isLeaf = true;
    boolean isReservable = false;
    for (int j = 0; j < fields.getLength(); j++) {
        Node fieldNode = fields.item(j);
        if (!(fieldNode instanceof Element))
            continue;
        Element field = (Element) fieldNode;
        if ("minResources".equals(field.getTagName())) {
            String text = ((Text) field.getFirstChild()).getData().trim();
            Resource val = FairSchedulerConfiguration.parseResourceConfigValue(text);
            minQueueResources.put(queueName, val);
        } else if ("maxResources".equals(field.getTagName())) {
            String text = ((Text) field.getFirstChild()).getData().trim();
            Resource val = FairSchedulerConfiguration.parseResourceConfigValue(text);
            maxQueueResources.put(queueName, val);
        } else if ("maxChildResources".equals(field.getTagName())) {
            String text = ((Text) field.getFirstChild()).getData().trim();
            Resource val = FairSchedulerConfiguration.parseResourceConfigValue(text);
            maxChildQueueResources.put(queueName, val);
        } else if ("maxRunningApps".equals(field.getTagName())) {
            String text = ((Text) field.getFirstChild()).getData().trim();
            int val = Integer.parseInt(text);
            queueMaxApps.put(queueName, val);
        } else if ("maxAMShare".equals(field.getTagName())) {
            String text = ((Text) field.getFirstChild()).getData().trim();
            float val = Float.parseFloat(text);
            val = Math.min(val, 1.0f);
            queueMaxAMShares.put(queueName, val);
        } else if ("weight".equals(field.getTagName())) {
            String text = ((Text) field.getFirstChild()).getData().trim();
            double val = Double.parseDouble(text);
            queueWeights.put(queueName, new ResourceWeights((float) val));
        } else if ("minSharePreemptionTimeout".equals(field.getTagName())) {
            String text = ((Text) field.getFirstChild()).getData().trim();
            long val = Long.parseLong(text) * 1000L;
            minSharePreemptionTimeouts.put(queueName, val);
        } else if ("fairSharePreemptionTimeout".equals(field.getTagName())) {
            String text = ((Text) field.getFirstChild()).getData().trim();
            long val = Long.parseLong(text) * 1000L;
            fairSharePreemptionTimeouts.put(queueName, val);
        } else if ("fairSharePreemptionThreshold".equals(field.getTagName())) {
            String text = ((Text) field.getFirstChild()).getData().trim();
            float val = Float.parseFloat(text);
            val = Math.max(Math.min(val, 1.0f), 0.0f);
            fairSharePreemptionThresholds.put(queueName, val);
        } else if ("schedulingPolicy".equals(field.getTagName()) || "schedulingMode".equals(field.getTagName())) {
            String text = ((Text) field.getFirstChild()).getData().trim();
            SchedulingPolicy policy = SchedulingPolicy.parse(text);
            queuePolicies.put(queueName, policy);
        } else if ("aclSubmitApps".equals(field.getTagName())) {
            String text = ((Text) field.getFirstChild()).getData();
            acls.put(AccessType.SUBMIT_APP, new AccessControlList(text));
        } else if ("aclAdministerApps".equals(field.getTagName())) {
            String text = ((Text) field.getFirstChild()).getData();
            acls.put(AccessType.ADMINISTER_QUEUE, new AccessControlList(text));
        } else if ("aclAdministerReservations".equals(field.getTagName())) {
            String text = ((Text) field.getFirstChild()).getData();
            racls.put(ReservationACL.ADMINISTER_RESERVATIONS, new AccessControlList(text));
        } else if ("aclListReservations".equals(field.getTagName())) {
            String text = ((Text) field.getFirstChild()).getData();
            racls.put(ReservationACL.LIST_RESERVATIONS, new AccessControlList(text));
        } else if ("aclSubmitReservations".equals(field.getTagName())) {
            String text = ((Text) field.getFirstChild()).getData();
            racls.put(ReservationACL.SUBMIT_RESERVATIONS, new AccessControlList(text));
        } else if ("reservation".equals(field.getTagName())) {
            isReservable = true;
            reservableQueues.add(queueName);
            configuredQueues.get(FSQueueType.PARENT).add(queueName);
        } else if ("allowPreemptionFrom".equals(field.getTagName())) {
            String text = ((Text) field.getFirstChild()).getData().trim();
            if (!Boolean.parseBoolean(text)) {
                nonPreemptableQueues.add(queueName);
            }
        } else if ("queue".endsWith(field.getTagName()) || "pool".equals(field.getTagName())) {
            loadQueue(queueName, field, minQueueResources, maxQueueResources, maxChildQueueResources, queueMaxApps, userMaxApps, queueMaxAMShares, queueWeights, queuePolicies, minSharePreemptionTimeouts, fairSharePreemptionTimeouts, fairSharePreemptionThresholds, queueAcls, resAcls, configuredQueues, reservableQueues, nonPreemptableQueues);
            isLeaf = false;
        }
    }
    // then store it as a parent queue
    if (isLeaf && !"parent".equals(element.getAttribute("type"))) {
        configuredQueues.get(FSQueueType.LEAF).add(queueName);
    } else {
        if (isReservable) {
            throw new AllocationConfigurationException("The configuration settings" + " for " + queueName + " are invalid. A queue element that " + "contains child queue elements or that has the type='parent' " + "attribute cannot also include a reservation element.");
        }
        configuredQueues.get(FSQueueType.PARENT).add(queueName);
    }
    // The root queue defaults to all access
    for (QueueACL acl : QueueACL.values()) {
        AccessType accessType = SchedulerUtils.toAccessType(acl);
        if (acls.get(accessType) == null) {
            AccessControlList defaultAcl = queueName.equals(ROOT) ? EVERYBODY_ACL : NOBODY_ACL;
            acls.put(accessType, defaultAcl);
        }
    }
    queueAcls.put(queueName, acls);
    resAcls.put(queueName, racls);
    if (maxQueueResources.containsKey(queueName) && minQueueResources.containsKey(queueName) && !Resources.fitsIn(minQueueResources.get(queueName), maxQueueResources.get(queueName))) {
        LOG.warn(String.format("Queue %s has max resources %s less than " + "min resources %s", queueName, maxQueueResources.get(queueName), minQueueResources.get(queueName)));
    }
}
Also used : AccessControlList(org.apache.hadoop.security.authorize.AccessControlList) HashMap(java.util.HashMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Resource(org.apache.hadoop.yarn.api.records.Resource) QueueACL(org.apache.hadoop.yarn.api.records.QueueACL) Text(org.w3c.dom.Text) ResourceWeights(org.apache.hadoop.yarn.server.resourcemanager.resource.ResourceWeights) ReservationACL(org.apache.hadoop.yarn.api.records.ReservationACL) AccessType(org.apache.hadoop.yarn.security.AccessType)

Example 5 with Text

use of org.w3c.dom.Text in project che by eclipse.

the class ClasspathEntry method decodeUnknownNode.

private static void decodeUnknownNode(Node node, XMLWriter xmlWriter, boolean insertNewLine) {
    switch(node.getNodeType()) {
        case Node.ELEMENT_NODE:
            NamedNodeMap attributes;
            HashMap parameters = null;
            if ((attributes = node.getAttributes()) != null) {
                int length = attributes.getLength();
                if (length > 0) {
                    parameters = new HashMap();
                    for (int i = 0; i < length; i++) {
                        Node attribute = attributes.item(i);
                        parameters.put(attribute.getNodeName(), attribute.getNodeValue());
                    }
                }
            }
            NodeList children = node.getChildNodes();
            int childrenLength = children.getLength();
            String nodeName = node.getNodeName();
            xmlWriter.printTag(nodeName, parameters, false, /*don't insert tab*/
            false, /*don't insert new line*/
            childrenLength == 0);
            if (childrenLength > 0) {
                for (int i = 0; i < childrenLength; i++) {
                    decodeUnknownNode(children.item(i), xmlWriter, false);
                }
                xmlWriter.endTag(nodeName, false, /*don't insert tab*/
                insertNewLine);
            }
            break;
        case Node.TEXT_NODE:
            String data = ((Text) node).getData();
            xmlWriter.printString(data, false, /*don't insert tab*/
            false);
            break;
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) HashMap(java.util.HashMap) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Text(org.w3c.dom.Text)

Aggregations

Text (org.w3c.dom.Text)144 Element (org.w3c.dom.Element)82 Document (org.w3c.dom.Document)51 Node (org.w3c.dom.Node)48 NodeList (org.w3c.dom.NodeList)27 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)20 DocumentBuilder (javax.xml.parsers.DocumentBuilder)19 Attr (org.w3c.dom.Attr)12 StringReader (java.io.StringReader)10 InputSource (org.xml.sax.InputSource)10 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)9 DOMException (org.w3c.dom.DOMException)7 IOException (java.io.IOException)6 Test (org.junit.Test)6 HashMap (java.util.HashMap)4 DOMSource (javax.xml.transform.dom.DOMSource)4 DocumentFragment (org.w3c.dom.DocumentFragment)4 NamedNodeMap (org.w3c.dom.NamedNodeMap)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3