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);
}
}
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;
}
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());
}
}
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)));
}
}
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;
}
}
Aggregations