DateFormat parse method usage

Hi all,
I am using the following code to parse the date String object to Date Object -
======================================================================
if(dateString == null || dateString.trim().equals(""))
return null;
DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.SHORT,
Locale.getDefault());
Date d = null;
try
dateFormatter.setLenient(false);
d = dateFormatter.parse(dateString);
catch(ParseException pe)
e.printStackTrace();
=================================================================
This code works perfectly for all formats of String objects I give except for the format - "12/12/12/1" in which case it converts to following date object -
"Wed Dec 12 00:00:00 IST 2012" although its an invalid date String.
Please let me know how I could have correct validation done in such cases or if this is the right behavior for DateFormat class.
Thanks in advance!!

I'm not going to comment on how exactly the parse method does its business, but I can suggest you use a regex pattern to validate the input you will send the parse method.
Here is some info from the CodeProject website on date validation(Note that this is aimed at .NET - but regex works the same in Java as it does in .NET [i think]):
Dates: As with numbers, we need two validators: a key-press validator, and a completion validator. The key-press validator can be pretty simple, if we limit how our user enters the date. Let’s say that we want to validate for the U.S. date format mm/dd/yyyy. Here is a validator that will do that:
^([0-9]|/)*$
The regex reads: “Match any string that contains a sequence of zero or more characters, where each character is either a digit or a slash.” This validator will give the user immediate feedback if they enter an invalid character, such as an ‘a’.
Copy that regex to Regex Tester and give it a try. Note that the validation fails if the user enters dashes, instead of slashes, between the parts of the date. How could we increase the flexibility of our regex to accommodate dashes? Think about the question for a minute before moving on.
All we need to do is add a dash to the alternates group:
^([0-9]|/|-)*$
We could add other alternates to make the regex as flexible as it needs to be.
The completion validator does a final check to determine whether the input matches a complete date pattern:
^[0-2]?[1-9](/|-)[0-3]?[0-9](/|-)[1-2][0-9][0-9][0-9]$
The regex reads as follows: "Match any string that conforms to this pattern: The first character can be a 0, 1, or 2, and it may be omitted. The second character can be any number and is required. The next character can be a slash or a dash, and is required…” And so on. This regex differs from the ones we used before in that it specifies each character of the pattern—the pattern is more of a template than a formula.
Note the first character of the regex: ‘[0-2]’. This character points out that we can limit allowable digits to less than the full set. We can also expand them; for example, ‘[0-9A-F]’ would allow any hexadecimal digit. In fact, we can use this structure, known as a character class, to specify any set of characters we want. For example, the character class ‘[A-Z]’ allows capital letters, and ‘[A-Za-z]’ allows upper or lower-case letters.
Our date regex also points out some of the limitations of regex validation. Paste the date regex shown into Regex Tester and try out some dates. The regex does a pretty good job with run-of-the-mill dates, but it allows some patently invalid ones, such as ‘29/29/2006’, or ‘12/39/2006'. The regex is clearly not ‘bulletproof’.
We could beef up the regular expression with additional features to catch these invalid dates, but it may be simpler to simply use a bit of .NET in the completion validator:
bool isValid = DateTime.TryParse(dateString, out dummy);
We gain the additional benefit that .NET will check the date for leap year validity, and so on. As always, the choice comes down to: What is simpler? What is faster? What is more easily understood? In my shop, we use a regex for the key-press validator, and DateTime.TryParse() for the completion validator.
Oh - and there is another regex validator:
^[0-9]{4}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30)))$
which matches the yyyy-mm-dd date format and also validates month and number of days in a month. Could be improved as currently all leap year dates (yyyy-02-29) will validate.
Sorry if I couldn't be of more help...
Edited by: JoKiLlSyA on Oct 9, 2008 11:22 PM

