Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate XML mapping of a recursive directory search

I am reading a list that has a large number of archives under certain components. e.g.

component1:filelocation1/a11.ear
component1:filelocation1/a12.ear
component2:filelocation2/a2.ear
component3:filelocation3/a3.ear
component4:filelocation3/basefile.properties

I need to unzip each archive recursively till the last level of data and generate an XML mapping file capturing the correct mapping from the component to the last element in the archive.

The xml document structure of which would be like:

<my-app>
    <mapping>
    <toplevel loc="filelocation1" filename="a11.ear" component="component1">
        <childlevel loc="." filename="x1.war">
          <childlevel loc="WEB-INF/classes" filename="abc1.class"/>
          <childlevel loc="WEB-INF/classes" filename="abc2.class"/>
        </childlevel>
    </toplevel> 
    <toplevel loc="filelocation1" filename="a12.ear" component="component1">
      <childlevel loc="." filename="x2.jar">
      <childlevel loc="org/test" filename="abc1.class"/>
      <childlevel loc="org/test" filename="abc2.class"/>
      </childlevel>
      <childlevel loc="." filename="x3.war">
          <childlevel loc="WEB-INF/lib" filename="web1.jar">
          <childlevel loc="org/test" filename="abc1.class"/>
      </childlevel>
      <childlevel loc="WEB-INF/classes" filename="abc2.class"/>
      </childlevel>
    </toplevel> 
    </mapping>
    </my-app>

What is the best appraoch to do that? I am considering using a DOM parser to generate the XML.

like image 505
rommel Avatar asked Nov 13 '22 09:11

rommel


1 Answers

Since JAR files are also ZIP files, if you are going to do this in Java, I would use the java.util.zip library. Although you will still have to recursively open any JARs embedded in WARs and EARs, it will save you the trouble of stepping through the directories containing flat files. You can also use the JarFile subclass of the ZipFile offered by the java.util.zip library.

http://docs.oracle.com/javase/6/docs/api/java/util/zip/ZipFile.html

I also probably wouldn't bother with a DOM parser for just printing out XML. You'd be building a (potentially big) structure in memory when you could instead be printing data to a stream as you go. Also, parsers are for parsing XML into a data structure, not vice versa. The standard Java DOM parsing classes, javax.xml.parsers.DocumentBuilderFactory and javax.xml.parsers.DocumentBuilder don't come with a "print" command. The standard way to create formatted text output from a org.w3c.dom.Document object is to use an XSL transformer (See http://java.sun.com/webservices/reference/tutorials/jaxp/html/xslt.html#gghkq) and again, that's probably more trouble than it's worth. I suppose it depends on how much you want to extend this program, but if what you have here is all it has to do, I wouldn't build a big DOM object.

like image 70
k-den Avatar answered Nov 16 '22 04:11

k-den