1 2 Previous Next 20 Replies Latest reply: Jun 3, 2008 11:15 AM by 807591 RSS

    Edit ODT Files

    807591
      Hi Friends,

      I need some ideas here.

      An ODT file, I noticed, is a zip file containing, amongst others, an xml file that one can get the text contents of the file from.

      But that may not be of interest, or may it? Suppose a file exists and it contains the text

      "We are all equal"

      that I want to load into a JTextArea. Then I will edit that text to, say,

      "Sote tuko sawa" (which is the Swahili translation for the phrase)

      and on click saveButton, it should do the appropriate of swapping the two sentences (editing) and then saving the file, still in odt format.

      Anyone?

      Thanks.
        • 1. Re: Edit ODT Files
          807591
          Anyone?
          • 2. Re: Edit ODT Files
            807591
            Sorry about the spamming but, please, need your help.
            • 3. Re: Edit ODT Files
              masijade
              So what's your question? I've seen a lot of statements in that post, but not one relevant question.
              • 4. Re: Edit ODT Files
                807591
                The question is, therefore, how do I open a .odt file in a swing JTextArea?

                And how do I save some text into .odt format from a JTextArea?
                • 5. Re: Edit ODT Files
                  masijade
                  Well, what do you think?

                  Use the Zip classes to extract the XML, parse that with an XML parser, pasting the text (how you keep the text and the xml tags square is your problem, hopefully all of the text is contained in one tag or you're in for fun if you insist on using a textarea to display it), then rewriting the xml file and using the zip classes to reconstruct the "odt" file (if what you said/assumed in the first post is true).

                  Then again, it might help to know exactly what an "ODT" file is. If it is written by a specfifc application, it might be helpful to google for that application file format (someone may already have broken it down and either "published" it, or written an API for it).

                  Edit: Aaaacch, I'm an idiot. ODT. Why not just say OpenOffice from the beginning. My God, I hate it when people refer to the file format with the file suffix. Just because Windoof orders them on that criteria, does not mean it is the correct way to do it (as a matter of fact, it is very often the wrong way to do it).

                  No, you cannot simply display an "ODT" file in a textarea.
                  • 6. Re: Edit ODT Files
                    JoachimSauer
                    Jamwa wrote:
                    The question is, therefore, how do I open a .odt file in a swing JTextArea?

                    And how do I save some text into .odt format from a JTextArea?
                    Do you know OpenOffice.org? The 100+ MB package of software? A lot of that is dedicated to "open a .odt in a text area". Granted you seem to require a lot less functionality, but the task that you attempt to do is not a trivial one (just in case you thought it would be as simple as textArea.loadOdt("foo.odt"), it is not!).

                    You'll have to get the ODF specification (Google it, it's produced by OASIS) and find out how the file is structured. Then you'll need to read the correct file inside the archive (the one that contains the text content), probably using DOM (so that you can later easily modify it). After you're done, you should probably modify the DOM to replace the text, write this DOM to a file (or stream) and create a new zip file (ending in .ODF of course).

                    Note that ODF requires some special properties of the zip file (for example there needs to be a small, uncompressed file at the very beginning of the ODF file, I can't remember it's name).

                    Alternatively you could google if there is already a library out there, that implements ODF access.
                    • 7. Re: Edit ODT Files
                      807591
                      Do you know OpenOffice.org? The 100+ MB package of software?
                      Yes I do.
                      • 8. Re: Edit ODT Files
                        807591
                        Edit: Aaaacch, I'm an idiot. ODT. Why not just say OpenOffice from the beginning.
                        My bad.
                        My God, I hate it when people refer to the file format with the file suffix. >Just because Windoof orders them on that criteria, does not mean it is >the correct way to do it (as a matter of fact, it is very often the wrong >way to do it).
                        Linux also saves in the form of, for instance, fileone.odt

                        >
                        No, you cannot simply display an "ODT" file in a textarea.
                        I guess this leads to the next question, my good people.

                        How do zip/unzip file packages using Java?
                        • 9. Re: Edit ODT Files
                          masijade
                          Jamwa wrote:
                          Linux also saves in the form of, for instance, fileone.odt
                          It does not use that as the defing criteria for the file type, however, as Windufus does.
                          I guess this leads to the next question, my good people.

                          How do zip/unzip file packages using Java?
                          As I already mentioned. If you look at the API you will see a number of classes with "Zip" in the name. Does this tell you something?
                          • 10. Re: Edit ODT Files
                            807591
                            As I already mentioned. If you look at the API you will see a number of classes with "Zip" in the name. Does this tell you something?
                            I dont get what you mean by this.
                            • 11. Re: Edit ODT Files
                              masijade
                              Go to
                              http://java.sun.com/javase/6/docs/api/index.html
                              and look at the lower lefthand list and you will see classes as follows:
                              ZipEntry
                              ZipError
                              ZipException
                              ZipFile
                              ZipInputStream
                              ZipOutputStream
                              • 12. Re: Edit ODT Files
                                807591
                                masijade. wrote:
                                Go to
                                http://java.sun.com/javase/6/docs/api/index.html
                                and look at the lower lefthand list and you will see classes as follows:
                                ZipEntry
                                ZipError
                                ZipException
                                ZipFile
                                ZipInputStream
                                ZipOutputStream
                                Thanks. Will do that
                                • 13. Re: Edit ODT Files
                                  807591
                                  Check out these pieces of code that I have so far managed to decipher:
                                  import java.util.*;
                                  import java.io.*;
                                  import java.util.zip.*;
                                  
                                  
                                  public class ZipLister
                                  {
                                  // Displays a list of the files contained in a zip file
                                       public static void main(String[] args)
                                       {
                                            try
                                            {
                                                         // Open the ZIP file
                                                         ZipFile zf = new ZipFile("filename.odt");
                                      
                                                         // Enumerate each entry
                                                         for (Enumeration entries = zf.entries(); entries.hasMoreElements();)
                                                  {
                                                                  // Get the entry name
                                                                  String zipEntryName = ((ZipEntry)entries.nextElement()).getName();
                                                      System.out.println(zipEntryName);
                                                          }
                                                }
                                            catch (IOException e)
                                            {
                                                 System.out.print(e);
                                            }
                                       }
                                  }
                                  which when I run gives the output
                                  root@localhost# java ZipLister
                                  mimetype
                                  Configurations2/statusbar/
                                  Configurations2/accelerator/current.xml
                                  Configurations2/floater/
                                  Configurations2/popupmenu/
                                  Configurations2/progressbar/
                                  Configurations2/menubar/
                                  Configurations2/toolbar/
                                  Configurations2/images/Bitmaps/
                                  content.xml
                                  styles.xml
                                  meta.xml
                                  Thumbnails/thumbnail.png
                                  settings.xml
                                  META-INF/manifest.xml
                                  and also the class file
                                  import java.util.*;
                                  import java.io.*;
                                  import java.util.zip.*;
                                  
                                  
                                  public class Unzipper
                                  {
                                  // Unzips a file in a given zip file
                                       public static void main(String[] args)
                                       {
                                            try 
                                            {
                                                         // Open the ZIP file
                                                         String inFilename = "filename.odt";
                                                         ZipInputStream in = new ZipInputStream(new FileInputStream(inFilename));
                                      
                                                         // Get the first entry
                                                         ZipEntry entry = in.getNextEntry();
                                      
                                                         // Open the output file
                                                         String outFilename = "o";
                                                         OutputStream out = new FileOutputStream(outFilename);
                                      
                                                         // Transfer bytes from the ZIP file to the output file
                                                         byte[] buf = new byte[1024];
                                                         int len;
                                                         while ((len = in.read(buf)) > 0) 
                                                 {
                                                                  out.write(buf, 0, len);
                                                         }
                                      
                                                         // Close the streams
                                                         out.close();
                                                         in.close();
                                                }
                                            catch (IOException e)
                                            { }
                                       }
                                  }
                                  ok. So far so good. However, Unzipper.java extracts only the first file in the odt file whereas I would like to extract the file called content.xml in the compressed odt file. What do I need to modify?
                                  • 14. Re: Edit ODT Files
                                    masijade
                                    Loop using
                                    ZipEntry entry = in.getNextEntry();
                                    1 2 Previous Next