Similar Messages

  • DateFormat.parse(String)

    can anybody tell me why I don't get a SHORT format:
    import java.util.*;
    import java.text.*;
    public class MyDate{
       public static void main(String[] args){
          Date date = makeDate("04/22/2003");
          System.out.println(date); // what i get looks more like FULL
        public static Date makeDate(String dateString){
           Date date = null;
           try {
              DateFormat fmt = DateFormat.getDateInstance(DateFormat.SHORT);
              date = fmt.parse(dateString);
          catch(ParseException e) {}
          return date;
    }Thank you.

    what I think is tripping you up is that a java.util.Date is not a String. It looks like you're thinking that you can format a java.util.Date object and somehow it is "11/23/2004" (or whatever) - but it's not - it's just a number. So, you have this String that you want to convert to a java.util.Date (which you correctly did with the parse method of the DateFormat class) but then you turn around and want to convert that java.util.Date to a String so you can put it into a List, so why not just put the original String into your List? Is it because you want to change the format? Fair enough, then you can use two DateFormat implementations, one to parse the incoming String to a Date, then the other to format the Date to the format you want. Or is it so you can do the sort chronologically? If that's the case, then you can write up a Comparator to do that, or you can store Dates in your list, sort them, and when it comes time to display this thing (if you ever do that) use a DateFormat implementation to convert those to a formatted String.
    Make sense?

  • DateFormat.parse() reads 12:00:00 as 00:00:00?

    Hi, i have a method, which uses DateFormat.parse() function, and i want to check whether a date and time input is valid. as you can see from the following code; i have defined the format as (dd-mm-yyyy at hh:mm:ss). the problem is, if i pass a string as (dd-mm-yyyy at 12:21:23), the method changes hour field to '00'. i.e., the line denoted as LINE N in the code prints out 'dd-mm-yyyy at 00:21:23'. i have tested other hours and they are all fine, 00 is interpreted as 00, 24 is 00 which is fine, but 12 is also parsed to 00.... any way to change this please?
    Many thanks!
    public boolean isValidDate(String dt)
            boolean isValid=false;
            DateFormat datef=new SimpleDateFormat("dd-MM-yyyy 'at' hh:mm:ss",Locale.UK);
            java.util.Date d = new java.util.Date();
            try{
                d=datef.parse(dt);
                //////////////////////  LINE N //////////////////////////////////////////
                System.out.println("is this right"+d.toString());
                /////////////////////  LINE N //////////////////////////////////////////
                int hr = d.getHours();
                System.out.println("This is the hour "+hr);
                if(hr<=18&&hr>=9)
                    isValid=true;
                    System.out.println("hour ok");
                else{System.out.println("hour not ok");}
            catch(ParseException pe)
                isValid=false;
            return isValid;       
        }

    use gregoriancalendar instead
    -->
    http://java.sun.com/j2se/1.4.2/docs/api/java/util/Greg
    orianCalendar.htmlTrue.
    @OP. You should never call getHours() on a date object, you should always use a Calendar.
    Kaj

  • Is is possible to allow multiple date format strings pass DateFormat.parse?

    I'm trying to use DateFormat.parse to validate a date in the 20/12/08 format. I would like to allow users to input 20-12-08 or 20 12 08.
    The code currently looks like this:
    formatter = DateFormat.getDateInstance(DateFormat.SMALL, US);It is currently throwing an exception if 10-12-08 is sent in where as 10/12/08 works just fine.
    Is there a alternate us locale that is more lenient?
    setLenient(true) didn't solve the problem for me. I think that would accept 32/12/08 not 10-12-08.
    I could parse the date string and replace - or ' ' with a /.. Is that best practice?
    What other options do I have? Could I use SimpleDateFormat somehow?
    Thanks for the input

    That just doesn't seem clean. It will work, but I would rather not have a valid application flow use an exception to get its job done.
    The application is also international, so many locales on one format is valid. The US user base seems to think they can type in the date however they want. So passed into the method is the locale, I would have to modify a lot of code to send in a list of valid locales.
    The best solution for me would be to have a locale that would allow - or ' ' or /. That way I still only have to make one call to df.parse and an exception produces an invalid date message. Is that possible?
    Thanks

  • Parse method hangs

    I am trying to to parse a file using DocumentBuilder. The parse() method hangs for no reason. There are no errors or exceptions thrown. I even used a StringReader but in vain. Pl see code below:
    The file is read correctly and i could even print the XML string out.
    System.out.println("Parsing XML file from performParsing():" +s);
              Document doc = null;
              DocumentBuilderFactory factory = null;
              DocumentBuilder builder = null;
              try{
                   factory = DocumentBuilderFactory.newInstance();
                   System.out.println("Parsing XML factory:" +factory);
                   builder = factory.newDocumentBuilder();
                   System.out.println("Parsing XML builder:" +builder);
                   URL url = new URL(s);
                   URLConnection urlConnection = url.openConnection();
                   InputStream in = urlConnection.getInputStream();
                   String XMLStr = getStringReaderFromFile(in); //private method that gives a string
                   StringReader stReader = new StringReader(XMLStr);
                   InputSource ins = new InputSource(stReader);
         doc = builder.parse(ins);--->IT HANGS HERE

    Not an expert on this but presumably you have the properties set up correctly.
    From javadoc ...
    DocumentBuilderFactory uses the system property javax.xml.parsers.XmlDocumentParserFactory to find the class to load. So you can change the parser by calling:
    System.setProperty("javax.xml.parsers.XmlDocumentParserFactory",
    "com.foo.myFactory");

  • URGENT:  javax.xml.parsers.DocumentBuilder.parse() method issue

    Hi folks,
    I have an urgent parsing problem. I developed an app with JDK1.4 and now it needs to run on JDK1.3.1. The problem is this:
    If I use the DocumentBuilder.parse() method shipped with JDK1.4 it will parse a document containing namespaces WITHOUT a namespace declaration in the root element. However, since XML support doesn't ship with JDK1.3.1 I need to use JAXP. If I try this with the DocumentBuilder.parse() version shipped with JAXP (latest version) the parse method throws a NullPointerException. If I put the namespace declaration in the document it will parse fine. I tried setting the DocumentBuilderFactory.setNamespaceAware() explicitly to false (should be the default) but no luck. Any ideas other than adding the declaration (long story why I don't want it there...)
    thanks

    example, with declaration of namespace:
    <?xml version="1.0" encoding="UTF-8"?>
    <pre:root xmlns:inn="http://blablabla/inn/version/1.0/">
    <pre:metadata>
    <pre:dublin-core>
    <pre:title>mytitle</pre:title>
    <pre:description>mydesc</pre:description>
    <pre:creator>bjmann</pre:creator>
    </inn:dublin-core>
    </pre:root>
    example, without declaration:
    <?xml version="1.0" encoding="UTF-8"?>
    <pre:root>
    <pre:metadata>
    <pre:dublin-core>
    <pre:title>mytitle</pre:title>
    <pre:description>mydesc</pre:description>
    <pre:creator>bjmann</pre:creator>
    </inn:dublin-core>
    </pre:root>
    this may help...

  • Syntax Error with JSON.Parse method to parse SharePoint List Items

    Hi All,
    I want to get SharePoint List data and bind that retrived data to the JQuery Grid Control.
    For this I used SPServices to get the SharePoint List data in SOAP Envelope. Now I need to parse the soap envelope and store the retrieved items in array to pass it to the Grid Control.
    While using the JSON.Parse(resporseText) method, Iam consistenly getting an Syntax Error!
    Could anyone help me with this ?
    Please find the SOAP Envelope I received from SP List as below:
    "<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetListItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/"><GetListItemsResult><listitems xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
    xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
    xmlns:rs='urn:schemas-microsoft-com:rowset'
    xmlns:z='#RowsetSchema'>
    <rs:data ItemCount="2">
    <z:row ows_OBNumber='112211.000000000' ows_Project_x0020_Name='Project 1' ows_MetaInfo='11;#' ows__ModerationStatus='0' ows__Level='1' ows_Title='Project 1' ows_ID='11' ows_UniqueId='11;#{FBBCBCF9-666D-42F9-92D7-67188C51BC9B}' ows_owshiddenversion='1' ows_FSObjType='11;#0' ows_Created='2015-01-12 10:25:06' ows_PermMask='0x7fffffffffffffff' ows_Modified='2015-01-12 10:25:06' ows_FileRef='11;#sites/Lists/Projects/11_.000' />
    <z:row ows_OBNumber='1122343.00000000' ows_Project_x0020_Name='Project 2 ' ows_MetaInfo='12;#' ows__ModerationStatus='0' ows__Level='1' ows_Title='Project 2' ows_ID='12' ows_UniqueId='12;#{0D772B76-68E4-4769-B6FF-6A269F9C7ABD}' ows_owshiddenversion='1' ows_FSObjType='12;#0' ows_Created='2015-01-12 10:33:48' ows_PermMask='0x7fffffffffffffff' ows_Modified='2015-01-12 10:33:48' ows_FileRef='12;#sites/Lists/Projects/12_.000' />
    </rs:data>
    </listitems></GetListItemsResult></GetListItemsResponse></soap:Body></soap:Envelope>"
    Any help on this will be greatly appreciated.
    Thanks In Advance.!

    Hi,
    According to your description, there is an issue when parsing result from the data retrieved using SPServices.
    By default, SPServices returns data as XML format, however, the JSON.parse() method “Throws a SyntaxError exception if the string to parse is not valid JSON”:
    https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
    To extract the data needed from the response, you can either take the demo below for as a reference:
    http://spservices.codeplex.com/wikipage?title=GetListItems
    Or use the jQuery.parseXML() function:
    http://api.jquery.com/jquery.parsexml/
    Best regards
    Patrick Liang
    TechNet Community Support

  • When passing Node to parse() method get NULL pointer exception.

    All,
    I trying to pass a Node to parse method to get the NodeType object back and getting
    null pointer exception.
    Example
    xml
    <root>
    <Test>
    <someData>ABC</someData>
    </Test>
    </root>
    if pass the Test node to get Test object back I am getting null pointer exception.
    Thanks for all ur help
    Shoeb

    file 2
    ~~~~
    import javax.swing.*;
    public class ExtendedPage extends BasePage
    private JTextField objJTextField = null;
    private JButton objJButtonBrowse = null;
    Change the line in bold to
    private JTextField objJTextField;
    And everything works.

  • Adobe Offline Form - Parse method is not possible for this type

    Hi All,
    I have developed an application for the offline scenario of interactive adobe form. I tried to load the adobe form from my desktop. After pressing the button "Display form" it throws an error "Parse method is not possible for this type".
    If I include wdContext.getNodeInfo().getAttribute("pdfObject").getModifiableSimpleType() in the doInit() method of the view I receive this error -
    com.sap.tc.webdynpro.progmodel.context.ContextException: MappedAttributeInfo(UploadView.pdfObject): must not modify the datatype of a mapped attribute
    When I comment it out and upload I receive the error enclosed -
    Parse method is not possible for this type
    Can someone please help me with a step by step solution to this problem?
    Any help is highly appreciated.
    Many thanks,
    Divya

    Hi Divya,
    Please try to do it as stated below:
         IWDAttributeInfo attInfo = wdContext.getNodeInfo().getAttribute("pdfObject");
         ISimpleTypeModifiable type = attInfo.getModifiableSimpleType();
         IWDModifiableBinaryType binaryType = (IWDModifiableBinaryType)type;
    Try putting the code in wdInit() or wdDoModifyView().
    Let me know if you still face the issue.
    Regards,
    Arafat

  • Possible to overwrite parse method in DOM with SAX Handler?

    Hi,
    Is it possible to overwrite the parse method within DOM?
    What a want to do is:
         private Node parseXml( String text ) throws SAXParseException, SAXException, IOException, ParserConfigurationException
              SAXParserFactory factory = SAXParserFactory.newInstance();
                   factory.setNamespaceAware(true);
                   SAXParser parser = factory.newSAXParser();
                   TestHandler handler = new TestHandler();
                   // Parse the file
                   parser.parse(new InputSource(new StringReader(text)), handler);
              return (Node)handler.getRoot();
         } //end parseXml()The reason I want to use this is that within SAX I can write my own line counter so I keep a count of which node is on which line of text.
    What I was thinking is that I could use SAX to return DOM Nodes with the line number attached, possibly using the node.setUserData() method?!
    I have tried to play around with it and it doen't seem to work, can anyone help?
    Cheers Alex

    I have managaed to re-write my SAX parser to create a JTree, this works perfectly I can move through the tree and each line of text that corresponds to the node is highlighted.
    My problem is however that in my application I have used the DOM structure throughout so some of the functionality is lost.
    Is I understand that JAXP uses both SAX and DOM together, so I was wondering if it is possible to combine my sax parse method within the DOM?
    If anything is unclear please say and I will try and explain better. The main reason for doing this is that I want to keep a reference to which line of text each node of the dom tree represents. I have not been able to implemnet one in DOM however using SAX I have managed.
    Many thanks,
    Alex

  • Failed to parse method name:

    Hi,
    I've the following environment:
    - Sun Solaris
    - WL 61 without service pack
    - jdk 1.3.1
    I wrote a simple Session Bean and make it IIOP enabled.
    For the Java-Corba client I use an IDL which describes
    the interfaces of the remote and home interface of the
    Session Bean.
    Everything works fine, that means I get the WL's NameService,
    home and the remote object. If I call a method of the remote
    object I got the following message:
    <Info> <IIOPEndPoint> <failed to parse method name: foundUnkownSubscriber>
    I found something in you newsgroup and you said the we should
    use SP2. Where can I get this SP2?
    Regards
    Andre Genser

    Andy Piper <[email protected]> wrote:
    "Andre Genser" <[email protected]> writes:
    I've the following environment:
    - Sun Solaris
    - WL 61 without service pack
    - jdk 1.3.1
    I wrote a simple Session Bean and make it IIOP enabled.
    For the Java-Corba client I use an IDL which describes
    the interfaces of the remote and home interface of the
    Session Bean.
    Everything works fine, that means I get the WL's NameService,
    home and the remote object. If I call a method of the remote
    object I got the following message:
    <Info> <IIOPEndPoint> <failed to parse method name: foundUnkownSubscriber>Are you sure you are spelling correctly? Seems like this should be
    foundUnknownSubscriber.
    andy
    -- Sorry Andy, it was the name but I didn't solved the problem. Now I got the following
    exception:
    <Apr 16, 2002 8:47:29 AM CEST> <Warning> <Dispatcher> <Error thrown by rmi server:
    'weblogic.rmi.cluster.ClusterableServerRef@111 - jvmid: '-5101001595216439928S:10.2.9.70:[8045,8045,9045,9045,8045,9045,-1]:agenser8045:myserver',
    oid: '273', implementation: '[BaseEJBObject] home: prototype.CallBackBean_w4a7u3_HomeImpl@29bced''
    java.lang.OutOfMemoryError
    <<no stack trace available>>
    >
    <Apr 16, 2002 8:47:29 AM CEST> <Error> <Kernel> <ExecuteRequest failed
    java.lang.OutOfMemoryError
    <<no stack trace available>>
    >
    What is wrong in my solution? Here is my simple IDL-File:
    module prototype{
         exception CallBackException {
         interface CallBack {
              void foundUnknownSubscriber( in string MSISDN) raises(CallBackException);
         interface CallBackFactory {
              CallBack create();
    Thanks.
    Andre

  • Regarding DocumentBuilder.parse method returns reference as null value.

    Hi
    We are in the process of migrating code which works on WebSphere/JBOSS to WLS..
    We are using DocumentBuilder object for parsing the Locale related xml data. I understand that the DocumentBuilder is provided by DocumentBuilderFactory which is an interface whose implementation is server specific.
    But when the Document Builder parse method is called with a valid file path, we are getting System.Out of doc.getFirstChild().getLocalName() as null (which ideally should have been the name of the first node). No Errors are observed .
    For WLS, the parser is giving the document object reference as null whereas in other servers like Websphere and JBOSS we are able to parse the same xml with proper object reference.
    Could you please help in this regard?
    Thanks and regards
    Anil

    Hi Vmotamar,
    If you think that the parsers available as part of WebLogic are causing the issues, Then you can use your Own Parsers while migrating to WebLogic Server. I mean Get the Parser Jar files which u used as part of JBoss or WebSphere and put it inside the "APP-INF\lib" directory of your EAR Application or Put them inside "WEB-INF\lib" of your WEBApplication...and then Apply ClassLoader Filtering feature provided by WebLogic ...which allows an application to use It's Own Jars\Classes ...
    Please refer to: http://forums.oracle.com/forums/thread.jspa?threadID=1109267&tstart=150 and in http://weblogic-wonders.com/weblogic/parsers_issues
    Thanks
    Jay SenSharma
    http://weblogic-wonders.com/weblogic/parsers_issues/ (WebLogic Wonders Are Here)

  • HTMLEditorKit.Parser.parse() method is not giving a proper HTMLDocument alw

    Hi,
    I am developing util program for HTML filter, for my project. I have find using HTMLDocument we can do that.
    But when I created I found few starnge things.
    HTMLEditorKit.Parser.parse() method is not giving a proper HTMLDocument always. the below application will show the HTML one if you comment the String str ="" ; and uncomment the next line.
    I am wondering why this happens.
    import java.io.StringReader;
    import java.io.StringWriter;
    import javax.swing.text.html.HTMLDocument;
    import javax.swing.text.html.HTMLEditorKit;
    import javax.swing.text.html.HTMLWriter;
    import javax.swing.text.html.parser.ParserDelegator;
    public class HTMLDocExample {
    public static void main(String args[]) throws Exception {
    System.out.println("start the parsing");
    String data = "dsfdssfsdf<A HREF='http://yahoo.com'>HTMLEditorKit.Parser</A><h1>dsfdsf</h1><table border='1' class='test'><tr><td><input type='text'>dfsf</input><select id='id1' name='name1'><option>fff</option><option>aaaaa</option></select><input type='text'/><a href=#></a></td></tr></table>";
    String str ="" ;
    //String str ="<TABLE >test</TABLE><TABLE >test</TABLE><TABLE >test</TABLE><TABLE >test</TABLE><TABLE >test</TABLE><TABLE >test</TABLE><TABLE >test</TABLE><TABLE >test</TABLE><TABLE >test</TABLE><TABLE >test</TABLE>" ;
    StringReader br = new StringReader(data + str );
    HTMLEditorKit htmlKit = new HTMLEditorKit();
    HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
    HTMLEditorKit.Parser parser = new ParserDelegator();
    HTMLEditorKit.ParserCallback callback = htmlDoc.getReader(0);
    parser.parse(br, callback, true);
    StringWriter writer = new StringWriter();
    HTMLWriter hWriter = new HTMLWriter(writer, htmlDoc);
    hWriter.write();
    String outPut = writer.toString();
    System.out.println(outPut);
    System.exit(0);
    }

    import java.io.StringReader;
    import java.io.StringWriter;
    import javax.swing.text.html.HTMLDocument;
    import javax.swing.text.html.HTMLEditorKit;
    import javax.swing.text.html.HTMLWriter;
    import javax.swing.text.html.parser.ParserDelegator;
    public class HTMLDocExample {
      public static void main(String args[]) throws Exception {
        System.out.println("start the parsing"); 
        String data = "dsfdssfsdf<A HREF='http://yahoo.com'>HTMLEditorKit.Parser</A><h1>dsfdsf</h1><table border='1' class='test'><tr><td><input type='text'>dfsf</input><select id='id1' name='name1'><option>fff</option><option>aaaaa</option></select><input type='text'/><a href=#></a></td></tr></table>";
        //String str ="" ;
        String str ="<TABLE >test</TABLE><TABLE >test</TABLE><TABLE >test</TABLE><TABLE >test</TABLE><TABLE >test</TABLE><TABLE >test</TABLE><TABLE >test</TABLE><TABLE >test</TABLE><TABLE >test</TABLE><TABLE >test</TABLE>" ;
        StringReader br = new StringReader(data + str );   
        HTMLEditorKit htmlKit = new HTMLEditorKit();
        HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
        HTMLEditorKit.Parser parser = new ParserDelegator();
        HTMLEditorKit.ParserCallback callback = htmlDoc.getReader(0);
        parser.parse(br, callback, true);
        StringWriter writer = new StringWriter();
        HTMLWriter hWriter = new HTMLWriter(writer, htmlDoc);
        hWriter.write();
        String outPut = writer.toString();       
        System.out.println(outPut);
        System.exit(0);
    }

  • Parse method of JAXP DocumentBuilder using InputStream

    I am having some problem. I am working on a generic XML messaging client server pair. The crux of it is that I am using the Transformer object to send data from the client to the server, using a Socket InputStream. The I do exactly the same from the server side to the client side.
    Example (client side):
    OutputStream out = socket.getOutputStream();
    Transformer transformer =
    TransformerFactory.newInstance().newTransformer();
    transformer.transform(new DOMSource(request),
    new StreamResult(out));
    Example (server side, in the thread for the connection):
    InputStream in = socket.getInputStream();
    DocumentBuilder builder =
    DocumentBuilderFactory.newInstance
    ().newDocumentBuilder();
    Document request = builder.parse(in);
    The problem I am experiencing is that the parse method on the server side is never completing. The parse method does not return at all. I can understand that this is due to the InputStream still being open, and need to know how can I let the parse method know that I am done writing to the OutputStream at the client side. Should I be sending an EOF character of sorts? If I should, what byte value represents this, and what do I need to do?
    Thank you
    Heinrich Pool

    Sorry hadn't noticed the Code Tags before. So the code formatted
    package de.gaskin.xml;
    import de.gaskin.buf.Buffer;
    import de.gaskin.io.LineArray;
    import de.gaskin.util.Debug;
    import java.net.Socket;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.io.ByteArrayOutputStream;
    import java.io.ByteArrayInputStream;
    import java.io.File;
    import java.util.Vector;
    import org.w3c.dom.Document;
    A Simple to use generic XML Document transfer based on sockets.
    <H2>Theory of Operation.</h2>
    <p>A client sends an array of lines (an XML document) prepended by a line
    that ONLY contains an integer (the number of lines in the document that
    follows).
    <p>The server reads the first line (so that it knows how many lines belong to
    the next document) it then reads the specified number of lines into memory and
    when all lines have been read in passes them (as an InputStream) to an
    XMLParser to produce an XML DOM that is made available through the<br>
    <code>getDocument()</code> method.
    <h2>API</h2>
    The API is very simple to use as the following templates demonstrate.
    <h3>Client</h3>
    <pre>
        public class MyXmlSocketClient extends XmlSocket {
           MyXmlSocketClient(String serverAddress, String fileName) {
              super(serverAddress, 4711);
              run(fileName);
    </pre>
    <h3>Server Worker</h3>
        <pre>
        public class MyXmlSocketServer extends XmlSocket {
           MyXmlSocketServer(Socket s) {
              super(s);
           public void run() {
              Document doc = getDocument();
              // Process document
        </pre>
    <h3>Server Listener</h3>
        <pre>
        public class MyXmlServer {
           ServerSocket ss;
           boolean      stop;
           MyXmlSocketServer() {
              stop = false;
              try {
                 ss = new ServerSocket(4711);
                 while (! stop) {
                    Socket s = ss.accept();
                    new Thread(new MyXmlServerSocket(s));
              catch (Exception x) {
                 x.printStackTrace();
        </pre>
    <P>Last updated 10th Jan 2001.
    @author  David. M. Gaskin.
    @version Version 1.0 Oct 2000
    @since    Version 1.0
    public class XmlSocket implements Runnable {
       final static boolean debug = false;
       protected BufferedReader  data;
      /* protected */ public PrintWriter     pw;
                 Socket          socket;
       public XmlSocket(String domain, int port) {
          try {
             socket = new Socket(domain, port);
             common(socket);
          catch (Exception x) {
    //       x.printStackTrace();
             throw new Error(x.toString());
       public XmlSocket(Socket s) {
          socket = s;
          common(s);
       void common(Socket s) {
          try {
             data = new BufferedReader(new InputStreamReader(s.getInputStream()));
             pw   = new PrintWriter(s.getOutputStream(), true);
          catch (Exception x) {
    //       x.printStackTrace();
             throw new Error(x.toString());
       public Document getDocument() {
          Document rc = null;
          try {
             String line = data.readLine();
             int    linesInDoc = Integer.parseInt(line.trim());
             if (linesInDoc != -1) {
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                for (int i = 0;i < linesInDoc;i++) {
                   line = data.readLine();
                   System.out.println(line);
                   byte[] byteLine = line.getBytes();
                   os.write(byteLine, 0, byteLine.length);
    //          System.out.println("END");
                rc = XmlFactory.readIn(new ByteArrayInputStream(os.toByteArray()));
          catch (Exception x) {
             x.printStackTrace();
          return rc;
       public void run(Vector lines) {
          int loopCnt = lines.size();
          if (debug) {
             println("" + loopCnt);
             for (int i = 0;i < loopCnt;i++) {
                println((String)lines.elementAt(i));
    //       pw.flush();
             println("-1");
          else {
             pw.println("" + loopCnt);
             for (int i = 0;i < loopCnt;i++) {
                pw.println((String)lines.elementAt(i));
    //       pw.flush();
             pw.println("-1");
          pw.flush();
       void println(String s) {
          System.out.println(s);
          pw.println(s);
       public void run() {
          Debug.d(this, "run", "S H O U L D   N E V E R   B E   I N V O K E D");
       public void run(String filename) {
          Buffer[] input = LineArray.readIn(filename);
          send(input);
       public void run(File file) {
          Buffer[] input = LineArray.readIn(file.getAbsolutePath());
          send(input);
       void send(Buffer[] input) {
          int loopCnt = input.length;
          pw.println("" + loopCnt);
          for (int i = 0;i < loopCnt;i++) {
             pw.println(input.toString());
    pw.flush();
    pw.println("-1");
    pw.flush();
    public int getDocIdx(String[] roots, Document doc) {
    int rc = -1;
    String rootName = doc.getDocumentElement().getTagName();
    int loopCnt = roots.length;
    for (int i = 0;i < loopCnt;i++) {
    if (rootName.equals(roots[i])) {
    rc = i;
    break;
    if (rc < 0)
    System.out.println(rootName + " N O T F O U N D");
    return rc;
    public Socket getSocket() {
    return socket;
    For testing only
    public static void main(String[] args) {
    XmlSocket xmlS = new XmlSocket("localhost", 36);
    xmlS.getDocument();

  • Parse method is not possible for this type

    I have a file upload component and one button in a view.
    I have created a binary type context element and mapped it with fileupload component.while clicking the submit button I am getting " Parse method is not possible for this type" exception.
    help me out.
    Thanks In advance

    Hi,
    Thanks for your response. I have written the following code in wddoinit():     
    IWDAttributeInfo attributeinfo = wdContext.getNodeInfo().getAttribute(IPrivateSubstanceDocView.IFileUpload02Element.DATA);
        attributeinfo.getModifiableSimpleType();
    fileUpload02 is my context.
    but I am getting a null pointer exception over here.
    can ypu please help it.
    Actually the case is this is a window, which is opening on click of a hyperlink on another View.
    With the action method I am calling this View.
    Thus on click of a hyperlink just I am opening a new  View then here I am a browse button etc...
    PLease help if you can

Maybe you are looking for