List of usage examples for org.dom4j Document getRootElement
Element getRootElement();
From source file:com.arc.cdt.debug.seecode.core.launch.CMPDInfoFromVDKConfigReader.java
License:Open Source License
/** * Given a VDK configuration file, extract CMPD information suitable for creating a launch configuration. * @param vdkConfig the XML file to read from. * @return cmpd description/*ww w .j av a 2s . c o m*/ * @throws VDKConfigException if an error occurs in reading the config file. */ @SuppressWarnings("unchecked") public static ICMPDInfo extractCMPDInfo(File vdkConfig, IProject project) throws VDKConfigException { try { SAXReader reader = new SAXReader(); Document doc = reader.read(vdkConfig); Element root = doc.getRootElement(); if (root == null || !root.getName().equalsIgnoreCase("CMPD")) { throw new DocumentException("Root element is not \"CMPD\" node"); } if (!"1".equals(root.attributeValue("version"))) { throw new DocumentException("VDK config file has unknown version: " + root.attribute("version")); } List<Element> processes = root.elements("PROCESS"); final List<ICMPDInfo.IProcess> pList = new ArrayList<ICMPDInfo.IProcess>(processes.size()); File workingDir = vdkConfig.getParentFile(); for (Element p : processes) { pList.add(formProcess(p, workingDir, project)); } List<Element> launches = root.elements("LAUNCH"); // should be just one final List<String> launchSwitches = new ArrayList<String>(); final List<String> startupCommands = new ArrayList<String>(); if (launches != null) { for (Element e : launches) { appendLaunchSwitches(launchSwitches, startupCommands, e); } } return new ICMPDInfo() { @Override public String[] getLaunchArgs() { return launchSwitches.toArray(new String[launchSwitches.size()]); } @Override public IProcess[] getProcesses() { return pList.toArray(new IProcess[pList.size()]); } @Override public String[] getStartupCommands() { return startupCommands.toArray(new String[startupCommands.size()]); } }; } catch (MalformedURLException e) { throw new VDKConfigException(e.getMessage(), e); } catch (DocumentException e) { throw new VDKConfigException(e.getMessage(), e); } }
From source file:com.arc.cdt.debug.seecode.options.SeeCodeOptions.java
License:Open Source License
/** * Read the XML file that maps compiler options to equivalent seecode * arguments that will be passed to the SeeCode swahili processor ("scac"). * For example, ARC's "-Xswap", "-arc700", etc. * <P>/*w ww. j av a 2 s. c om*/ * If the target does not require any compiler options to be reflected, then * this method returns null. * <P> * The name of the XML file is derived from the target name. * * @param target * the name of the target (e.g., "ac", "vc", etc.) * @param project * applicable project. * @return the option mapping for computing seecode arguments, or * <code>null</code> if there isn't any such mapping. */ public static ISeeCodeOptionsAugmenter readOptionMapping(String target, IProject project) throws ConfigurationException { // Use cached copy if we've already loaded it. try { Element root = null; // If already parsed, remember it. if (target.equals(sCachedTarget)) root = sCached; else { String xml = target.toLowerCase() + "_options.xml"; InputStream input = SeeCodeOptions.class.getResourceAsStream(xml); if (input != null) { SAXReader reader = new SAXReader(); Document doc = reader.read(input); root = doc.getRootElement(); sCached = root; sCachedTarget = target; } } if (root != null) { IConfiguration buildConfiguration = getBuildConfiguration(project); if (buildConfiguration != null && isConsistentWithTarget(buildConfiguration, target.toLowerCase())) { return new SeeCodeOptionsAugmenter(root, buildConfiguration); } } } catch (DocumentException e) { throw new ConfigurationException(e.getMessage(), e); } return null; }
From source file:com.arc.mw.util.StateSaver.java
License:Open Source License
public static void restoreState(String path, IXMLSavable object) throws DocumentException { SAXReader reader = new SAXReader(); Document doc = reader.read(path); object.restoreState(doc.getRootElement()); }
From source file:com.arc.xml.XmlProcessor.java
License:Open Source License
public XmlProcessor(InputSource input) throws BadXmlException { try {//ww w .j a v a 2 s . com Document doc = new MySAXReader().read(input); Element root = (Element) doc.getRootElement(); processMeta(root); } catch (DocumentException x) { // Shouldn't happen throw new BadXmlException(x.getMessage()); } }
From source file:com.arc.xml.XmlProcessor.java
License:Open Source License
private void walk(Document doc) throws SAXParseException, SAXException { Element e = (Element) doc.getRootElement(); // This will be the root element IBinding b = mRoot.getBinding(e.getName()); if (b == null) error(e, "Don't recognize root node \"" + e.getName() + "\""); else {//ww w. j a v a 2s . c o m IBuilder bb = null; try { bb = b.getBuilder(); } catch (RuntimeException x) { throw x; } catch (Exception x) { error(e, "Can't instantiate builder for root \"" + e.getName() + "\": " + x.getMessage()); return; } bb.build(e, b, null); } }
From source file:com.aricojf.util.ConfigParser.java
/** * ??// w ww. ja va 2 s. c o m * ?String "/?/?" * ep: "config/conn/user" * @return */ public static String parseConfigParmByPath(String configFilePath, String nodes) { value = ""; FileInputStream fs = null; File file = new File(configFilePath); if (file.exists()) { file = null; try { fs = new FileInputStream(configFilePath); SAXReader reader = new SAXReader(); org.dom4j.Document document = reader.read(fs); org.dom4j.Element element = document.getRootElement(); String[] nod = nodes.trim().split(FILE_SEPARATOR); for (int i = 1; i < nod.length; i++) { List<Element> elements = element.elements(nod[i]); /** * ??????? * ??xml????? */ for (Element ele : elements) { element = ele; value = ele.getText(); break; } } } catch (Exception e) { e.printStackTrace(); } finally { closeConn(fs); } } return null == value ? "" : value; }
From source file:com.atguigu.p2p.util.XmlUtils.java
License:Apache License
@SuppressWarnings("unchecked") public static Map<String, String> xml2Map(String xml) { Map<String, String> map = new HashMap<String, String>(); try {// w w w . j a va 2 s . c o m Document document = DocumentHelper.parseText(xml); Element rootElement = document.getRootElement(); for (Iterator<Element> it = rootElement.elementIterator(); it.hasNext();) { Element element = (Element) it.next(); map.put(element.getName(), element.getText()); } } catch (Exception e) { logger.error("xml2Map error:", e); } return map; }
From source file:com.baidu.terminator.plugin.extractor.http.HttpXmlExtractor.java
License:Open Source License
@Override public List<RequestElement> extract(Object request) { List<RequestElement> elements = new ArrayList<RequestElement>(); HttpRequest httpRequest = (HttpRequest) request; String method = httpRequest.getMethod().getName(); RequestElement methondElement = new RequestElement(); methondElement.setKey("METHOD"); methondElement.setValue(method);/*w w w.j a v a 2 s.c om*/ elements.add(methondElement); String uri = httpRequest.getUri(); List<RequestElement> uri_elements = UriElementGetter.getUri(uri); for (int i = 0; i < uri_elements.size(); i++) { elements.add(uri_elements.get(i)); } // RequestElement uriElement = new RequestElement(); // uriElement.setKey("URI"); // uriElement.setValue(uri); // elements.add(uriElement); List<Entry<String, String>> headers = httpRequest.getHeaders(); for (Entry<String, String> entry : headers) { RequestElement headerElement = new RequestElement(); headerElement.setKey(entry.getKey()); headerElement.setValue(entry.getValue()); elements.add(headerElement); } ChannelBuffer content = httpRequest.getContent(); if (content.readable()) { String stringContent = ChannelUtil.readChannelBufferAsString(content); Document document = null; try { document = DocumentHelper.parseText(stringContent); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } Element root = document.getRootElement(); contentmap.clear(); getElementList(root); for (int i = 0; i < contentmap.size(); i++) { elements.add((RequestElement) contentmap.get(i)); } } logger.info("the elements of request is :" + elements); return elements; }
From source file:com.baomidou.springwind.util.yuntongxin.CCPRestSDK.java
License:Open Source License
/** * @description xml??map//from ww w . j a va2s. c o m * @param xml * @return Map */ private HashMap<String, Object> xmlToMap(String xml) { HashMap<String, Object> map = new HashMap<String, Object>(); org.dom4j.Document doc = null; try { doc = org.dom4j.DocumentHelper.parseText(xml); // XML org.dom4j.Element rootElt = doc.getRootElement(); // ? HashMap<String, Object> hashMap2 = new HashMap<String, Object>(); ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String, Object>>(); for (Iterator i = rootElt.elementIterator(); i.hasNext();) { org.dom4j.Element e = (org.dom4j.Element) i.next(); if ("statusCode".equals(e.getName()) || "statusMsg".equals(e.getName())) map.put(e.getName(), e.getText()); else { if ("SubAccount".equals(e.getName()) || "TemplateSMS".equals(e.getName()) || "totalCount".equals(e.getName()) || "token".equals(e.getName()) || "callSid".equals(e.getName()) || "state".equals(e.getName()) || "downUrl".equals(e.getName())) { if (!"SubAccount".equals(e.getName()) && !"TemplateSMS".equals(e.getName())) { hashMap2.put(e.getName(), e.getText()); } else if ("SubAccount".equals(e.getName())) { HashMap<String, Object> hashMap3 = new HashMap<String, Object>(); for (Iterator i2 = e.elementIterator(); i2.hasNext();) { org.dom4j.Element e2 = (org.dom4j.Element) i2.next(); hashMap3.put(e2.getName(), e2.getText()); } arrayList.add(hashMap3); hashMap2.put("SubAccount", arrayList); } else if ("TemplateSMS".equals(e.getName())) { HashMap<String, Object> hashMap3 = new HashMap<String, Object>(); for (Iterator i2 = e.elementIterator(); i2.hasNext();) { org.dom4j.Element e2 = (org.dom4j.Element) i2.next(); hashMap3.put(e2.getName(), e2.getText()); } arrayList.add(hashMap3); hashMap2.put("TemplateSMS", arrayList); } map.put("data", hashMap2); } else { HashMap<String, Object> hashMap3 = new HashMap<String, Object>(); for (Iterator i2 = e.elementIterator(); i2.hasNext();) { org.dom4j.Element e2 = (org.dom4j.Element) i2.next(); // hashMap2.put(e2.getName(),e2.getText()); hashMap3.put(e2.getName(), e2.getText()); } if (hashMap3.size() != 0) { hashMap2.put(e.getName(), hashMap3); } else { hashMap2.put(e.getName(), e.getText()); } map.put("data", hashMap2); } } } } catch (org.dom4j.DocumentException e) { e.printStackTrace(); logger.error(e.getMessage()); } catch (Exception e) { logger.error(e.getMessage()); e.printStackTrace(); } return map; }
From source file:com.bay12games.df.rawedit.xml.RawsDescriptionLoader.java
License:Open Source License
/** * Load and parse specified raws definition file. If elements is null, only * the content of the current file will be returned. If elements is not null * and contains not-null token and container maps, the content of current * file will be appended as if the definitions were in one file.<br><br> * * The instances of the underlying containers are preserved.<br><br> * * <p><strong>Note:</strong> There should be only one global access to the token/container * definitions in the application.<br> * * @param file The file to load and parse * @param elements The tuple-container for token, container and id elements * @return New instance of ElementContainer with maps of tokens and containers if the * supplied ElementContainer was null, or the same instance with updated content. * The supplied instance (possibly null) is returned on error *//*from w w w. j a v a 2s.c o m*/ public ElementContainer parse(File file, ElementContainer elements) { Document d; try { SAXReader reader = new SAXReader(); d = reader.read(file); } catch (DocumentException ex) { log.error("Unable to load file:" + file + '.', ex); return elements; } if (elements == null) { containers = new HashMap<String, Container>(); tokens = new HashMap<String, Token>(); ids = new HashMap<String, Id>(); elements = new ElementContainer(containers, tokens, ids); } else { if (elements.getContainers() != null) { containers = elements.getContainers(); } else { containers = new HashMap<String, Container>(); } if (elements.getTokens() != null) { tokens = elements.getTokens(); } else { tokens = new HashMap<String, Token>(); } if (elements.getIds() != null) { ids = elements.getIds(); } else { ids = new HashMap<String, Id>(); } } Element root = d.getRootElement(); for (Element e : root.elements()) { if ("c".equals(e.getName())) { parseContainer(e); } else if ("t".equals(e.getName())) { parseToken(e); } else if ("id".equals(e.getName())) { parseId(e); } } return elements;//new ElementContainer(containers, tokens, ids); }