List of usage examples for org.dom4j Element getQualifiedName
String getQualifiedName();
From source file:com.alibaba.citrus.dev.handler.util.DomUtil.java
License:Open Source License
private static Element copy(org.dom4j.Element dom4jElement, ElementFilter filter) throws Exception { dom4jElement = filter.filter(dom4jElement); if (dom4jElement == null) { return null; }// w w w . j a v a2 s . co m Element element = new Element(dom4jElement.getQualifiedName(), dom4jElement.getNamespaceURI()); for (Object attr : dom4jElement.attributes()) { String name = ((Attribute) attr).getQualifiedName(); String value = ((Attribute) attr).getValue(); element.addAttribute(name, value); } for (Object ns : dom4jElement.declaredNamespaces()) { String name = ((Namespace) ns).getPrefix(); String value = ((Namespace) ns).getURI(); if (isEmpty(name)) { name = "xmlns"; } else { name = "xmlns:" + name; } element.addAttribute(name, value); } for (Object e : dom4jElement.elements()) { Element subElement = copy((org.dom4j.Element) e, filter); if (subElement != null) { element.addSubElement(subElement); } } if (dom4jElement.elements().isEmpty()) { String text = trimToNull(dom4jElement.getText()); if (text != null) { element.setText(text); } } return element; }
From source file:com.christophermrossi.jpt.PageTemplateImpl.java
License:Open Source License
private void processElement(Element element, ContentHandler contentHandler, LexicalHandler lexicalHandler, Interpreter beanShell, Stack slotStack) throws SAXException, PageTemplateException, IOException { // Get attributes Expressions expressions = new Expressions(); AttributesImpl attributes = getAttributes(element, expressions); // Process instructions // evaluate expression if (expressions.evaluate != null) { Expression.evaluate(expressions.evaluate, beanShell); }/*from w w w. j a v a 2 s . c o m*/ // use macro if (expressions.useMacro != null) { processMacro(expressions.useMacro, element, contentHandler, lexicalHandler, beanShell, slotStack); return; } // fill slot if (expressions.defineSlot != null) { //System.err.println( "fill slot: " + expressions.defineSlot ); if (!slotStack.isEmpty()) { Map slots = (Map) slotStack.pop(); Slot slot = (Slot) slots.get(expressions.defineSlot); //System.err.println( "slot: " + slot ); if (slot != null) { slot.process(contentHandler, lexicalHandler, beanShell, slotStack); slotStack.push(slots); return; } // else { use content in macro } slotStack.push(slots); } else { throw new PageTemplateException("slot definition not allowed outside of macro"); } } // define if (expressions.define != null) { processDefine(expressions.define, beanShell); } // condition if (expressions.condition != null && !Expression.evaluateBoolean(expressions.condition, beanShell)) { // Skip this element (and children) return; } // repeat Loop loop = new Loop(expressions.repeat, beanShell); while (loop.repeat(beanShell)) { // content or replace Object jptContent = null; if (expressions.content != null) { jptContent = processContent(expressions.content, beanShell); } // attributes if (expressions.attributes != null) { processAttributes(attributes, expressions.attributes, beanShell); } // omit-tag boolean jptOmitTag = false; if (expressions.omitTag != null) { if (expressions.omitTag.equals("")) { jptOmitTag = true; } else { jptOmitTag = Expression.evaluateBoolean(expressions.omitTag, beanShell); } } // Declare element Namespace namespace = element.getNamespace(); if (!jptOmitTag) { contentHandler.startElement(namespace.getURI(), element.getName(), element.getQualifiedName(), attributes); } // Process content if (jptContent != null && jptContent != DEFAULT) { // Content for this element has been generated dynamically if (jptContent instanceof HTMLFragment) { HTMLFragment html = (HTMLFragment) jptContent; html.toXhtml(contentHandler, lexicalHandler); } // plain text else { char[] text = ((String) jptContent).toCharArray(); contentHandler.characters(text, 0, text.length); } } else { defaultContent(element, contentHandler, lexicalHandler, beanShell, slotStack); } // End element if (!jptOmitTag) { contentHandler.endElement(namespace.getURI(), element.getName(), element.getQualifiedName()); } } }
From source file:com.cladonia.xml.xdiff.XmlElementNode.java
License:Open Source License
protected Line parseStartTag(Vector lines, Line current, Element elem) { boolean localInsertElement = false; boolean localDeleteElement = false; StyledElement styledElement = new StyledElement(); if (insertElement || insideInsertChain(parent)) { localInsertElement = true;/*w w w .j a va 2 s . c om*/ styledElement.addString(INSERT_OPEN_BRACKET); currentColor = COLOR_GREEN; } else if (deleteElement || insideDeleteChain(parent)) { localDeleteElement = true; styledElement.addString(DELETE_OPEN_BRACKET); currentColor = Color.RED; } else if (merged || insideInsertMergeChain(getXmlElementNodeParent())) { styledElement.addString(MERGED_OPEN_BRACKET); currentColor = COLOR_MERGED; } else { styledElement.addString(OPEN_BRACKET); currentColor = Color.BLACK; } styledElement.addString(new ElementName(elem.getQualifiedName())); current.addStyledElement(styledElement); Namespace ns = elem.getNamespace(); if (ns != null) { XElement parent = (XElement) elem.getParent(); if (parent != null) { Namespace prev = parent.getNamespaceForPrefix(ns.getPrefix()); if (prev == null || !ns.equals(prev)) { StyledElement sns = formatNamespace(ns); if (sns != null) { if (current.length() + sns.length() + 1 > MAX_LINE_LENGTH) { current = new Line(); lines.add(current); current.addStyledString(TAB); } else { current.addStyledString(SPACE); } current.addStyledElement(sns); } } } else { StyledElement sns = formatNamespace(ns); if (sns != null) { if (current.length() + sns.length() + 1 > MAX_LINE_LENGTH) { current = new Line(); lines.add(current); current.addStyledString(TAB); } else { current.addStyledString(SPACE); } current.addStyledElement(sns); } } } List namespaces = elem.additionalNamespaces(); if (namespaces != null && namespaces.size() > 0) { Iterator iterator = namespaces.iterator(); for (int i = 0; i < namespaces.size(); i++) { StyledElement sns = formatNamespace((Namespace) iterator.next()); if (sns != null) { if (current.length() + sns.length() + 1 > MAX_LINE_LENGTH) { current = new Line(); lines.add(current); current.addStyledString(TAB); } else { current.addStyledString(SPACE); } current.addStyledElement(sns); } } } List attributes = elem.attributes(); if (attributes != null && attributes.size() > 0) { Iterator iterator = attributes.iterator(); for (int i = 0; i < attributes.size(); i++) { StyledElement sa = formatAttribute((Attribute) iterator.next()); if (current.length() + sa.length() + 1 > MAX_LINE_LENGTH) { current = new Line(); lines.add(current); current.addStyledString(TAB); } else { current.addStyledString(SPACE); } current.addStyledElement(sa); } } if (!elem.hasContent() || hasPIorWhiteSpaceOnly((XElement) elem)) { if (updateElementFrom != null) { // content was blanked, don't add closing slash } else if (localInsertElement) { current.addStyledString(INSERT_SLASH); } else if (localDeleteElement) { current.addStyledString(DELETE_SLASH); } else if (merged || insideInsertMergeChain(getXmlElementNodeParent())) { current.addStyledString(MERGED_SLASH); } else { current.addStyledString(SLASH); } } if (localInsertElement) { current.addStyledString(INSERT_CLOSE_BRACKET); } else if (localDeleteElement) { current.addStyledString(DELETE_CLOSE_BRACKET); } else if (merged || insideInsertMergeChain(getXmlElementNodeParent())) { current.addStyledString(MERGED_CLOSE_BRACKET); } else { current.addStyledString(CLOSE_BRACKET); } currentColor = Color.BLACK; return current; }
From source file:com.cladonia.xml.XMLFormatter.java
License:Mozilla Public License
protected void writeElement(Element element) throws IOException { if (DEBUG)//from w w w .j ava 2 s .c o m System.out.println("XMLFormatter.writeElement( " + element + ")"); // if ( indentMixed) { // super.writeElement( element); // } else { int size = element.nodeCount(); String qualifiedName = element.getQualifiedName(); boolean hasElement = false; boolean hasText = false; // first test to see if this element has mixed content, // if whitespace is significant ... for (int i = 0; i < size; i++) { Node node = element.node(i); if (node instanceof Element) { hasElement = true; } else if (node instanceof Text) { String text = node.getText(); if (text != null && text.trim().length() > 0) { hasText = true; } } } Attribute space = element.attribute("space"); boolean preserveSpace = false; if (space != null) { String prefix = space.getNamespacePrefix(); String value = space.getValue(); // System.out.println( "prefix = "+prefix+" value = "+value); if (prefix != null && "xml".equals(prefix) && "preserve".equals(value)) { preserveSpace = true; } } writePrintln(); indent(); writer.write("<"); writer.write(qualifiedName); int previouslyDeclaredNamespaces = namespaceStack.size(); Namespace ns = element.getNamespace(); if (isNamespaceDeclaration(ns)) { namespaceStack.push(ns); writeNamespace(ns); } // Print out additional namespace declarations for (int i = 0; i < size; i++) { Node node = element.node(i); if (node instanceof Namespace) { Namespace additional = (Namespace) node; if (isNamespaceDeclaration(additional)) { namespaceStack.push(additional); writeNamespace(additional); } } } writeAttributes(element); lastOutputNodeType = Node.ELEMENT_NODE; if (size <= 0) { writeEmptyElementClose(qualifiedName); } else { writer.write(">"); if (!hasElement && !preserveSpace) { // text only // we have at least one text node so lets assume // that its non-empty // System.out.println( "writeElementContent (Text) ..."); boolean previousWrapText = wrapText; wrapText = true; writeElementContent(element); wrapText = previousWrapText; } else if (preserveMixedContent && hasElement && hasText) { // preserve space // Mixed content // System.out.println( "writeMixedElementContent ..."); Node lastNode = writeMixedElementContent(element); } else if (preserveSpace) { // preserve space // Mixed content // System.out.println( "writePreserveElementContent ..."); Node lastNode = writePreservedElementContent(element); } else { // hasElement && !hasText // System.out.println( "writeElementContent (Element) ..."); boolean previousWrapText = wrapText; wrapText = true; ++indentLevel; writeElementContent(element); --indentLevel; wrapText = previousWrapText; writePrintln(); indent(); } writer.write("</"); writer.write(qualifiedName); writer.write(">"); // writePrintln(); } // remove declared namespaceStack from stack while (namespaceStack.size() > previouslyDeclaredNamespaces) { namespaceStack.pop(); } lastOutputNodeType = Node.ELEMENT_NODE; // } }
From source file:com.cladonia.xml.XMLFormatter.java
License:Mozilla Public License
protected void writeMixedElement(Element element) throws IOException { if (DEBUG)/*from www. j av a2s . c om*/ System.out.println("XMLFormatter.writeMixedElement( " + element + ")"); int size = element.nodeCount(); String qualifiedName = element.getQualifiedName(); boolean previousTrim = format.isTrimText(); boolean previousWrapText = wrapText; if (isPreserveMixedContent()) { format.setTrimText(false); wrapText = false; } else { wrapText = true; } writer.write("<"); writer.write(qualifiedName); int previouslyDeclaredNamespaces = namespaceStack.size(); Namespace ns = element.getNamespace(); if (isNamespaceDeclaration(ns)) { namespaceStack.push(ns); writeNamespace(ns); } // Print out additional namespace declarations boolean textOnly = true; for (int i = 0; i < size; i++) { Node node = element.node(i); if (node instanceof Namespace) { Namespace additional = (Namespace) node; if (isNamespaceDeclaration(additional)) { namespaceStack.push(additional); writeNamespace(additional); } } else if (node instanceof Element) { textOnly = false; } } writeAttributes(element); lastOutputNodeType = Node.ELEMENT_NODE; if (size <= 0) { writeEmptyElementClose(qualifiedName); // writePrintln(); } else { writer.write(">"); writeMixedElementContent(element); writer.write("</"); writer.write(qualifiedName); writer.write(">"); } // remove declared namespaceStack from stack while (namespaceStack.size() > previouslyDeclaredNamespaces) { namespaceStack.pop(); } lastOutputNodeType = Node.ELEMENT_NODE; format.setTrimText(previousTrim); wrapText = previousWrapText; }
From source file:com.cladonia.xml.XMLFormatter.java
License:Mozilla Public License
protected void writePreservedElement(Element element) throws IOException { if (DEBUG)// w ww. j ava2 s .c o m System.out.println("XMLFormatter.writePreservedElement( " + element + ")"); int size = element.nodeCount(); String qualifiedName = element.getQualifiedName(); boolean previousTrim = format.isTrimText(); boolean previousWrapText = wrapText; format.setTrimText(false); wrapText = false; boolean hasElement = false; boolean hasText = false; // first test to see if this element has mixed content, // if whitespace is significant ... for (int i = 0; i < size; i++) { Node node = element.node(i); if (node instanceof Element) { hasElement = true; } else if (node instanceof Text) { String text = node.getText(); if (text != null && text.trim().length() > 0) { hasText = true; } } } Attribute space = element.attribute("space"); boolean defaultSpace = false; if (space != null) { String prefix = space.getNamespacePrefix(); String value = space.getValue(); // System.out.println( "prefix = "+prefix+" value = "+value); if (prefix != null && "xml".equals(prefix) && "default".equals(value)) { defaultSpace = true; } } writer.write("<"); writer.write(qualifiedName); int previouslyDeclaredNamespaces = namespaceStack.size(); Namespace ns = element.getNamespace(); if (isNamespaceDeclaration(ns)) { namespaceStack.push(ns); writeNamespace(ns); } // Print out additional namespace declarations for (int i = 0; i < size; i++) { Node node = element.node(i); if (node instanceof Namespace) { Namespace additional = (Namespace) node; if (isNamespaceDeclaration(additional)) { namespaceStack.push(additional); writeNamespace(additional); } } } writeAttributes(element); lastOutputNodeType = Node.ELEMENT_NODE; if (size <= 0) { writeEmptyElementClose(qualifiedName); } else { writer.write(">"); if (preserveMixedContent && hasElement && hasText) { // mixed content // System.out.println( "writeMixedElementContent ..."); Node lastNode = writeMixedElementContent(element); } else if (!defaultSpace) { // preserve space // System.out.println( "writePreservedElementContent ..."); Node lastNode = writePreservedElementContent(element); } else { // System.out.println( "writeElementContent ..."); format.setTrimText(isTrimText()); boolean prevWrapText = wrapText; wrapText = true; writeElementContent(element); wrapText = prevWrapText; format.setTrimText(false); } writer.write("</"); writer.write(qualifiedName); writer.write(">"); } // remove declared namespaceStack from stack while (namespaceStack.size() > previouslyDeclaredNamespaces) { namespaceStack.pop(); } lastOutputNodeType = Node.ELEMENT_NODE; format.setTrimText(previousTrim); wrapText = previousWrapText; }
From source file:com.creativity.controller.CepWebService.java
@SuppressWarnings("rawtypes") public CepWebService(String cep) { try {/*from w w w . j a v a 2s . co m*/ URL url = new URL("http://cep.republicavirtual.com.br/web_cep.php?cep=" + cep + "&formato=xml"); Document document = getDocumento(url); Element root = document.getRootElement(); for (Iterator i = root.elementIterator(); i.hasNext();) { Element element = (Element) i.next(); if (element.getQualifiedName().equals("uf")) { setEstado(element.getText()); } if (element.getQualifiedName().equals("cidade")) { setCidade(element.getText()); } if (element.getQualifiedName().equals("bairro")) { setBairro(element.getText()); } if (element.getQualifiedName().equals("tipo_logradouro")) { setTipoLogradouro(element.getText()); } if (element.getQualifiedName().equals("logradouro")) { setLogradouro(element.getText()); } if (element.getQualifiedName().equals("resultado")) { setResultado(Integer.parseInt(element.getText())); } } } catch (Exception ex) { ex.printStackTrace(); } }
From source file:com.globalsight.everest.segmentationhelper.XmlLoader.java
License:Apache License
public static SegmentationRule parseSegmentationRule(String p_xmlText) throws Exception { Document doc = parseWithSAX(p_xmlText); Element root = doc.getRootElement(); SegmentationRule segmentationRule = new SegmentationRule(); SrxHeader header = new SrxHeader(); HashMap<String, String> formathandle = new HashMap<String, String>(); HashMap<String, ArrayList<Rule>> langrules = new HashMap<String, ArrayList<Rule>>(); ArrayList<LanguageMap> langruleMap = new ArrayList<LanguageMap>(); segmentationRule.setRootName(root.getQualifiedName()); // Now it only supports SRX2.0. segmentationRule.setVersion(root.attributeValue("version")); Element headerElement = root.element("header"); Element bodyElement = root.element("body"); Element maprulesElement = bodyElement.element("maprules"); // to get SRX header informations if (headerElement != null) { // option segmentsubflows String segsub = headerElement.attributeValue("segmentsubflows"); if (segsub == null) { // Take default value. header.isSegmentsubflows(true); } else {//from w w w . j a v a 2s . co m header.isSegmentsubflows(!segsub.equalsIgnoreCase("no")); } // option cascade String cascade = headerElement.attributeValue("cascade"); if (cascade == null) { header.isCascade(false); } else { header.isCascade(!cascade.equalsIgnoreCase("no")); } // elements : formathandle List formatList = headerElement.elements("formathandle"); Iterator formatIter = formatList != null ? formatList.iterator() : (new ArrayList()).iterator(); // If the header does not contain formathandle elements // we use the default values if (!formatIter.hasNext()) { formathandle.put("start", "no"); formathandle.put("end", "yes"); formathandle.put("isolated", "no"); } // If the header contains formathandle elements // we use the values specified by formathandle elements else { while (formatIter.hasNext()) { Element formatElement = (Element) formatIter.next(); String type = formatElement.attributeValue("type"); String include = formatElement.attributeValue("include"); formathandle.put(type, include); } } header.setFormatHandle(formathandle); // okpsrx extension Element okpOptions = headerElement.element("options"); Element okpSample = headerElement.element("sample"); Element okpRangeRule = headerElement.element("rangeRule"); if (okpOptions != null) { String oneSegmentIncludesAll = okpOptions.attributeValue("oneSegmentIncludesAll"); String trimLeadingWs = okpOptions.attributeValue("trimLeadingWhitespaces"); String trimTrailingWs = okpOptions.attributeValue("trimTrailingWhitespaces"); header.setOneSegmentIncludesAll("yes".equalsIgnoreCase(oneSegmentIncludesAll)); header.setTrimLeadingWhitespaces("yes".equalsIgnoreCase(trimLeadingWs)); header.setTrimTrailingWhitespaces("yes".equalsIgnoreCase(trimTrailingWs)); } if (okpSample != null) { String language = okpSample.attributeValue("language"); String useMappedRules = okpSample.attributeValue("useMappedRules"); String sample = okpSample.getText(); header.setSample(sample); header.setSampleLanguage(language); header.setUseMappedRulesForSample(!"no".equalsIgnoreCase(useMappedRules)); } if (okpRangeRule != null) { String rangeRule = okpRangeRule.getText(); header.setRangeRule(rangeRule); } } if (bodyElement != null) { Element languagerulesElement = bodyElement.element("languagerules"); if (languagerulesElement != null) { Iterator languageruleIter = languagerulesElement.elementIterator(); while (languageruleIter.hasNext()) { Element languageruleElement = (Element) languageruleIter.next(); String languageName = languageruleElement.attributeValue("languagerulename"); Iterator ruleIter = languageruleElement.elementIterator(); ArrayList<Rule> rules = new ArrayList<Rule>(); while (ruleIter.hasNext()) { Element ruleSub = (Element) ruleIter.next(); String breakvalue = ruleSub.attributeValue("break"); String beforebreak = ruleSub.elementText("beforebreak"); String afterbreak = ruleSub.elementText("afterbreak"); Rule rule = new Rule(); if (breakvalue == null) { // Take default value. rule.isBreak(true); } else { if (breakvalue.equalsIgnoreCase("no")) { rule.isBreak(false); } else { rule.isBreak(true); } } // System.out.println(rule.getBreak()); rule.setAfterBreak(afterbreak); rule.setBeforeBreak(beforebreak); rules.add(rule); } langrules.put(languageName, rules); } } // end languageruleElement } // end bodyElement if (maprulesElement != null) { Iterator languagemapIter = maprulesElement.elementIterator(); while (languagemapIter.hasNext()) { Element languagemapElement = (Element) languagemapIter.next(); String languagepattern = languagemapElement.attributeValue("languagepattern"); String languagerulename = languagemapElement.attributeValue("languagerulename"); LanguageMap langMap = new LanguageMap(); langMap.setLanguagePattern(languagepattern); langMap.setLanguageruleName(languagerulename); langruleMap.add(langMap); } } segmentationRule.setHeader(header); segmentationRule.setLanguageMap(langruleMap); segmentationRule.setRules(langrules); return segmentationRule; }
From source file:com.mg.framework.support.ui.UIProducer.java
License:Open Source License
private static Element copyElement(Element element, RuntimeMacrosLoader runtimeMacrosLoader) { //if (INCLUDE_TAG_NAME.equals(element.getQualifiedName())) //return includeMacros(); Element result = DocumentHelper.createElement(element.getQualifiedName()); result.setAttributes(element.attributes()); List<Element> childElements = MiscUtils.convertUncheckedList(Element.class, element.elements()); for (Element childElement : childElements) { Element copy;//from w ww. j a v a 2 s. c o m if (childElement.getQualifiedName().equals(INCLUDE_TAG_NAME)) { copy = includeMacros(result, childElement, runtimeMacrosLoader); /*Document macros = null; String runtimeMacros = childElement.attributeValue(RUNTIME_MACROS_NAME_ATTR); if (runtimeMacros == null) macros = loadMacros(childElement.attributeValue(MACROS_NAME_ATTR)); else macros = loadRuntimeMacros(runtimeMacros, runtimeMacrosLoader); String macrosType = macros.getRootElement().getQualifiedName(); if (macrosType.equals(EMPTY_MACROS)) copy = null; //handle special case for empty macros else if (macrosType.equals(WRAP_MACROS)) { //copy macros contents exclude root element List<Element> macrosChildElements = MiscUtils.convertUncheckedList(Element.class, macros.getRootElement().elements()); for (Element macrosChild : macrosChildElements) { Element macrosElement = copyElement(macrosChild, runtimeMacrosLoader); if (macrosElement != null) result.add(macrosElement); } copy = null; } else copy = copyElement(macros.getRootElement(), runtimeMacrosLoader); //copy root element */ } else { copy = copyElement(childElement, runtimeMacrosLoader); } if (copy != null) result.add(copy); } return result; }
From source file:com.mg.framework.support.ui.UIRender.java
License:Open Source License
private Widget createWidget(Element element, Container parent) { Widget result = null;// w ww .java 2s.c o m Label labelFor = null; String elementName = element.getQualifiedName(); String id = element.attributeValue(Widget.WIDGET_ID); result = componentMap.get(id); if (result != null) throw new ApplicationException( Messages.getInstance().getMessage(Messages.DUPLICATE_WIDGET_NAME_ERROR, new Object[] { id })); boolean scroll = XMLUtils.checkBoolean(element.attributeValue(ScrollPane.SCROLL), false); if (elementName.equals(FIELD_WIDGET)) { result = createFieldWidget(id, element); labelFor = (Label) componentMap.get(generateLabelForName(id)); // ? } else if (elementName.equals(CONTAINER_WIDGET)) { String layout = element.attributeValue(Container.LAYOUT, StringUtils.EMPTY_STRING); result = widgetFactory.createWidget(layout, id, element, view); labelFor = createLabelFor(element, id, result); } else { result = widgetFactory.createWidget(elementName, id, element, view); labelFor = createLabelFor(element, id, result); } result.init(element); //? ? ? //?. https://issues.m-g.ru/bugzilla/show_bug.cgi?id=4191 if (labelFor != null && !result.isVisible()) labelFor.setVisible(false); Widget tmpResult = null; if (scroll || result.isScrollable()) { tmpResult = result; result = widgetFactory.createWidget(WidgetFactory.SCROLL_PANE, generateScrollName(id), null, view); ((ScrollPane) result).setViewPortView(tmpResult); } try { parent.add(result, labelFor, element); } finally { if (tmpResult != null) result = tmpResult; } componentMap.put(id, result); return result; }