List of usage examples for org.dom4j DocumentHelper createDocument
public static Document createDocument()
From source file:net.sf.jvifm.model.ShortcutsManager.java
License:Open Source License
public void store() { try {/* w w w. j a va 2s . co m*/ Document document = DocumentHelper.createDocument(); Element root = document.addElement("commands"); for (Shortcut shortcut : shortcutsList) { Element shortcutElement = root.addElement("command"); shortcutElement.addElement("name").addText(shortcut.getName()); shortcutElement.addElement("text").addText(shortcut.getText()); } FileOutputStream fos = new FileOutputStream(storePath); OutputFormat outformat = OutputFormat.createPrettyPrint(); outformat.setEncoding("UTF-8"); BufferedOutputStream out = new BufferedOutputStream(fos); XMLWriter writer = new XMLWriter(out, outformat); writer.write(document); writer.flush(); writer.close(); fos.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:net.sf.kraken.KrakenPlugin.java
License:Open Source License
/** * Returns the web options config for the given transport, if it exists. * * @param type type of the transport we want the options config for. * @return XML document with the options config. *//*from w ww . j av a2 s. c o m*/ public Document getOptionsConfig(TransportType type) { // Load any custom-defined servlets. File optConf = new File(this.pluginDirectory, "web" + File.separator + "WEB-INF" + File.separator + "options" + File.separator + type.toString() + ".xml"); Document optConfXML; try { FileReader reader = new FileReader(optConf); SAXReader xmlReader = new SAXReader(); xmlReader.setEncoding("UTF-8"); optConfXML = xmlReader.read(reader); } catch (FileNotFoundException e) { // Non-existent: Return empty config optConfXML = DocumentHelper.createDocument(); optConfXML.addElement("optionsconfig"); } catch (DocumentException e) { // Bad config: Return empty config optConfXML = DocumentHelper.createDocument(); optConfXML.addElement("optionsconfig"); } return optConfXML; }
From source file:net.sf.kraken.KrakenPlugin.java
License:Open Source License
/** * Returns the web global options, if it exists. * * @return XML document with the options config. *///from w w w. j av a 2s. co m public Document getOptionsConfig() { // Load any custom-defined servlets. File optConf = new File(this.pluginDirectory, "web" + File.separator + "WEB-INF" + File.separator + "options" + File.separator + "global.xml"); Document optConfXML; try { FileReader reader = new FileReader(optConf); SAXReader xmlReader = new SAXReader(); xmlReader.setEncoding("UTF-8"); optConfXML = xmlReader.read(reader); } catch (FileNotFoundException e) { // Non-existent: Return empty config optConfXML = DocumentHelper.createDocument(); optConfXML.addElement("optionsconfig"); } catch (DocumentException e) { // Bad config: Return empty config optConfXML = DocumentHelper.createDocument(); optConfXML.addElement("optionsconfig"); } return optConfXML; }
From source file:net.windward.Acquire.Framework.java
License:BEER-WARE LICENSE
public final void IncomingMessage(String message) throws DocumentException, IOException { try {/* w w w. j av a2 s . c o m*/ long startTime = System.currentTimeMillis(); // get the xml - we assume we always get a valid message from the server. SAXReader reader = new SAXReader(); Document xml = reader.read(new StringReader(message)); String rootName = xml.getRootElement().getName(); if (rootName.equals("setup")) { System.out.println("Received setup message"); if (log.isInfoEnabled()) log.info("Recieved setup message"); myGuid = xml.getRootElement().attribute("my-guid").getValue(); Element elemMap = xml.getRootElement().element("map"); GameMap map = new GameMap(Integer.parseInt(elemMap.attribute("width").getValue()), Integer.parseInt(elemMap.attribute("height").getValue())); DataObjects dataSetup = xmlToData(xml); brain.Setup(map, dataSetup.me, dataSetup.hotels, dataSetup.players); // say ready Document docSetup = DocumentHelper.createDocument(); Element elem = DocumentHelper.createElement("ready"); docSetup.add(elem); tcpClient.SendMessage(docSetup.asXML()); } else if (rootName.equals("query-card")) { DataObjects dataQuery = xmlToData(xml); int card = brain.QuerySpecialPowerBeforeTurn(dataQuery.map, dataQuery.me, dataQuery.hotels, dataQuery.players); // send the selected card back Document docQueryCard = DocumentHelper.createDocument(); Element elem = DocumentHelper.createElement("reply"); docQueryCard.add(elem); elem.add(DocumentHelper.createAttribute(elem, "cmd", "query-card")); elem.add(DocumentHelper.createAttribute(elem, "msg-id", xml.getRootElement().attribute("msg-id").getValue())); elem.add(DocumentHelper.createAttribute(elem, "card", "" + card)); tcpClient.SendMessage(docQueryCard.asXML()); } else if (rootName.equals("query-tile")) { DataObjects dataQueryTile = xmlToData(xml); PlayerPlayTile playTile = brain.QueryTileOnly(dataQueryTile.map, dataQueryTile.me, dataQueryTile.hotels, dataQueryTile.players); // send the selected tile back Document docQueryCard = DocumentHelper.createDocument(); Element elem = DocumentHelper.createElement("reply"); docQueryCard.add(elem); elem.add(DocumentHelper.createAttribute(elem, "cmd", "query-tile")); elem.add(DocumentHelper.createAttribute(elem, "msg-id", xml.getRootElement().attribute("msg-id").getValue())); if (playTile != null) { if (playTile.tile != null) { elem.add(DocumentHelper.createAttribute(elem, "tile-x", "" + playTile.tile.getX())); elem.add(DocumentHelper.createAttribute(elem, "tile-y", "" + playTile.tile.getY())); } if (playTile.createdHotel != null) { elem.add(DocumentHelper.createAttribute(elem, "created-hotel", playTile.createdHotel.getName())); } if (playTile.mergeSurvivor != null) { elem.add(DocumentHelper.createAttribute(elem, "merge-survivor", playTile.mergeSurvivor.getName())); } } tcpClient.SendMessage(docQueryCard.asXML()); } else if (rootName.equals("query-tile-purchase")) { DataObjects dataQueryTilePur = xmlToData(xml); PlayerTurn playTurn = brain.QueryTileAndPurchase(dataQueryTilePur.map, dataQueryTilePur.me, dataQueryTilePur.hotels, dataQueryTilePur.players); // send the selected card back Document docQueryCard = DocumentHelper.createDocument(); Element elem = DocumentHelper.createElement("reply"); docQueryCard.add(elem); elem.add(DocumentHelper.createAttribute(elem, "cmd", "query-tile-purchase")); elem.add(DocumentHelper.createAttribute(elem, "msg-id", xml.getRootElement().attribute("msg-id").getValue())); if (playTurn != null) { elem.add(DocumentHelper.createAttribute(elem, "card", "" + playTurn.getCard())); if (playTurn.tile != null) { elem.add(DocumentHelper.createAttribute(elem, "tile-x", "" + playTurn.tile.getX())); elem.add(DocumentHelper.createAttribute(elem, "tile-y", "" + playTurn.tile.getY())); } if (playTurn.createdHotel != null) { elem.add(DocumentHelper.createAttribute(elem, "created-hotel", playTurn.createdHotel.getName())); } if (playTurn.mergeSurvivor != null) { elem.add(DocumentHelper.createAttribute(elem, "merge-survivor", playTurn.mergeSurvivor.getName())); } if (playTurn.getBuy() != null && playTurn.getBuy().size() > 0) { StringBuilder buyStock = new StringBuilder(); for (HotelStock stock : playTurn.getBuy()) buyStock.append(stock.getChain().getName() + ':' + stock.getNumShares() + ';'); elem.add(DocumentHelper.createAttribute(elem, "buy", buyStock.toString())); } if (playTurn.getTrade() != null && playTurn.getTrade().size() > 0) { StringBuilder tradeStock = new StringBuilder(); for (PlayerTurn.TradeStock trade : playTurn.getTrade()) tradeStock .append(trade.getTradeIn2().getName() + ':' + trade.getGet1().getName() + ';'); elem.add(DocumentHelper.createAttribute(elem, "trade", tradeStock.toString())); } } tcpClient.SendMessage(docQueryCard.asXML()); } else if (rootName.equals("query-merge")) { DataObjects dataQueryMerge = xmlToData(xml); HotelChain survivor = null; for (HotelChain hotel : dataQueryMerge.hotels) if (hotel.getName().equals(xml.getRootElement().attribute("survivor").getValue())) { survivor = hotel; break; } HotelChain defunct = null; for (HotelChain hotel : dataQueryMerge.hotels) if (hotel.getName().equals(xml.getRootElement().attribute("defunct").getValue())) { defunct = hotel; break; } PlayerMerge merge = brain.QueryMergeStock(dataQueryMerge.map, dataQueryMerge.me, dataQueryMerge.hotels, dataQueryMerge.players, survivor, defunct); // send the selected card back Document docQueryMerge = DocumentHelper.createDocument(); Element elem = DocumentHelper.createElement("reply"); docQueryMerge.add(elem); elem.add(DocumentHelper.createAttribute(elem, "cmd", "query-card")); elem.add(DocumentHelper.createAttribute(elem, "msg-id", xml.getRootElement().attribute("msg-id").getValue())); if (merge != null) { elem.add(DocumentHelper.createAttribute(elem, "keep", "" + merge.getKeep())); elem.add(DocumentHelper.createAttribute(elem, "sell", "" + merge.getSell())); elem.add(DocumentHelper.createAttribute(elem, "trade", "" + merge.getTrade())); } tcpClient.SendMessage(docQueryMerge.asXML()); } else if (xml.getRootElement().getName().equals("exit")) { System.out.println("Received exit message"); if (log.isInfoEnabled()) { log.info("Received exit message"); } System.exit(0); } else { String msg = String.format("ERROR: bad message (XML) from server - root node %1$s", xml.getRootElement().getName()); log.warn(msg); //Trace.WriteLine(msg); } long turnTime = System.currentTimeMillis() - startTime; if (turnTime > 800) { System.out.println("WARNING - turn took " + turnTime / 1000 + " seconds"); } } catch (RuntimeException ex) { System.out.println(String.format("Error on incoming message. Exception: %1$s", ex)); ex.printStackTrace(); log.error("Error on incoming message.", ex); } }
From source file:net.windward.Acquire.Framework.java
License:BEER-WARE LICENSE
private void ConnectToServer() throws IOException { try {/* w w w . jav a2 s . c om*/ Document doc = DocumentHelper.createDocument(); Element root = DocumentHelper.createElement("join"); root.addAttribute("name", brain.getName()); root.addAttribute("school", MyPlayerBrain.SCHOOL); root.addAttribute("language", "Java"); byte[] data = brain.getAvatar(); if (data != null) { Element avatarElement = DocumentHelper.createElement("avatar"); BASE64Encoder encoder = new BASE64Encoder(); avatarElement.setText(encoder.encode(data)); root.add(avatarElement); } doc.add(root); tcpClient.SendMessage(doc.asXML()); } catch (Exception e) { log.warn("ConnectToServer() threw Exception: " + e.getMessage()); } }
From source file:net.windward.Windwardopolis2.Framework.java
License:BEER-WARE LICENSE
private void PlayerOrdersEvent(String order, java.util.ArrayList<Point> path, java.util.ArrayList<Passenger> pickUp) { try {/*from w w w . ja va2s.c o m*/ // update our info if (path.size() > 0) { brain.getMe().getLimo().getPath().clear(); brain.getMe().getLimo().getPath().addAll(path); } if (pickUp.size() > 0) { brain.getMe().getPickUp().clear(); brain.getMe().getPickUp().addAll(pickUp); } Document xml = DocumentHelper.createDocument(); Element elem = DocumentHelper.createElement(order); xml.add(elem); if (path.size() > 0) { StringBuilder buf = new StringBuilder(); for (Point ptOn : path) { buf.append(String.valueOf(ptOn.x) + ',' + String.valueOf(ptOn.y) + ';'); } Element newElem = DocumentHelper.createElement("path"); newElem.setText(buf.toString()); elem.add(newElem); } if (pickUp.size() > 0) { StringBuilder buf = new StringBuilder(); for (Passenger psngrOn : pickUp) { buf.append(psngrOn.getName() + ';'); } Element newElem = DocumentHelper.createElement("pick-up"); newElem.setText(buf.toString()); elem.add(newElem); } try { String toSend = xml.asXML(); tcpClient.SendMessage(toSend); } catch (IOException e) { System.out.println("bad sent orders event"); e.printStackTrace(); } } catch (Exception e) { log.error("PlayerOrderEvent( " + order + ", ...) threw Exception: " + e.getMessage()); } }
From source file:net.windward.Windwardopolis2.Framework.java
License:BEER-WARE LICENSE
private void PlayerPowerSend(PlayerAIBase.CARD_ACTION action, PowerUp powerup) { if (log.isInfoEnabled()) log.info("Request " + action + " " + powerup); cardLastPlayed = powerup;/*w ww. j av a2s . c om*/ cardLastSendTime = System.currentTimeMillis(); Document xml = DocumentHelper.createDocument(); Element elem = DocumentHelper.createElement("order"); elem.add(DocumentHelper.createAttribute(elem, "action", action.name())); Element elemCard = DocumentHelper.createElement("powerup"); elemCard.add(DocumentHelper.createAttribute(elemCard, "card", powerup.getCard().name())); if (powerup.getCompany() != null) elemCard.add(DocumentHelper.createAttribute(elemCard, "company", powerup.getCompany().getName())); if (powerup.getPassenger() != null) elemCard.add(DocumentHelper.createAttribute(elemCard, "passenger", powerup.getPassenger().getName())); if (powerup.getPlayer() != null) elemCard.add(DocumentHelper.createAttribute(elemCard, "player", powerup.getPlayer().getName())); elem.add(elemCard); xml.add(elem); try { String toSend = xml.asXML(); tcpClient.SendMessage(toSend); } catch (IOException e) { System.out.println("bad sent orders event"); e.printStackTrace(); } }
From source file:nidonuevo.model.Engine.java
public void saveToXML() { // try { // Thread.sleep(3000);//Para esperar a q se cargue todo, despues lo borraremos // } catch (InterruptedException ex) { // Logger.getLogger(Engine.class.getName()).log(Level.SEVERE, null, ex); // }//from w ww . j av a2 s. com Document document = DocumentHelper.createDocument(); Element root = document.addElement("GameData"); //PLAYER Element player = root.addElement("Player"); player.addElement("name").addText(LMS.getPlayer().getName()); player.addElement("happiness").addText("" + LMS.getPlayer().getHappiness()); player.addElement("numberOfFriends").addText("" + LMS.getPlayer().getNumberOfFriends()); player.addElement("level").addText("" + LMS.getPlayer().getLevel()); player.addElement("numerOfTrophies").addText("" + LMS.getPlayer().getNumberOfTrophies()); ////FRIENDS Element friends = player.addElement("Friends"); for (int i = 0; i < LMS.getPlayer().getFriends().size(); i++) { Element friend = friends.addElement("Friend").addAttribute("id", "" + LMS.getPlayer().getFriends().get(i).getId()); } ////INVENTORY Element inventory = player.addElement("Inventory"); inventory.addElement("Capacity").addText("" + LMS.getPlayer().getInventory().getCapacity()); inventory.addElement("Quantity").addText("" + LMS.getPlayer().getInventory().getQuantity()); //////ITEMS Element items = inventory.addElement("Items"); for (int i = 0; i < LMS.getPlayer().getInventory().getItems().size(); i++) { Element item = items.addElement("Item").addAttribute("id", "" + LMS.getPlayer().getInventory().getItems().get(i).getId()); item.addElement("stock").addText("" + LMS.getPlayer().getInventory().getItems().get(i).getStock()); } //Mapa Actual Element cMap = root.addElement("CurrentMap"); cMap.addElement("Map").addText("" + getCurrentMap()); try { OutputFormat format = OutputFormat.createPrettyPrint(); format.setIndent(true); XMLWriter writer = new XMLWriter(new FileWriter("GameData.xml"), format); writer.write(document); writer.setIndentLevel(2); writer.close(); } catch (IOException ex) { Logger.getLogger(Engine.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:nidonuevo.model.Engine.java
private void saveGameToXML() { Document document = DocumentHelper.createDocument(); Element root = document.addElement("Game"); //General/*from www. ja va 2 s.c om*/ Element general = root.addElement("General"); general.addElement("title").addText(title); general.addElement("width").addText("" + getWidth()); general.addElement("height").addText("" + getHeight()); //Mapas Element maps = root.addElement("Maps"); for (int i = 0; i < LMS.getMaps().size(); i++) { Element map = maps.addElement("Map").addAttribute("id", "" + i); map.addElement("NumberLayers").addText("" + LMS.getMaps().get(i).getLayers().size()); Element source = map.addElement("Source"); for (int j = 0; j < LMS.getMaps().get(i).getLayers().size(); j++) { source.addElement("Path").addAttribute("id", "" + j).addText(LMS.getMaps().get(i).getPaths()[j]); source.addElement("Img").addAttribute("id", "" + j).addText(LMS.getMaps().get(i).getDirImg()[j]); //falta width,gehith, layer. mapa, etc, terminar mapash } //TRIGGERS Element triggers = map.addElement("Triggers"); for (int j = 0; j < LMS.getMaps().get(i).getTriggers().size(); j++) { Element trigger = triggers.addElement("Trigger"); if (LMS.getMaps().get(i).getTriggers().get(j) instanceof TriggerChangeMap) { TriggerChangeMap aux = (TriggerChangeMap) LMS.getMaps().get(i).getTriggers().get(j); trigger.addElement("type").addText("TriggerChangeMap"); trigger.addElement("par").addText("" + aux.x); trigger.addElement("par").addText("" + aux.y); trigger.addElement("par").addText("" + aux.getChangeTo()); trigger.addElement("par").addText("" + aux.getpX()); trigger.addElement("par").addText("" + aux.getpY()); } else if (LMS.getMaps().get(i).getTriggers().get(j) instanceof TriggerMap) { TriggerMap aux = (TriggerMap) LMS.getMaps().get(i).getTriggers().get(j); trigger.addElement("type").addText("TriggerMap"); trigger.addElement("par").addText("" + aux.x); trigger.addElement("par").addText("" + aux.y); trigger.addElement("par").addText("" + aux.getChangeTo()); trigger.addElement("par").addText("" + aux.getpX()); trigger.addElement("par").addText("" + aux.getpY()); } else if (LMS.getMaps().get(i).getTriggers().get(j) instanceof TriggerMini) { TriggerMini aux = (TriggerMini) LMS.getMaps().get(i).getTriggers().get(j); trigger.addElement("type").addText("TriggerMini"); trigger.addElement("par").addText("" + aux.x); trigger.addElement("par").addText("" + aux.y); trigger.addElement("par").addText("" + aux.getChangeTo()); } else if (LMS.getMaps().get(i).getTriggers().get(j) instanceof TriggerMonologue) { TriggerMonologue aux = (TriggerMonologue) LMS.getMaps().get(i).getTriggers().get(j); trigger.addElement("type").addText("TriggerMonologue"); trigger.addElement("par").addText("" + aux.x); trigger.addElement("par").addText("" + aux.y); trigger.addElement("par").addText("" + aux.getChangeTo()); } } //GOALS Element goals = map.addElement("Goals"); for (int j = 0; j < LMS.getMaps().get(i).getGoals().size(); j++) { Element goal = goals.addElement("Goal"); Goal aux = LMS.getMaps().get(i).getGoals().get(j); //int id, boolean active,String desc,int tipo goal.addElement("id").addText("" + aux.getId()); goal.addElement("active").addText("" + aux.isActive()); goal.addElement("description").addText("" + aux.getDescription()); goal.addElement("type").addText("" + aux.getTipo()); } //MINIGAME } //Player Element player = root.addElement("Player"); player.addElement("positionX").addText("" + LMS.getPlayer().getPositionX()); player.addElement("positionY").addText("" + LMS.getPlayer().getPositionY()); player.addElement("dir").addText("" + LMS.getPlayer().getDir()); player.addElement("path").addText("" + LMS.getPlayer().getPath()); player.addElement("contDelay").addText("" + LMS.getPlayer().getContDelay()); player.addElement("width").addText("" + LMS.getPlayer().getWidth()); player.addElement("height").addText("" + LMS.getPlayer().getHeight()); player.addElement("tW").addText("" + LMS.getPlayer().gettW()); player.addElement("tH").addText("" + LMS.getPlayer().gettH()); player.addElement("speed").addText("" + LMS.getPlayer().getSpeed()); player.addElement("happiness").addText("" + LMS.getPlayer().getHappiness()); player.addElement("numberOfFriends").addText("" + LMS.getPlayer().getNumberOfFriends()); player.addElement("numberOfTrophies").addText("" + LMS.getPlayer().getNumberOfTrophies()); Element inventory = player.addElement("Inventory"); for (int i = 0; i < LMS.getPlayer().getInventory().getItems().size(); i++) { Element item = inventory.addElement("Item").addAttribute("id", "" + LMS.getPlayer().getInventory().getItems().get(i).getId()); item.addElement("name").addText("" + LMS.getPlayer().getInventory().getItems().get(i).getName()); item.addElement("stock").addText("" + LMS.getPlayer().getInventory().getItems().get(i).getStock()); item.addElement("description") .addText("" + LMS.getPlayer().getInventory().getItems().get(i).getDescription()); item.addElement("image").addText("" + LMS.getPlayer().getInventory().getItems().get(i).getImage()); } //MINIJUEGOS Element miniGames = root.addElement("MiniGames"); for (int j = 0; j < LMS.getPlayer().getMiniGames().size(); j++) { Element miniGame = miniGames.addElement("MiniGame"); miniGame.addElement("id").addText("" + j); MiniGame aux = LMS.getPlayer().getMiniGames().get(j); //jugadores Element persons = miniGame.addElement("Persons"); ; for (int k = 0; k < aux.getPersons().size(); k++) { persons.addElement("id").addText("" + aux.getPersons().get(k).id); } //mensajes Element messages = miniGame.addElement("Messages"); for (int k = 0; k < aux.getMessages().size(); k++) { messages.addElement("message").addText("" + aux.getMessages().get(k)); } //respuestas Element answers = miniGame.addElement("Answers"); for (int k = 0; k < aux.getAnswers().size(); k++) { String auxS = ""; for (int j1 = 0; j1 < aux.getAnswers().get(k).length; j1++) { if (j1 == 0) { auxS = aux.getAnswers().get(k)[j1]; } else { auxS = auxS + "-" + aux.getAnswers().get(k)[j1]; } } answers.addElement("answer").addText(auxS); } //respuestas correctas Element correct = miniGame.addElement("Correct1"); for (int k = 0; k < aux.getCorrect().size(); k++) { correct.addElement("correct").addText("" + aux.getCorrect().get(k)); } //puntos Element points = miniGame.addElement("Points"); for (int k = 0; k < aux.getPoints().size(); k++) { points.addElement("point").addText("" + aux.getPoints().get(k)); } } //FIN //WRITER try { OutputFormat format = OutputFormat.createPrettyPrint(); format.setIndent(true); XMLWriter writer = new XMLWriter(new FileWriter("juego.xml"), format); writer.write(document); writer.setIndentLevel(2); writer.close(); } catch (IOException ex) { Logger.getLogger(Engine.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:nl.knaw.dans.dccd.model.EntityTree.java
License:Apache License
/** Get the dom4j xml document object that represents the entity tree, * but then only the structure, not the Entity objects. * * @return The document// w w w .j a v a 2 s. c o m */ public Document getTreeStructAsDocument() { // note: if projectEntity == null, throw an exception ? // if (getProjectEntity() == null) { logger.warn("No project, creating empty tree"); } // create a XML DOM document using dom4j // don't delegate to the entities this time Document document = DocumentHelper.createDocument(); Element structmap = document.addElement(ENTITYTREESTRUCT_NS_PREFIX + ENTITYTREESTRUCT_NS);//("structmap"); // root structmap.addAttribute("version", ENTITYTREESTRUCT_VERSION); structmap.addNamespace(ENTITYTREESTRUCT_NS, ENTITYTREESTRUCT_NS_URL); buildTreeStructElementTree(structmap, getProjectEntity()); return document; }