/** * Parse a MAVLink xml messages descriptor file. * * @param mavlink MAVLink data used and to fill * @param file Parsed file * @param path Path to file * @param target Path for generation * @param inInclude True if we are in an include file * @return implementation code for readers and writers. * @throws ParserConfigurationException * @throws SAXException * @throws IOException */ public Map<String, String> parseFile( MAVLinkData mavlink, String file, String path, String target, boolean inInclude) throws ParserConfigurationException, SAXException, IOException { Map<String, String> implementations = new HashMap<String, String>(); SAXParserFactory fabrique = SAXParserFactory.newInstance(); SAXParser parseur = fabrique.newSAXParser(); if (!inInclude) { mavlink.setFile(file.substring(0, file.indexOf('.'))); System.out.println("MAVLinkData : " + mavlink.getFile()); } else { System.out.println("MAVLinkData INCLUDE : " + file.substring(0, file.indexOf('.'))); } MAVLinkHandler gestionnaire = new MAVLinkHandler(this, mavlink, path, target); parseur.parse(new File(path + File.separator + file), gestionnaire); mavlink = gestionnaire.getMavlink(); generateMessageClass(mavlink, target); mavlink.getEnums().putAll(mavlink.getEnums()); mavlink.getMessages().putAll(mavlink.getMessages()); generateEnumClass(mavlink, target, implementations); return implementations; }
/** * Generate MAVLink Java Enum classes * * @param mavlink * @param targetPath * @param implementations */ protected void generateEnumClass( MAVLinkData mavlink, String targetPath, Map<String, String> implementations) { String packageRootName = "org.mavlink.messages"; String packageName = packageRootName; String directory = targetPath + "/org/mavlink/messages/"; OutputStream output = null; PrintWriter writer = null; for (MAVLinkEnum message : mavlink.getEnums().values()) { try { String className = message.getName(); String filename = directory + className + ".java"; output = new FileOutputStream(filename, false); writer = new PrintWriter(output); writer.print("/**\n * Generated class : " + className + "\n * DO NOT MODIFY!\n **/\n"); writer.print("package " + packageName + ";\n"); String description = message.getDescription(); writer.print( "/**\n * Interface " + className + "\n * " + (description == null ? "" : message.getDescription().trim()) + "\n **/\n"); writer.print("public interface " + className + " {\n"); implementations.put(className.trim(), className.trim()); for (int j = 0; j < message.getEntries().size(); j++) { MAVLinkEntry entry = message.getEntries().get(j); writer.print(" /**\n * " + entry.getDescription() + "\n"); for (int k = 0; k < entry.getParams().size(); k++) { MAVLinkParam param = entry.getParams().get(k); writer.print(" * PARAM " + param.getIndex() + " : " + param.getComment() + "\n"); } writer.print(" */\n"); writer.print( " public final static int " + entry.getName() + " = " + entry.getValue() + ";\n"); } writer.print("}\n"); } catch (Exception e) { System.err.println("ERROR : " + e); e.printStackTrace(); } finally { try { writer.close(); output.close(); } catch (Exception ex) { System.err.println("ERROR : " + ex); ex.printStackTrace(); } } } }