Code review requested

Discussion in 'Java' started by Ed, Jul 19, 2004.

  1. Ed

    Ed Guest

    Hi All,

    I am starting out in Java. My development skill set is in another
    language and therefore am not sure of the best way to develop in Java
    (ie. performance, efficient, memory consumption, etc).

    I have written a program that accepts an XML document as an input in
    the form of a stream.
    I need to insert a filename into the XML document and output again as
    a stream.
    I have written what I think is the way to do it. I am sure there are
    better ways or things I have not considered.

    Specific questions I have:
    1. How do I convert special characters in the text without manually
    coding "replace '>' with >" ?
    2. Is treating it as a byte stream the best way? My intent is to
    avoid the overhead of using the Document approach I have read about
    for the sake of inserting one tag.
    3. To avoid namespace problems I deliberately insert just after the
    root tag. Is this appropriate? Is it fair to assume the second
    occurrence of ">" is the end of the second tag?
    4. What about Unicode? I am simply writing my new tag and filename
    out with no concern for proper Unicode handling. How can I improve
    that?
    Also, the comparison with the input byte to '>' does not consider
    Unicode, should it?

    Any advice or feedback will be greatly appreciated.

    Thanks,
    Ed

    The code:
    //note: the inputStream is as the name suggests, an InputStream.
    //The result is an OutputStream.
    ....
    try {
    //Filename
    filename = "C:/somepath/somefile.dat";

    /*
    * Search the byte stream for the end of the second tag.
    * The assumption is that the first tag is the <?xml?>
    * declaration.
    * The second is the start of the message type.
    */
    int closeTag = 0;
    int x = 0;
    while ((x = inputStream.read()) > 0){
    if (closeTag < 3){
    if ((char)x == '>'){ closeTag++; }
    }
    result.write(x);
    if (closeTag == 2){
    closeTag++;
    result.write("\r\n<SourceFilename>".getBytes());
    result.write(filename.getBytes());
    result.write("</SourceFilename>".getBytes());
    };
    }

    } catch (Exception e) {
    throw new Exception("Error:" + e.getMessage(), e);
    }
     
    Ed, Jul 19, 2004
    #1
    1. Advertising

  2. Ed

    Filip Larsen Guest

    Ed wrote

    > I have written a program that accepts an XML document as an input in
    > the form of a stream.
    > I need to insert a filename into the XML document and output again as
    > a stream.
    > I have written what I think is the way to do it. I am sure there are
    > better ways or things I have not considered.


    You may want to pay
    http://java.sun.com/xml/jaxp/dist/1.1/docs/tutorial/TOC.html a visit,
    especially part II about SAX.


    Regards,
    --
    Filip Larsen
     
    Filip Larsen, Jul 19, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ian Pilcher

    Code review requested

    Ian Pilcher, Dec 15, 2003, in forum: Java
    Replies:
    0
    Views:
    344
    Ian Pilcher
    Dec 15, 2003
  2. Mike Wahler
    Replies:
    1
    Views:
    395
    E. Robert Tisdale
    Oct 18, 2004
  3. Vyacheslav Kononenko

    Re: Code review requested, Accelerated C++

    Vyacheslav Kononenko, Oct 18, 2004, in forum: C++
    Replies:
    0
    Views:
    493
    Vyacheslav Kononenko
    Oct 18, 2004
  4. www
    Replies:
    51
    Views:
    1,487
  5. luserXtrog

    A little light code review requested

    luserXtrog, Aug 18, 2010, in forum: C Programming
    Replies:
    2
    Views:
    341
    luserXtrog
    Aug 18, 2010
Loading...

Share This Page