List of usage examples for org.jdom2 Element getChildText
public String getChildText(final String cname)
From source file:open.dolphin.adm10.rest.AbstractResource.java
/** * ?????// w w w .jav a 2s . c om * @param sb content??????StringBuilder * @param current XML? */ protected void writeChildren(StringBuilder sb, Element current) { int eType = -1; String eName = current.getName(); if (eName.equals(PARAGRAPH_NAME)) { eType = TT_PARAGRAPH; startParagraph(sb, current.getAttributeValue(LOGICAL_STYLE_NAME), current.getAttributeValue(ALIGNMENT_NAME)); } else if (eName.equals(CONTENT_NAME) && (current.getChild(TEXT_NAME) != null)) { eType = TT_CONTENT; startContent(sb, current.getAttributeValue(FOREGROUND_NAME), current.getAttributeValue(SIZE_NAME), current.getAttributeValue(BOLD_NAME), current.getAttributeValue(ITALIC_NAME), current.getAttributeValue(UNDERLINE_NAME), current.getChildText(TEXT_NAME)); } else if (eName.equals(COMPONENT_NAME)) { eType = TT_COMPONENT; startComponent(sb, current.getAttributeValue(NAME_NAME), // compoenet=number current.getAttributeValue(COMPONENT_ELEMENT_NAME)); } else if (eName.equals(ICON_NAME)) { eType = TT_ICON; startIcon(sb, current); } else if (eName.equals(PROGRESS_COURSE_NAME)) { eType = TT_PROGRESS_COURSE; startProgressCourse(sb); } else if (eName.equals(SECTION_NAME)) { eType = TT_SECTION; startSection(sb); } else { debug("Other element:" + eName); } // // ?: ???????? // if (eType == TT_PARAGRAPH || eType == TT_PROGRESS_COURSE || eType == TT_SECTION) { java.util.List children = (java.util.List) current.getChildren(); Iterator iterator = children.iterator(); while (iterator.hasNext()) { Element child = (Element) iterator.next(); writeChildren(sb, child); } } switch (eType) { case TT_PARAGRAPH: endParagraph(sb); break; case TT_CONTENT: endContent(sb); break; case TT_ICON: endIcon(sb); break; case TT_COMPONENT: endComponent(sb); break; case TT_PROGRESS_COURSE: endProgressCourse(sb); break; case TT_SECTION: endSection(sb); break; } }
From source file:org.apache.ctakes.core.knowtator.KnowtatorXMLParser.java
License:Apache License
public Collection<KnowtatorAnnotation> parse(URI knowtatorXML) throws JDOMException, IOException { Element annotationsElem = new SAXBuilder().build(knowtatorXML.toURL()).getRootElement(); // parse <annotation> elements Set<String> ignoredAnnotators = new HashSet<String>(); Map<String, KnowtatorAnnotation> annotations = new HashMap<String, KnowtatorAnnotation>(); for (Element annotationElem : annotationsElem.getChildren("annotation")) { for (Element annotatorElem : this.getChild(annotationElem, "annotator")) { String annotatorName = annotatorElem.getText(); if (!this.annotatorNames.contains(annotatorName)) { ignoredAnnotators.add(annotatorName); } else { for (Element mentionElem : this.getChild(annotationElem, "mention")) { for (String id : this.getAttributeValue(mentionElem, "id")) { KnowtatorAnnotation annotation = new KnowtatorAnnotation(); annotation.id = id; annotations.put(id, annotation); List<Element> spanElems = annotationElem.getChildren("span"); if (!spanElems.isEmpty()) { for (Element spannedTextElem : this.getChild(annotationElem, "spannedText")) { annotation.spannedText = spannedTextElem.getText(); }/*from w w w. java 2 s .c o m*/ for (Element spanElem : spanElems) { for (String startStr : this.getAttributeValue(spanElem, "start")) { for (String endStr : this.getAttributeValue(spanElem, "end")) { annotation.addSpan(Integer.parseInt(startStr), Integer.parseInt(endStr)); } } } } } } } } } LOGGER.debug(String.format("Ignored annotators %s in %s", ignoredAnnotators, knowtatorXML)); // parse <stringSlotMention> elements Map<String, Slot<String>> stringSlots = new HashMap<String, Slot<String>>(); for (Element slotMentionElem : annotationsElem.getChildren("stringSlotMention")) { for (IdAndSlot<String> idAndSlot : this.parseSlotMention(slotMentionElem, "stringSlotMentionValue")) { stringSlots.put(idAndSlot.id, idAndSlot.slot); } } // parse <booleanSlotMention> elements Map<String, Slot<Boolean>> booleanSlots = new HashMap<String, Slot<Boolean>>(); for (Element slotMentionElem : annotationsElem.getChildren("booleanSlotMention")) { for (IdAndSlot<String> idAndSlot : this.parseSlotMention(slotMentionElem, "booleanSlotMentionValue")) { Slot<String> slot = idAndSlot.slot; Boolean value = Boolean.parseBoolean(slot.value); booleanSlots.put(idAndSlot.id, new Slot<Boolean>(slot.name, value)); } } // parse <complexSlotMention> elements Map<String, Slot<KnowtatorAnnotation>> mentionSlots = new HashMap<String, Slot<KnowtatorAnnotation>>(); for (Element slotMentionElem : annotationsElem.getChildren("complexSlotMention")) { for (IdAndSlot<String> idAndSlot : this.parseSlotMention(slotMentionElem, "complexSlotMentionValue")) { Slot<String> slot = idAndSlot.slot; KnowtatorAnnotation mention = annotations.get(slot.value); if (mention != null) { mentionSlots.put(idAndSlot.id, new Slot<KnowtatorAnnotation>(slot.name, mention)); } } } // parse <classMention> elements for (Element classMentionElem : annotationsElem.getChildren("classMention")) { for (String id : this.getAttributeValue(classMentionElem, "id")) { KnowtatorAnnotation annotation = annotations.get(id); if (annotation == null) { continue; } annotation.type = classMentionElem.getChildText("mentionClass"); for (Element hasSlotMentionElem : classMentionElem.getChildren("hasSlotMention")) { for (String slotId : this.getAttributeValue(hasSlotMentionElem, "id")) { Slot<String> stringSlot = stringSlots.get(slotId); if (stringSlot != null) { annotation.stringSlots.put(stringSlot.name, stringSlot.value); } else { Slot<Boolean> booleanSlot = booleanSlots.get(slotId); if (booleanSlot != null) { annotation.booleanSlots.put(booleanSlot.name, booleanSlot.value); } else { Slot<KnowtatorAnnotation> mentionSlot = mentionSlots.get(slotId); if (mentionSlot != null) { annotation.annotationSlots.put(mentionSlot.name, mentionSlot.value); } else { LOGGER.warn("no simple slot for " + slotId); } } } } } } } return annotations.values(); }
From source file:org.apache.marmotta.maven.plugins.repochecker.RepositoryCheckerMojo.java
License:Apache License
private ResponseHandler<String> createTimestampHandler(final Artifact artifact) { return new ResponseHandler<String>() { public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException { if (response.getStatusLine().getStatusCode() >= 300) { return null; }//from w w w . ja va 2 s . c o m if (response.getEntity() == null) return null; InputStream content = response.getEntity().getContent(); if (content == null) return null; try { Document doc = new SAXBuilder().build(content); Element meta = doc.getRootElement(); if (!"metadata".equals(meta.getName())) throw new IOException(); Element vers = meta.getChild("versioning"); if (vers == null) throw new IOException(); Element sVers = vers.getChild("snapshotVersions"); if (sVers == null) throw new IOException(); for (Element sv : sVers.getChildren("snapshotVersion")) { // if there is a classifier, check if it's the right one if (artifact.hasClassifier()) { if (!artifact.getClassifier().equals(sv.getChildText("classifier"))) { continue; } } if (!artifact.getType().equals(sv.getChildText("extension"))) { continue; } // If we reach this, then it's the right snapshotVersion StringBuilder sb = new StringBuilder(artifact.getArtifactId()); sb.append("-").append(sv.getChildText("value")); if (artifact.hasClassifier()) sb.append("-").append(sv.getChildText("classifier")); sb.append(".").append(sv.getChildText("extension")); return sb.toString(); } return null; } catch (JDOMException e) { throw new IOException(e); } } }; }
From source file:org.apache.roller.weblogger.business.themes.ThemeMetadataParser.java
License:Apache License
/** * Unmarshall the given input stream into our defined * set of Java objects.// www .ja v a2 s. c o m **/ public ThemeMetadata unmarshall(InputStream instream) throws ThemeParsingException, IOException, JDOMException { if (instream == null) { throw new IOException("InputStream is null!"); } ThemeMetadata theme = new ThemeMetadata(); SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(instream); // start at root and get theme id, name, description and author Element root = doc.getRootElement(); theme.setId(root.getChildText("id")); theme.setName(root.getChildText("name")); theme.setDescription(root.getChildText("description")); theme.setAuthor(root.getChildText("author")); // dual-theme (standard & mobile) or one-theme-fits-all? theme.setDualTheme("true".equalsIgnoreCase(root.getChildText("dualTheme"))); // if either id or name is null then throw a parsing exception if (StringUtils.isEmpty(theme.getId()) || StringUtils.isEmpty(theme.getName())) { throw new ThemeParsingException("'id' and 'name' are required theme elements"); } // now grab the preview image path Element previewImage = root.getChild("preview-image"); if (previewImage != null) { theme.setPreviewImage(previewImage.getAttributeValue("path")); } else { throw new ThemeParsingException("No preview image specified"); } // grab the stylesheet if it exists Element stylesheet = root.getChild("stylesheet"); if (stylesheet != null) { theme.setStylesheet(elementToStylesheet(stylesheet)); } // now grab the static resources List<Element> resources = root.getChildren("resource"); for (Element resource : resources) { theme.addResource(resource.getAttributeValue("path")); } // now grab the templates boolean weblogActionTemplate = false; List<Element> templates = root.getChildren("template"); for (Element template : templates) { ThemeMetadataTemplate tmpl = elementToTemplateMetadata(template); theme.addTemplate(tmpl); if (ComponentType.WEBLOG.equals(tmpl.getAction())) { weblogActionTemplate = true; } } // make sure all required elements are present and values are valid // check that there is a template with action='weblog' if (!weblogActionTemplate) { throw new ThemeParsingException("did not find a template of action = 'weblog'"); } return theme; }
From source file:org.apache.roller.weblogger.business.themes.ThemeMetadataParser.java
License:Apache License
private ThemeMetadataTemplate elementToTemplateMetadata(Element element) throws ThemeParsingException { ThemeMetadataTemplate template = new ThemeMetadataTemplate(); template.setName(element.getChildText("name")); template.setDescription(element.getChildText("description")); template.setLink(element.getChildText("link")); template.setContentType(element.getChildText("contentType")); String actionString = element.getAttributeValue("action"); if (StringUtils.isEmpty(actionString)) { throw new ThemeParsingException("Template must contain an 'action' element"); } else {//from w w w . j a v a 2s. co m try { template.setAction(ComponentType.valueOf(actionString.toUpperCase())); } catch (IllegalArgumentException e) { throw new ThemeParsingException("Unknown template action value '" + actionString + "'"); } } //parsing template code segment List<Element> renditionList = element.getChildren("rendition"); for (Element renditionElement : renditionList) { ThemeMetadataTemplateRendition rendition = new ThemeMetadataTemplateRendition(); String renditionValue = renditionElement.getAttributeValue("type"); if (renditionValue != null) { try { rendition.setType(RenditionType.valueOf(renditionValue.toUpperCase())); } catch (IllegalArgumentException e) { throw new ThemeParsingException("Invalid rendition type " + renditionValue + " found."); } } else { // default to standard if type not provided, as most templates are single-rendition rendition.setType(RenditionType.STANDARD); } String templateString = renditionElement.getChildText("templateLanguage"); if (StringUtils.isEmpty(templateString)) { throw new ThemeParsingException("rendition must contain a 'templateLanguage' element"); } else { try { rendition.setTemplateLang(TemplateLanguage.valueOf(templateString.toUpperCase())); } catch (IllegalArgumentException e) { throw new ThemeParsingException("Unknown templateLanguage value '" + templateString + "'"); } } rendition.setContentsFile(renditionElement.getChildText("contentsFile")); if (StringUtils.isEmpty(rendition.getContentsFile())) { throw new ThemeParsingException("Rendition must contain a 'contentsFile' element"); } template.addTemplateRendition(rendition); } String navbar = element.getChildText("navbar"); if ("true".equalsIgnoreCase(navbar)) { template.setNavbar(true); } String hidden = element.getChildText("hidden"); if ("true".equalsIgnoreCase(hidden)) { template.setHidden(true); } // validate template if (StringUtils.isEmpty(template.getName())) { throw new ThemeParsingException("templates must contain a 'name' element"); } return template; }
From source file:org.apache.roller.weblogger.business.themes.ThemeMetadataParser.java
License:Apache License
private ThemeMetadataTemplate elementToStylesheet(Element element) throws ThemeParsingException { ThemeMetadataTemplate template = new ThemeMetadataTemplate(); template.setName(element.getChildText("name")); template.setDescription(element.getChildText("description")); template.setLink(element.getChildText("link")); template.setContentType(element.getChildText("contentType")); template.setAction(ComponentType.STYLESHEET); // parsing rendition segment List<Element> renditionList = element.getChildren("rendition"); for (Element renditionElement : renditionList) { ThemeMetadataTemplateRendition rendition = new ThemeMetadataTemplateRendition(); String renditionValue = renditionElement.getAttributeValue("type"); if (renditionValue != null) { try { rendition.setType(RenditionType.valueOf(renditionValue.toUpperCase())); } catch (IllegalArgumentException e) { throw new ThemeParsingException("Invalid rendition type " + renditionValue + " found."); }//from www . j av a 2s .co m } else { // default to standard if type not provided, as most templates are single-rendition rendition.setType(RenditionType.STANDARD); } String templateString = renditionElement.getChildText("templateLanguage"); if (StringUtils.isEmpty(templateString)) { throw new ThemeParsingException("rendition must contain a 'templateLanguage' element"); } else { try { rendition.setTemplateLang(TemplateLanguage.valueOf(templateString.toUpperCase())); } catch (IllegalArgumentException e) { throw new ThemeParsingException("Unknown templateLanguage value '" + templateString + "'"); } } rendition.setContentsFile(renditionElement.getChildText("contentsFile")); if (StringUtils.isEmpty(rendition.getContentsFile())) { throw new ThemeParsingException("stylesheet must contain a 'contentsFile' element"); } template.addTemplateRendition(rendition); } // validate template if (StringUtils.isEmpty(template.getName())) { throw new ThemeParsingException("stylesheet must contain a 'name' element"); } if (StringUtils.isEmpty(template.getLink())) { throw new ThemeParsingException("stylesheet must contain a 'link' element"); } return template; }
From source file:org.apache.roller.weblogger.config.runtime.RuntimeConfigDefsParser.java
License:Apache License
private PropertyDef elementToPropertyDef(Element element) { PropertyDef prop = new PropertyDef(); prop.setName(element.getAttributeValue("name")); prop.setKey(element.getAttributeValue("key")); prop.setType(element.getChildText("type")); prop.setDefaultValue(element.getChildText("default-value")); // optional elements if (element.getChild("rows") != null) { prop.setRows(element.getChildText("rows")); }//from w w w . j av a 2s . c o m if (element.getChild("cols") != null) { prop.setCols(element.getChildText("cols")); } return prop; }
From source file:org.apache.roller.weblogger.util.Trackback.java
License:Apache License
/** * Parse XML returned from trackback POST, returns error or success message * in RollerMessages object.//from ww w . ja v a 2 s.co m */ private RollerMessages parseTrackbackResponse(String response, RollerMessages messages) throws JDOMException, IOException { SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(new StringReader(StringEscapeUtils.unescapeHtml4(response))); Element root = doc.getRootElement(); if ("response".equals(root.getName())) { int code = -99; try { code = Integer.parseInt(root.getChildText("error")); } catch (NumberFormatException ignoredByDesign) { } String message = root.getChildText("message"); if (code != 0) { messages.addError("weblogEdit.trackbackFailure", Utilities.removeHTML(message)); } else { messages.addMessage("weblogEdit.trackbackSuccess"); } } else { messages.addError("weblogEdit.trackbackErrorParsing", Utilities.removeHTML(response)); } return messages; }
From source file:org.arcanist.client.ArcanistCCGFrame.java
License:Open Source License
/** * Loads appearance settings./*from www . ja va2 s. c o m*/ */ public boolean loadAppearance() { File appearanceFile = new File(Prefs.appearanceFilePath); if (!appearanceFile.exists()) return false; ArcanistCCG.LogManager.write(LogManager.INFO_LEVEL, "Loading appearance settings (" + Prefs.appearanceFilePath + ")."); Document doc = XmlUtils.readXML(appearanceFile); if (doc == null) return false; Element rootNode = doc.getRootElement(); Element tableElement = rootNode.getChild("table"); if (tableElement != null) { Element mainBgColorElement = tableElement.getChild("main-bgcolor"); Prefs.mainBgColor = XmlUtils.parseRGBAttribs(mainBgColorElement, null); if (Prefs.mainBgColor != null) pronoun.getContentPane().setBackground(Prefs.mainBgColor); Element tableBgColorElement = tableElement.getChild("table-bgcolor"); Prefs.tableBgColor = XmlUtils.parseRGBAttribs(tableBgColorElement, null); if (Prefs.tableBgColor != null) tablePane.getParent().setBackground(Prefs.tableBgColor); Element tableBgImageElement = tableElement.getChild("table-bgimage"); if (tableBgImageElement != null) { try { Prefs.tableBgTiled = XmlUtils .parseString(tableBgImageElement.getAttributeValue("tiled"), "false").equals("true"); Prefs.tableBgImage = tableBgImageElement.getText(); // Defaults to "" if (Prefs.tableBgImage.length() > 0) { tablePane.setBackgroundImage(Prefs.tableBgImage, Prefs.tableBgTiled); } else { tablePane.setBackgroundImage(null, Prefs.tableBgTiled); } } catch (IOException f) { JOptionPane.showInternalMessageDialog(desktop, "Couldn't load background image.", "Error", JOptionPane.PLAIN_MESSAGE); Prefs.tableBgImage = ""; Prefs.tableBgTiled = false; } } Element gridElement = tableElement.getChild("table-grid"); tablePane.setGridSnap(Math.max(1, XmlUtils.parseInt(gridElement.getAttributeValue("value"), 1))); } Element playerElement = rootNode.getChild("player"); if (playerElement != null) { Element chatAliasElement = playerElement.getChild("chat-alias"); if (chatAliasElement != null) { String savedAlias = chatAliasElement.getText(); String prevAlias = ArcanistCCG.NetManager.getPlayerAlias(); Prefs.setPlayerAlias(savedAlias); String newAlias = ArcanistCCG.NetManager.getPlayerAlias(); if (prevAlias.equals(newAlias) == false) { ArcanistCCG.NetManager.chatNotice(ChatPanel.STYLE_NOTICE2, "--" + prevAlias + " set alias to " + newAlias + "--"); } } Element chatTimestmpsElement = playerElement.getChild("chat-timestamps"); Prefs.chatTimestamps = XmlUtils.parseString(chatTimestmpsElement.getAttributeValue("value"), "false") .equals("true"); } boolean tableSized = false; Element windowsElement = rootNode.getChild("windows"); if (windowsElement != null) { List<Element> windowList = windowsElement.getChildren("window"); Iterator<Element> it = windowList.iterator(); while (it.hasNext()) { Element windowElement = it.next(); JInternalFrame newFrame = null; String windowType = windowElement.getAttributeValue("type"); if (windowType == null) continue; else if (windowType.equals("table")) { newFrame = tableFrame; tableSized = true; } else if (windowType.equals("jumbo")) { jumboFrame = createJumboFrame(); newFrame = jumboFrame; } else if (windowType.equals("chat")) { if (chatFrame == null) chatFrame = new ChatFrame(pronoun); newFrame = chatFrame; } else if (windowType.equals("tokens")) { newFrame = new TokensFrame(pronoun); } else if (windowType.equals("counter")) { newFrame = new CounterFrame(pronoun); String counterName = XmlUtils.parseString(windowElement.getChildText("name"), "Amount"); String counterValue = XmlUtils.parseString(windowElement.getAttributeValue("value"), "0"); ((CounterFrame) newFrame).setName(counterName); ((CounterFrame) newFrame).setValue(counterValue); } else if (windowType.equals("dieroller")) { newFrame = new DieRollerFrame(pronoun); String dieCount = XmlUtils.parseString(windowElement.getAttributeValue("count"), "1"); String dieSides = XmlUtils.parseString(windowElement.getAttributeValue("sides"), "20"); ((DieRollerFrame) newFrame).setDice(dieCount, dieSides); } else if (windowType.equals("timer")) { newFrame = new TimerFrame(pronoun); ((TimerFrame) newFrame) .setCountdown(XmlUtils.parseInt(windowElement.getAttributeValue("countdown"), 0)); } else continue; if (newFrame != null) { Rectangle newBounds = new Rectangle(); newBounds.x = XmlUtils.parseInt(windowElement.getAttributeValue("x"), 0); newBounds.y = XmlUtils.parseInt(windowElement.getAttributeValue("y"), 0); newBounds.width = XmlUtils.parseInt(windowElement.getAttributeValue("w"), 100); newBounds.height = XmlUtils.parseInt(windowElement.getAttributeValue("h"), 100); newFrame.reshape(newBounds.x, newBounds.y, newBounds.width, newBounds.height); } } } return tableSized; }
From source file:org.arcanist.client.ArcanistCCGFrame.java
License:Open Source License
/** * Loads keyboard settings.//from ww w . j ava 2s .c o m */ public void loadKeyboard() { File keyboardFile = new File(Prefs.keyboardFilePath); if (!keyboardFile.exists()) return; ArcanistCCG.LogManager.write(LogManager.INFO_LEVEL, "Loading keyboard settings (" + Prefs.keyboardFilePath + ")."); Map<String, KeyStroke> newStrokeMap = new HashMap<String, KeyStroke>(); Map<String, String> newArgMap = new HashMap<String, String>(); Iterator<Element> it = null; Document doc = XmlUtils.readXML(keyboardFile); if (doc == null) return; Element rootNode = doc.getRootElement(); Element hotkeysElement = rootNode.getChild("hotkeys"); if (hotkeysElement == null) return; List<Element> hotkeyList = hotkeysElement.getChildren("hotkey"); it = hotkeyList.iterator(); while (it.hasNext()) { Element hotkeyElement = it.next(); String hotkeyAction = hotkeyElement.getChildText("action"); String hotkeyString = hotkeyElement.getChildText("keystroke"); if (hotkeyAction == null || hotkeyAction.length() == 0 || hotkeyString == null) continue; KeyStroke hotkeyStroke = null; // Blank hotkeyString means null if (hotkeyString.length() > 0) { hotkeyStroke = KeyStroke.getKeyStroke(hotkeyString); if (hotkeyStroke == null) { ArcanistCCG.LogManager.write(LogManager.ERROR_LEVEL, "Invalid keystroke for keyboard setting: '" + hotkeyAction + "'."); continue; } } newStrokeMap.put(hotkeyAction, hotkeyStroke); String hotkeyArg = hotkeyElement.getChildText("arg"); if (hotkeyArg == null || hotkeyArg.length() == 0) continue; newArgMap.put(hotkeyAction, hotkeyArg); } if (newStrokeMap.size() == 0) return; synchronized (Prefs.hotkeyLock) { for (String hotkeyAction : newStrokeMap.keySet()) { KeyStroke hotkeyStroke = newStrokeMap.get(hotkeyAction); if (Prefs.hotkeyStrokeMap.containsKey(hotkeyAction)) { Prefs.hotkeyStrokeMap.put(hotkeyAction, hotkeyStroke); } else { ArcanistCCG.LogManager.write(LogManager.ERROR_LEVEL, "Unrecognied keyboard setting action: '" + hotkeyAction + "'."); continue; } if (newArgMap.containsKey(hotkeyAction)) { String hotkeyArg = newArgMap.get(hotkeyAction); if (Prefs.hotkeyArgMap.containsKey(hotkeyAction)) { Prefs.hotkeyArgMap.put(hotkeyAction, hotkeyArg); } else { ArcanistCCG.LogManager.write(LogManager.ERROR_LEVEL, "Unexpected keyboard setting arg for action: '" + hotkeyAction + "'."); continue; } } } } updateHotkeys(); }