Advertisement
Guest User

Untitled

a guest
Nov 14th, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.86 KB | None | 0 0
  1. import javax.xml.parsers.DocumentBuilderFactory;
  2. import javax.xml.parsers.DocumentBuilder;
  3. import javax.xml.transform.Transformer;
  4. import javax.xml.transform.TransformerFactory;
  5. import javax.xml.transform.dom.DOMSource;
  6. import javax.xml.transform.stream.StreamResult;
  7.  
  8. import org.w3c.dom.Document;
  9. import org.w3c.dom.NodeList;
  10. import org.w3c.dom.Node;
  11. import org.w3c.dom.Element;
  12. import java.io.File;
  13.  
  14. public class Main {  
  15.   public static void main(String[] args) {
  16.     try {
  17.       String filepath = "D:\\Eclipse workspace\\Fraktale Java\\files\\rect_out";
  18.       File fXmlFile = new File(filepath + ".xml");
  19.       DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
  20.       DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
  21.       Document doc = dBuilder.parse(fXmlFile);
  22.       doc.getDocumentElement().normalize();      
  23.      
  24.       float oldSize   = 0.f;
  25.       float newSize   = 0.f;
  26.       float oldX    = 0.f;
  27.       float oldY    = 0.f;
  28.      
  29.       Element root = doc.getDocumentElement();
  30.      
  31.       System.out.println("Root element :" + root.getNodeName());
  32.       NodeList nList = doc.getChildNodes();
  33.       System.out.println("-----------------------");
  34.       System.out.println(nList.item(0).getChildNodes().getLength());
  35.       //int rectCount = 0;
  36.      
  37.       for (int temp = 0; temp < nList.getLength(); temp++) {
  38.  
  39.         Node nNode = nList.item(temp);
  40.        
  41.         if (nNode.getNodeType() == Node.ELEMENT_NODE) {        
  42.           Element eElement = (Element) nNode;
  43.          
  44.           oldSize = Float.parseFloat(getAttributeValue("width", (Element) (getTag("rect", eElement))));
  45.           newSize = oldSize/3;
  46.           setAttributeValue("width", Float.toString(newSize), (Element) (getTag("rect", eElement)));
  47.           setAttributeValue("height", Float.toString(newSize), (Element) (getTag("rect", eElement)));
  48.          
  49.           nNode.removeChild(getTag("rect", eElement));
  50.          
  51.           Element[] newRects = new Element[8];
  52.           float x = 0;
  53.           float y = 0;
  54.           for(int i = 0; i < newRects.length; i++) {
  55.             if((x == 1) && (y == 1))
  56.               x++;
  57.            
  58.             newRects[i] = doc.createElement("rect");
  59.             newRects[i].setAttribute("x", Float.toString(oldX+x*newSize));
  60.             newRects[i].setAttribute("y", Float.toString(oldY+y*newSize));
  61.             newRects[i].setAttribute("width", Float.toString(newSize));
  62.             newRects[i].setAttribute("height", Float.toString(newSize));
  63.             newRects[i].setAttribute("fill", "white");
  64.             root.appendChild(newRects[i]);
  65.             if(x == 2) {
  66.               x = 0;
  67.               y++;
  68.             }
  69.             else
  70.               x++;
  71.           }
  72.         }
  73.       }      
  74.      
  75.       TransformerFactory transformerFactory = TransformerFactory.newInstance();
  76.       Transformer transformer = transformerFactory.newTransformer();
  77.       DOMSource source = new DOMSource(doc);
  78.       StreamResult result = new StreamResult(new File(filepath + "_out.xml"));
  79.       transformer.transform(source, result);
  80.      
  81.     } catch (Exception e) {
  82.       e.printStackTrace();
  83.     }
  84.   }
  85.    
  86.   private static String getTagValue(String sTag, Element eElement) {
  87.     NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
  88.        
  89.     Node nValue = (Node) nlList.item(0);
  90.        
  91.     return nValue.getNodeValue();
  92.   }
  93.  
  94.   private static Node getTag(String sTag, Element eElement) {
  95.     Node nNode = eElement.getElementsByTagName(sTag).item(0);
  96.        
  97.     return nNode;
  98.   }
  99.    
  100.   private static String getAttributeValue(String sAttribute, Element eElement) {
  101.     return eElement.getAttribute(sAttribute);
  102.   }
  103.  
  104.   private static void setAttributeValue(String sAttribute, String sValue, Element eElement) {
  105.     eElement.setAttribute(sAttribute, sValue);
  106.   }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement