package uloha1; import java.io.File; import java.io.IOException; import java.net.URI; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; /************************************************************************* * Take this skeleton code and implement the missing body of * changeName, getWageCosts, and * removeCarsProducedEarlierThan methods. * Everything else should be left untouched.
* * After completion, the main method should work correctly.
* * You may use either NetBeans or Ant build.xml to compile and run it. */ public class Uloha1 { /** * W3C object model representation of a xml document. * Note: We use the interface(!) not its implementation */ private Document doc; /************************************************************************* * Method changing the (first) name * At person(s) having surname, change the name * (text content within name element) to newName. * * Other persons should not be affected. */ public void changeName(String surname, String newName) { NodeList list = doc.getElementsByTagName("person"); for (int i=0;i carsToRemove = new LinkedList(); for (int i=0;iconvertYear) carsToRemove.add(oneCar); } } int count = carsToRemove.size(); for (Node car:carsToRemove) { Node parent = car.getParentNode(); parent.removeChild(car); } return count; } /*************************************************************************/ /** * Vytvori novou instanci teto tridy a nacte obsah * xml dokumentu se zadanym URL. */ public static Uloha1 newInstance(URI uri) throws SAXException, ParserConfigurationException, IOException { return new Uloha1(uri); } /** * Create a new instance of this class and read the content * of the given XML file. */ public static Uloha1 newInstance(File file) throws SAXException, ParserConfigurationException, IOException { return newInstance(file.toURI()); } /** * Constructor creating a new instance of Uloha1 class * reading from the file at the given URI */ private Uloha1(URI uri) throws SAXException, ParserConfigurationException, IOException { // Vytvorime instanci tovarni tridy DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // Pomoci tovarni tridy ziskame instanci DocumentBuilderu DocumentBuilder builder = factory.newDocumentBuilder(); // DocumentBuilder pouzijeme pro zpracovani XML dokumentu // a ziskame model dokumentu ve formatu W3C DOM doc = builder.parse(uri.toString()); } public void serializetoXML(URI output) throws IOException, TransformerConfigurationException, TransformerException { // Vytvorime instanci tovarni tridy TransformerFactory factory = TransformerFactory.newInstance(); // Pomoci tovarni tridy ziskame instanci tzv. kopirovaciho transformeru Transformer transformer = factory.newTransformer(); // Vstupem transformace bude dokument v pameti DOMSource source = new DOMSource(doc); // Vystupem transformace bude vystupni soubor StreamResult result = new StreamResult(output.toString()); // Provedeme transformaci transformer.transform(source, result); } public void serializetoXML(File output) throws IOException, TransformerException { serializetoXML(output.toURI()); } public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException, TransformerException { if (args.length < 1) { System.err.println("Chybi argument se jmenem souboru pro " + "zpracovani!"); return; } else if (args.length < 2) { System.err.println("Chybi argument se jmenem vystupniho souboru!"); return; } File input = new File(args[0]); File output = new File(args[1]); Uloha1 uloha1 = newInstance(input); System.out.println(uloha1.getAverageCarAge()); uloha1.changeName("Hroza", "Honzík"); uloha1.changeName("Novák", "Liborek"); int carsRemoved = uloha1.removeCarsProducedEarlierThan(1996); System.out.println("Removed " + carsRemoved + " cars older than 1996."); uloha1.serializetoXML(output); } /** * Help method for testing purposes. */ Document getDocument() { return doc; } }