[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [emf-dev] Loading using streams



Boris,

You do have a resource (data.xml) and you're loading it with an EMF
Resource (XMLResource). Your only problem is that a URI should have been
passed to the XMLResourceImpl's constructor.

That said, I don't see why you're going through all this extra work of
creating a ByteArrayInputStream. You should just load it in the usual EMF
way:

    Resource resource = resourceSet.getResource(URI.createFileURI(new
File("data.xml")), true);

I'd recommend reading chapter's 2 and 13 of the EMF book
(http://www.amazon.com/exec/obidos/ASIN/0131425420/ref%3Dnosim/fc-20/103-9904445-8667840).

Frank.

P.S., as I said in my other reply, please post these questions to the emf
newsgroup, not here.





                                                                                                                                          
                      "Lublinsky,Boris                                                                                                    
                      S."                      To:       <emf-dev@xxxxxxxxxxx>                                                            
                      <Boris.Lublinsky@        cc:                                                                                        
                      CNA.com>                 Subject:  [emf-dev] Loading using streams                                                  
                      Sent by:                                                                                                            
                      emf-dev-admin@ecl                                                                                                   
                      ipse.org                                                                                                            
                                                                                                                                          
                                                                                                                                          
                      01/20/2004 10:52                                                                                                    
                      AM                                                                                                                  
                      Please respond to                                                                                                   
                      emf-dev                                                                                                             
                                                                                                                                          
                                                                                                                                          






      I was using emf serialization in my client code:


                                  java.lang.String data = null;
                                  try{

                                          // Read in data file and
                  stringify it
                                          File inpf = new File("data.xml");

                                          FileInputStream fis = new
                  FileInputStream(inpf);
                                          byte [] b = new byte[5000];
                                          int len = fis.read(b);
                                          data = new
                  java.lang.String(b,0,len);
                                  }
                                  catch(Exception e){}


                                  // Create input stream for resource
                                  ByteArrayInputStream ins = new
                  ByteArrayInputStream(data.getBytes());


                                  // Create resource
                                  Resource resource = new
                  XMLResourceImpl();


                                  // Create XMLMap, used for proper XML
                  mapping. This is an empty Map,
                                  // it is using generated annotations for
                  proper streaming
                                  XMLResource.XMLMap xmlMap = new
                  XMLMapImpl();


                                  // Create options map for loading
                                  Map options = new HashMap();


                                  // Populate options

                  options.put(XMLResource.OPTION_XML_MAP,xmlMap);


                                  // Initialize package, required for
                  putting package in the registry
                                  CnaPackage cPackage =
                  CnaPackageImpl.init();
                                  try{
                                          resource.load(ins,options);
                                  }
                                  catch(Exception e){
                                          e.printStackTrace();
                                  }


                                  // Get the root element from the resource

                                  Message message =
                  (Message)(resource.getContents().get(0));


                  This seems to be fairly straightforward code, but it does
                  not work, because I am not using resource, thus not
                  defining URL. I have two options. Either to create
                  resource just for the sake of URL or modify XMLHandler,
                  XMLLoadImpl and XMLResourceImpl to check for nulls. Is it
                  a bug? Feature? What is your recommendation?