/** * Returns the text representation of the generated schema. */ @Override public String toString() { return XMLUtil.toString(schema); }
/** * Parses XML from a file. * * @param filePath Full path to a file containing valid XML. * @return XML document. * @throws Exception Unspecified exception. */ public static Document parseXMLFromLocation(String filePath) throws Exception { return parseXMLFromStream(new FileInputStream(filePath)); }
/** * Returns the formatted name for the node. * * @param node Node to format. * @param format Desired format (opening tag, closing tag, empty tag, or both). * @return Formatted name. */ public static String formatNodeName(Node node, TagFormat format) { StringBuilder sb = new StringBuilder((format == TagFormat.CLOSING ? "</" : "<") + node.getNodeName()); if (format != TagFormat.CLOSING) { sb.append(formatAttributes(node)); } sb.append(format == TagFormat.EMPTY ? " />" : ">"); if (format == TagFormat.BOTH) { sb.append(formatNodeName(node, TagFormat.CLOSING)); } return sb.toString(); }
/** * Returns the layout as an xml-formatted string. */ @Override public String toString() { if (root == null) { return ""; } try { Document doc = XMLUtil.parseXMLFromString("<layout/>"); Element node = doc.getDocumentElement(); buildDocument(node, root); LayoutUtil.copyAttributes(root.getAttributes(), node); node.setAttribute("version", LAYOUT_VERSION); node.setAttribute("name", layoutName); return XMLUtil.toString(doc); } catch (Exception e) { throw MiscUtil.toUnchecked(e); } }
/** * Parses XML from a list of strings. * * @param xml String iterable containing valid XML. * @return XML document. * @throws Exception Unspecified exception. */ public static Document parseXMLFromList(Iterable<String> xml) throws Exception { return parseXMLFromString(StrUtil.fromList(xml)); }
/** * Parses XML from an input source. * * @param source An input source containing valid XML. * @return XML document. * @throws Exception Unspecified exception. */ public static Document parseXMLFromSource(InputSource source) throws Exception { return newDocumentBuilder(false).parse(source); }
@Override public Treenode render(Node node) { Treenode tnode = new Treenode(); if (node.getNodeType() == Node.TEXT_NODE) { setLabel(tnode, node.getNodeValue(), XMLConstants.STYLE_CONTENT); } else if (node.getNodeType() == Node.COMMENT_NODE) { setLabel(tnode, "<!--" + node.getNodeValue() + "-->", XMLConstants.STYLE_COMMENT); } else if (node.getParentNode() == null) { // Closing tag tnode.setAttribute("closing", true); setLabel(tnode, XMLUtil.formatNodeName(node, TagFormat.CLOSING), XMLConstants.STYLE_TAG); } else { boolean leaf = !node.hasChildNodes(); String label = "<" + node.getNodeName(); NamedNodeMap attrs = node.getAttributes(); if (attrs != null) { for (int i = 0; i < attrs.getLength(); i++) { Node attr = attrs.item(i); label += " " + attr.getNodeName() + "='" + attr.getNodeValue() + "'"; } } setLabel(tnode, label + (leaf ? " />" : ">"), XMLConstants.STYLE_TAG); tnode.addEventListener("toggle", nodeListener); } return tnode; }
/** * Parse the layout from XML content. * * @param xml The XML content to parse. * @return The root layout element. */ public static Layout parseText(String xml) { try { return parseDocument(XMLUtil.parseXMLFromString(xml)); } catch (Exception e) { throw MiscUtil.toUnchecked(e); } }
/** * Parses XML from an input stream. * * @param stream Input stream containing valid XML. * @return XML document. * @throws Exception Unspecified exception. */ public static Document parseXMLFromStream(InputStream stream) throws Exception { Document document = newDocumentBuilder(false).parse(stream); stream.close(); return document; }
.trimToNull(XMLUtil.parseXMLFromString(sb.toString()).getDocumentElement().getTextContent());
/** * Converts an XML document to a formatted XML string. * * @param doc The document to format. * @return Formatted XML document. */ public static String toString(Document doc) { return toString(doc, 4); }
/** * Parses XML from a string. * * @param xml String containing valid XML. * @return XML document. * @throws Exception Unspecified exception. */ public static Document parseXMLFromString(String xml) throws Exception { return parseXMLFromStream(IOUtils.toInputStream(xml, StandardCharsets.UTF_8)); }
/** * Returns the FSP as an XML document. * * @return The FSP as an XML document. */ public Document getDocument() { if (document == null) { try (InputStream is = stream) { stream = null; document = XMLUtil.newDocumentBuilder(true).parse(is); } catch (Exception e) { throw new ParserException(e, "Exception parsing resource \"%s\"", source); } } return document; }
/** * Parses an xml extension from an xml string. * * @param xml XML containing the extension. * @param tagName The top level tag name. * @return Result of the parsed extension. * @throws Exception Unspecified exception. */ protected Object fromXml(String xml, String tagName) throws Exception { Document document = XMLUtil.parseXMLFromString(xml); NodeList nodeList = document.getElementsByTagName(tagName); if (nodeList == null || nodeList.getLength() != 1) { throw new DOMException(DOMException.NOT_FOUND_ERR, "Top level tag '" + tagName + "' was not found."); } Element element = (Element) nodeList.item(0); Class<?> beanClass = getBeanClass(element); BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(beanClass); doParse(element, builder); DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); factory.setParentBeanFactory(SpringUtil.getAppContext()); AbstractBeanDefinition beanDefinition = builder.getBeanDefinition(); factory.registerBeanDefinition(tagName, beanDefinition); return factory.getBean(tagName); }
/** * Returns an XML-formatted string that mirrors the CWF component tree starting at the specified * root. * * @param root BaseComponent whose subtree is to be traversed. * @param excludedProperties An optional list of properties that should be excluded from the * output. These may either be the property name (e.g., "uuid") or a property name * qualified by a component name (e.g., "window.uuid"). Optionally, an entry may be * followed by an "=" and a value to exclude matches with a specific value. Note that * "innerAttrs" and "outerAttrs" are always excluded. * @return The XML text representation of the component subtree. */ public static String toXML(BaseComponent root, String... excludedProperties) { return XMLUtil.toString(toDocument(root, excludedProperties)); }
/** * Parse layout from an input stream. * * @param stream The input stream. * @return The root layout element. */ public static Layout parseStream(InputStream stream) { try (InputStream is = stream) { return parseDocument(XMLUtil.parseXMLFromStream(is)); } catch (Exception e) { throw MiscUtil.toUnchecked(e); } }
/** * Parse a tag library. * * @param stream Input stream referencing the tag library. * @return The parsed tag library. */ public TagLibrary parse(InputStream stream) { try (InputStream is = stream) { Element root = XMLUtil.parseXMLFromStream(is).getDocumentElement(); String uri = getValue(root, "uri"); NodeList nodes = root.getElementsByTagName("function"); int nodeCount = nodes.getLength(); TagLibrary tagLibrary = new TagLibrary(uri); for (int i = 0; i < nodeCount; i++) { Element ele = (Element) nodes.item(i); String name = getValue(ele, "name"); String clazz = getValue(ele, "function-class"); String signature = getValue(ele, "function-signature"); tagLibrary.addFunction(name, clazz, signature); } return tagLibrary; } catch (Exception e) { throw MiscUtil.toUnchecked(e); } }