Digital Reader


Published on

Digital Reader is an application software which enables to view and edit different text formats like PDF,TXT,XLS,RTF,HTML in a single platform.

Published in: Technology, Education
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Digital Reader

  1. 1. Government Engineering College, Hassan 573 201Visvesvaraya Technological University, Belgaum Project Report on Digital Reader 4GH08CS010 Chethan.H.A 4GH09CS402 Gowtham.A.M 4GH08CS034 Pavan.P.Naik 4GH08CS058 Yogesh.K.S Under the Guidance of Mr. Annaiah HB.E.,M.T ech., Asst. Professor Dept. of Computer Science & Engineering GEC, HassanDepartment of Computer Science & Engineering Government Engineering College Hassan June, 2012
  2. 2. Government Engineering College, Hassan 573 201 Visvesvaraya Technological University, Belgaum Certificate This is to certify that the project work entitled “Digital Reader” is a bonafide workcarried out by Chethan H.A(4GH08CS010), Gowtham A.M(4GH09CS402), Pavan PNaik(4GH08CS034), Yogesh K.S(4GH08CS058) in partial fulfillment of the award of thedegree of Bachelor of Engineering in Computer Science & Engineering of VisvesvarayaTechnological University, Belgaum, during the year 2011 - 2012. It is certified that allcorrections / suggestions indicated during internal evaluation have been incorporated inthe report. The project report has been approved as it satisfies the academic require-ments in respect of the project work prescribed for the Bachelor of Engineering Degree. Guide Head of the Department Mr. Annaiah H B.E.,M.T ech. Dr.K.C.RavishankarB.E.,M.T ech.,P h.D. Assistant Professor Professor and Head Dept of CS & E Dept of CS & E GEC, Hassan 573 201 GEC, Hassan 573 201 Principal Dr. KarisiddappaB.E.,M.T ech.,P h.D. Principal GEC, Hassan 573 201 Examiners : 1. 2. Date : Place : Hassan
  3. 3. Acknowledgement At the outset we express our most sincere grateful thanks to our GuideAnnaiah H,Asst.Professor Department of CS & E, for his continoussupport and advice not only during the course of our project but also duringthe period of our stay in GECH.We express our gratitude to Dr.K.C. Ravishankar, Professor and Head,Department of CS & E for his encouragement and support throughout ourwork.We wish to express our thanks to our beloved Principal, Dr.Karisiddappa,for encouragement throughout our studies.Finally we express our gratitude to all teaching and non-teaching staff ofDept. of CS & E, fellow classmates and our parents for their timely supportand suggestions.Chethan H.AGowtham A.MPavan P NaikYogesh K.S i
  4. 4. Table of ContentsTable of Contents iiList of Figures iiiAbstract iv1 Introduction 1 1.1 Problem Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.2 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.3 Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.4 Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.5 Organization of the report . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Document Readers 4 2.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Requirement Analysis 6 3.1 Literature Survey . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 3.2 Technologies Used . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 3.3 JPedal Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 3.4 Eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 3.5 Java Runtime Environment(JRE) . . . . . . . . . . . . . . . . . . . . . . 22 3.6 Hardware Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 3.7 Software Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 Design and Implementation 25 4.1 Model-View-Controller(MVC) . . . . . . . . . . . . . . . . . . . . . . . . 25 4.2 Developing an HTML Viewer . . . . . . . . . . . . . . . . . . . . . . . . 26 4.3 Developing an XML Viewer . . . . . . . . . . . . . . . . . . . . . . . . . 28 4.4 Developing a Text Viewer . . . . . . . . . . . . . . . . . . . . . . . . . . 31 4.5 Developing an RTF Viewer . . . . . . . . . . . . . . . . . . . . . . . . . . 34 4.6 PDF Viewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 ii
  5. 5. 4.7 Text Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 4.8 File Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 4.9 Image Viewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425 Results and analysis 43 5.1 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 5.2 Types of Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 5.3 Testing with JUnit using Eclipse . . . . . . . . . . . . . . . . . . . . . . . 446 Conclusions and Future Enhancements 47References 48List of Figures3.1 Workspace launcher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183.2 Creating New Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183.3 Creating Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193.4 Create Java class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203.5 Run Java Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203.6 Creating the JAR file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213.7 Adding a library(.jar) to project . . . . . . . . . . . . . . . . . . . . . . . . . 224.1 MVC Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264.2 HTML Viewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294.3 Xml Viewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324.4 Text Viewer and editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354.5 RTF Viewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374.6 PDF Viewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384.7 Text search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404.8 File search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414.9 Image Viewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 iii
  6. 6. Abstract Digital Reader is a application software to view and edit and manage filesin Text(.txt), Portable Document Format (.pdf), Rich Text Form(.rtf) ,Hyper-Text Markup Language(.html) and Extensible Markup Language (.xml) andimage file formats like JPG.Digital Reader can be applicable to any operatingsystem and supports users to view and edit .txt , .pdf, .rtf, .html , .xml textfile extension fromats and also few image file formats. The grahical user in-terface of the Digital reader can be changed to the users wish.User can selecthis own text format to view or edit in this single platform and make easy ofthe viewing and editing process.Digital Reader representations are discrete, the information represented canbe discrete, such as numbers, letters or computer icons,images.Digital reader isa multi-format digital document reader software that is built using platformindependent Java language and designed User Interface with swings pack-ages.There are several readers available out there supporting one or the otherreadable document formats but there are none which can support multipleformats like pdf, rtf, html,xml text etc.,. But this software enables the userto read multiple document formats using the single software. Java is taken asa language for the development of digital reader because it is platform inde-pendent and supports rich user interface design with its swings package. Thisapplication makes it possible to save system resources in launching differentapplications for different documents as only one application can handle sev-eral document formats. iv
  7. 7. Chapter 1IntroductionDigital documents have become more in usage nowadays as compared to regular paperusage since it save both time and money with the paper and is also environment friendlyas use of paper involves cutting down trees to make them. So when there are digitaldocuments created, there must be a digital reader for reading those documents and usingthem for other purposes like printing, editing, etc. For this purpose there are severalapplications available as its readers.This application makes it possible to save system re-sources in launching different applications for different documents as only one applicationcan handle several document formats.This application makes it possible to save systemresources in launching different applications for different documents as only one appli-cation can handle several document formats.There are digital documents created, theremust be a digital reader for reading those documents.For this purpose there are severalapplications available as its readers.The readers available to view the documents differfrom format to format.Hence there exist many applications for viewing the documents.A filename extension is a suffix (separated from the basefilename by a dot) to the nameof a computer file applied to indicate the encoding (file format) of its contents or usage.Examples of filename extensions are .htm, .pdf, .xml and .txt.Filename extensions can beconsidered a type of metadata. They are commonly used to imply information about theway data might be stored in the file.Formatted text, styled text or rich text, as opposedto plain text, has styling information beyond the minimum of semantic elements: colours,styles (boldface, italic), sizes and special features (such as hyperlinks).Formatted text cannot rightly be identified with binary files or be distinct from ASCIItext. This is because formatted text is not necessarily binary, it may be text-only, suchas HTML, RTF or enriched text files, and it may be ASCII-only. Conversely, a plain textfile may be non-ASCII (in an encoding such as Unicode UTF-8). Text-only formattedtext is achieved by markup which too is textual, while some editors of formatted text like 1
  8. 8. Digital Reader Chapter 1Microsoft Word save in a binary format. For viewing Portable document format(pdf)document files there are readers like Adobe acrobat, foxit reader, Nitro pdf reader etc.,For viewing (Rich text format) rtf documents, there are software like WordPad, ABWord,MS Word, etc.,. Similarly there are other various software for viewing other documentformats.Adobe Acrobat is a family of application software developed by Adobe Systems to view,create, manipulate, print and manage files in Portable Document Format (PDF). Allmembers of the family, except Adobe Reader (formerly Acrobat Reader), are proprietarycommercial software, while the latter is available as freeware and can be downloadedfrom Adobe’s web site. Adobe Reader enables users to view and print PDF files but hasnegligible PDF creation capabilities. Acrobat and Reader are widely used as a way topresent information with a fixed layout similar to a paper publication.Microsoft Office Word is a word processor designed by Microsoft. It was first released in1983 under the name Multi-Tool Word for Xenix systems. Subsequent versions were laterwritten for several other platforms including IBM PCs running DOS (1983), the AppleMacintosh (1984), the ATT Unix PC (1985), Atari ST (1986), SCO UNIX, OS/2, andMicrosoft Windows (1989). It is a component of the Microsoft Office software system; itis also sold as a standalone product and included in Microsoft Works Suite.1.1 Problem StatementAlways there is a need for an application to open virtually any document format includingPDFs, TXTs, RTFs, HTMLs, XMLs, and Images that can used in any of the operat-ing systems provided there exists Java runtime environment(JRE) in the host OperatingSystem.1.2 ObjectivesPresenting a simple application called Digital Reader (DR) which supports the fol-lowing view modes and corresponding file formats which can work on different Operatingsystems and platforms. The aim and goal of this project is to create an application forreading multiple document formats. • Text/Binary/: all files (unlimited size) • RTF: RTF/ UTF-8 encoded filesDept. of CS & E, GEC, Hassan 2
  9. 9. Digital Reader Chapter 1 • Images: JPG. • Internet: all file types supported by Web Browsers (HTML/ XML etc) • Portable document format(PDF) with suitable view. • Suppotable for Microsoft Excel viewer.Digital Reader is a Unicode-compatible and can be opened in a click of a mouse.1.3 MotivationAs there was no application that can support most of the document formats, this wasthe motivation for us to develop this kind of application.1.4 ApplicationsSome of the applications can be listed as follows: • Support for multiple document formats such as text,pdf,rtf,html,xml. • Additonal utilities such as Filesearch and Textsearch. • Comparators for editable and non-editable documents. • Editors with Enable/Disable option.1.5 Organization of the reportBreif description or bird view of the remaining report topics.Dept. of CS & E, GEC, Hassan 3
  10. 10. Chapter 2Document Readers2.1 OverviewA Document Reader is an application software that presents the data stored in a com-puter file in a human-friendly form. The file contents are generally displayed on thescreen, or they may be printed. Also, they may be read aloud using speech synthesis.Document Readers may edit files, yet it is common for them to be able to save datain a different file format, or to copy information from the viewed file to the system-wide clipboard. They must have sufficient knowledge about the format of the file to beviewed. Even plain text files are not so simple file viewers may have to handle differentcode pages and newlinestyles.A fundamental type of Document Readers are filters thattranslate binary files into plain text (one example antiword). However, depending onthe competence of the translating routines, some information may be lost. Disassemblersalso fall in this category.Another common type of Document Reader is a picture viewerthat can display picture files of various formats. Common features here are thumbnailpreview and creation, and image zooming.For more complex or proprietary file formats, file viewers are usually provided by thesame companies that make the editing software using those formats (viewers may bedistributed free of charge, while editors have to be bought).A Document Reader is full-functionality software in the sense that it have a capabil-ity to create a file, or modify the content of an existing one. Instead, it is can be used todisplay or print the content.The primary reason behind this missing functionality is oftendue to marketing and control. For example, a popular software program, Adobe Acrobat,can be used to create content for most computer platforms, under various operating sys-tems. To ensure that people can access the documents created with Adobe Acrobat, the 4
  11. 11. Digital Reader Chapter 2software publisher created a viewer program, the Acrobat Reader, and made it availablefor free. This viewer application allows the content created by the proprietary authoringsoftware to be readable on all supported operating-system platforms, free of charge, thusmaking it a more attractive solution.There are many products that can qualify as a file viewer: Microsoft Word viewer orMicrosoft PowerPoint viewer, and the OpenOffice equivalents are examples. In a sense,a web browser is a type of Document Reader, which translates, or renders, the HTMLmarkups into a human-friendly presentation. Although HTML is plain text, viewing anHTML file in a browser and in a text editor produces significantly different results.Although web browsers are arguably the best Document Reader, since they supportmany graphic, multimedia, and document formats, they are likely to always be some-what lacking in the output quality and feature performance of more specialized softwarepackages. Creating and using alternative publishing systems and their accompanyingDocument Readers still makes a lot of business sense, given the content and presentationcontrol they provide.Dept. of CS & E, GEC, Hassan 5
  12. 12. Chapter 3Requirement Analysis3.1 Literature SurveyTo study and analyze more about the various document formats, the following literaturesurvey has done and discussed in this chapter.3.1.1 Portable Document Format(PDF)Portable Document Format (PDF) is an open standard for document exchange. Thisfile format, created by Adobe Systems in 1993, is used for representing documents ina manner independent of application software, hardware, and operating systems. EachPDF file encapsulates a complete description of a fixed-layout flat document, includingthe text, fonts, graphics, and other information needed to display it. While the PDFspecification has been available free of charge since at least 2001, PDF was originally aproprietary format controlled by Adobe. It was officially released as an open standardon July 1, 2008, and published by the International Organization for Standardization asISO 32000-1:2008. In 2008, Adobe published a Public Patent License to ISO 32000-1granting a royalty-free rights for all patents owned by Adobe that are necessary to make,use, sell and distribute PDF compliant implementations.3.1.2 Rich Text Format(RTF)The Rich Text Format (often abbreviated RTF) is a proprietary document file formatwith published specification developed by Microsoft Corporation since 1987 for Microsoftproducts and for cross-platform document interchange. 6
  13. 13. Digital Reader Chapter 3Most word processors are able to read and write some versions of RTF. There are sev-eral different revisions of RTF specification and portability of files will depend on whatversion of RTF is being used. RTF specifications are changed and published with majorMicrosoft Word and Office versions.It should not be confused with enriched text (mimetype ”text/enriched” of RFC 1896)or its predecessor Rich Text (mimetype ”text/richtext” of RFC 1341 and 1521); nor withIBM’s RFT-DCA (Revisable Format Text-Document Content Architecture) which arecompletely different specifications.3.1.3 Hypertext Markup Language (HTML)HTML is the markup language used for most web pages. E-books using HTML can beread using a Web browser. The specifications for the format are available without chargefrom the W3C.HTML adds specially marked meta-elements to otherwise plain text encoded using char-acter sets like ASCII or UTF-8. As such, suitably formatted files can be, and sometimesare, generated by hand using a plain text editor or programmer’s editor. Many HTMLgenerator applications exist to ease this process and often require less intricate knowledgeof the format details involved.HTML on its own is not a particularly efficient format to store information in, requiringmore storage space for a given work than many other formats. However, several e-Bookformats including the Amazon Kindle, Open eBook, Compressed HM, Mobipocket andEPUB store each book chapter in HTML format, then use ZIP compression to compressthe HTML data, images, metadata and style sheets into a single, significantly smaller,file.HTML files encompass a wide range of standards and displaying HTML files correctlycan be complicated. Additionally many of the features supported, such as forms, are notrelevant to e-books.3.1.4 Text Document(.txt)A text file (sometimes spelled ”textfile”: an old alternate name is ”flatfile”) is a kindof computer file that is structured as a sequence of lines of electronic text. A text fileexists within a computer file system. The end of a text file is often denoted by placing oneor more special characters, known as an end-of-file marker, after the last line in a text file.Dept. of CS & E, GEC, Hassan 7
  14. 14. Digital Reader Chapter 3”Text file” refers to a type of container, while plain text refers to a type of content.Text files can contain plain text, but they are not limited to such.At a generic level ofdescription, there are two kinds of computer files: text files and binary files.3.1.5 Extensible Markup Language (.xml)Extensible Markup Language (XML) is a markup language that defines a set of rules forencoding documents in a format that is both human-readable and machine-readable. Itis defined in the XML 1.0 Specification produced by the W3C, and several other relatedspecifications, all gratis open standards.The design goals of XML emphasize simplicity, generality, and usability over the In-ternet. It is a textual data format with strong support via Unicode for the languages ofthe world. Although the design of XML focuses on documents, it is widely used for therepresentation of arbitrary data structures, for example in web services.Many application programming interfaces (APIs) have been developed for software de-velopers to use to process XML data, and several schema systems exist to aid in thedefinition of XML-based languages.As of 2009, hundreds of XML-based languages have been developed, including RSS,Atom, SOAP, and XHTML. XML-based formats have become the default for many office-productivity tools, including Microsoft Office (Office Open XML), andLibreOffice (OpenDocument), and Apple’s iWork. XML has also been employed as thebase language for communication protocols, such as XMPP.3.1.6 Image viewerAn image viewer or image browser is a computer program that can display stored graphi-cal image; it can often handle various graphics file formats. Such software usually rendersthe image according to properties of the display such as color depth, display resolution,and color profile.Although you may use a full-featured bitmap graphics editor (such as Photoshop orthe GIMP or the StylePix) as an image viewer, these have many editing functionalitiesDept. of CS & E, GEC, Hassan 8
  15. 15. Digital Reader Chapter 3which are not needed for just viewing images, and therefore usually start rather slowly.Also, most viewers have functionalities that editors usually lack, such as stepping throughall the images in a directory (possibly as a slideshow).Image viewers give maximal flexibility to the user by providing a direct view of thedirectory structure available on a hard disk. Most image viewers do not provide any kindof automatic organization of pictures and therefore the burden remains on the user tocreate and maintain their folder structure (using tag- or folder-based methods). How-ever, some image viewers also have features for organizing images, especially an imagedatabase, and hence can also be used as image organizers.Some image viewers, such as Windows Photo Viewer that comes with Windows oper-ating systems, change a JPEG image if it is rotated, resulting in loss of image quality;others offer lossless rotation.3.1.7 File SearchDesktop search is the name for the field of search tools which search the contents of auser’s own computer files, rather than searching the Internet. These tools are designed tofind information on the user’s PC, including web browser histories, e-mail archives, textdocuments, sound files, images and video.One of the main advantages of desktop search programs is that search results arrivein a few seconds; Windows search companion ”can be some help, but it searches throughWindows files and folders only, not e-mail or contact databases, and unless you enablethe Indexing Service (in Windows 2000 or XP), the Windows search tool is extremelyslow.” Windows Vista enables the indexing service by default.Desktop search is emerging as a concern for large firms for two main reasons: untappedproductivity and security. A commonly cited statistic states that 80 per cent of a com-pany’s data is locked up inside unstructured data the information stored on an enduser’s PC, the files and directories they’ve created on a network, documents stored inrepositories such as corporate intranets and a multitude of other locations. Moreover,many companies have structured or unstructured information stored in older file formatsto which they don’t have ready access.Desktop search engines build and maintain an index database to achieve reasonable per-formance when searching several gigabytes of data. Indexing usually takes place whenDept. of CS & E, GEC, Hassan 9
  16. 16. Digital Reader Chapter 3the computer is idle and most search applications can be set to suspend it if a portablecomputer is running on batteries, in order to save power. When indexing the files, desktopsearch tools collect three types of information about files: • file and directory names. • metadata, such as titles, authors, comments in file types such as MP3, PDF and JPEG. • content of supported documents.Besides programs that use indexing, there are many programs that open and search filesinstantly. Their disadvantage is that they can search only a certain directory, not theentire computer, but their great advantage is that they do not load the resources of com-puter with indexing. Furthermore, they always use the current status of the documents.3.1.8 Text SearchA search engine is an information retrieval system designed to help find informationstored on a computer system. The search results are usually presented in a list and arecommonly called hits. Search engines help to minimize the time required to find informa-tion and the amount of information which must be consulted, akin to other techniquesfor managing information overload.The most public, visible form of a search engine is a Web search engine which searchesfor information on the World Wide Web.Search engines provide an interface to a group of items that enables users to specifycriteria about an item of interest and have the engine find the matching items. Thecriteria are referred to as a search query. In the case of text search engines, the searchquery is typically expressed as a set of words that identify the desired concept that oneor moredocuments may contain. There are several styles of search query syntax thatvary in strictness. It can also switch names within the search engines from previous sites.Whereas some text search engines require users to enter two or three words separated bywhite space, other search engines may enable users to specify entire documents, pictures,sounds, and various forms of natural language. Some search engines apply improvementsto search queries to increase the likelihood of providing a quality set of items through aprocess known as query expansion.Dept. of CS & E, GEC, Hassan 10
  17. 17. Digital Reader Chapter 33.1.9 File comparisonFile comparison in computing compares the contents of computer files, finding their com-mon contents and their differences. The result of the comparison may be presented ina graphic user interface or as part of larger tasks in networks, file systems, or revisioncontrol.Some widely-used file comparison programs are diff, cmp, FileMerge, Araxis Merge, Win-Merge, Beyond Compare, and Microsoft File Compare.Many text editors and word processors perform file comparison to highlight the changesto a document.3.2 Technologies Used3.2.1 Java programming languageJava is a programming language originally developed by James Gosling at Sun Microsys-tems (which has since merged into Oracle Corporation) and released in 1995 as a corecomponent of Sun Microsystems’ Java platform. Java applications are typically compiledto bytecode (class file) that can run on any Java Virtual Machine (JVM) regardless ofcomputer architecture. Java is a general-purpose, concurrent, class-based, object-orientedlanguage that is specifically designed to have as few implementation dependencies as pos-sible. It is intended to let application developers ”write once, run anywhere” (WORA),meaning that code that runs on one platform does not need to be recompiled to runon another. Java is currently one of the most popular programming languages in use,particularly for client-server web applications.There were five primary goals in the creation of the Java language:Dept. of CS & E, GEC, Hassan 11
  18. 18. Digital Reader Chapter 3It should be simple, object-oriented and familiarIt should be robust and secureIt should be architecture-neutral and portableIt should execute with high performanceIt should be interpreted, threaded, and dynamic Java is an object-oriented programming language with a built-in application program-ming interface (API) that can handle graphics and user interfaces and that can be usedto create applications or applets. Because of its rich set of API’s, similar to Macintoshand Windows, and its platform independence, Java can also be thought of as a platformin itself. Java also has standard libraries for doing mathematics.Much of the syntax of Java is the same as C and C++. One major difference is that Javadoes not have pointers. However, the biggest difference is that you must write objectoriented code in Java. Procedural pieces of code can only be embedded in objects. In thefollowing we assume that the reader has some familiarity with a programming language.In particular, some familiarity with the syntax of C/C++ is useful.Major release versions of Java, along with their release dates:JDK 1.0 (January 23, 1996)JDK 1.1 (February 19, 1997)J2SE 1.2 (December 8, 1998)J2SE 1.3 (May 8, 2000)J2SE 1.4 (February 6, 2002)J2SE 5.0 (September 30, 2004)Java SE 6 (December 11, 2006)Java SE 7 (July 28, 2011)Sun has defined and supports four editions of Java targeting different application envi-ronments and segmented many of its APIs so that they belong to one of the platforms.The platforms are:Java Card for smartcards.Java Platform, Micro Edition (Java ME) targeting environments with limited resources.Java Platform, Standard Edition (Java SE) targeting workstation environments.Java Platform, Enterprise Edition (Java EE) targeting large distributed enterprise or In-ternet environments.The original and reference implementation Java compilers, virtual machines, and classlibraries were developed by Sun from 1995. As of May 2007, in compliance with the spec-Dept. of CS & E, GEC, Hassan 12
  19. 19. Digital Reader Chapter 3ifications of the Java Community Process, Sun relicensed most of its Java technologiesunder the GNU General Public License.In Java we distinguish between applications, which are programs that perform the samefunctions as those written in other programming languages, and applets, which are pro-grams that can be embedded in a Web page and accessed over the Internet. Our initialfocus will be on writing applications. When a program is compiled, a byte code is pro-duced that can be read and executed by any platform that can run Java.3.2.2 SwingsSwing is far from a simple component toolkit, however. It includes rich undo support,a highly customizable text package, integrated internationalization and accessibility sup-port. To truly leverage the cross-platform capabilities of the Java platform, Swing sup-ports numerous look and feels, including the ability to create your own look and feel.The ability to create a custom look and feel is made easier with Synth, a look and feelspecifically designed to be customized. Swing wouldn’t be a component toolkit withoutthe basic user interface primitives such as drag and drop, event handling, customizablepainting, and window management.Dept. of CS & E, GEC, Hassan 13
  20. 20. Digital Reader Chapter 3The Swing toolkit includes a rich set of components for building GUIs and adding inter-activity to Java applications. Swing includes all the components you would expect froma modern toolkit: table controls, list controls, tree controls, buttons, and labels.Swing is part of the Java Foundation Classes (JFC). The JFC also include other fea-tures important to a GUI program, such as the ability to add rich graphics functionalityand the ability to create a program that can work in different languages and by userswith different input devices.The following list shows some of the features that Swing and the Java Foundation Classesprovide.Swing GUI ComponentsThe Swing toolkit includes a rich array of components: from basic components, such asbuttons and check boxes, to rich and complex components, such as tables and text. Evendeceptively simple components, such as text fields, offer sophisticated functionality, suchas formatted text input or password field behavior. There are file browsers and dialogsto suit most needs, and if not, customization is possible. If none of Swing’s providedDept. of CS & E, GEC, Hassan 14
  21. 21. Digital Reader Chapter 3components are exactly what you need, you can leverage the basic Swing componentfunctionality to create your own.Java 2D APITo make your application stand out; convey information visually; or add figures, images,or animation to your GUI, you’ll want to use the Java 2D API. Because Swing is built onthe 2D package, it’s trivial to make use of 2D within Swing components. Adding images,drop shadows, compositing it’s easy with Java 2D.Pluggable Look-and-Feel SupportAny program that uses Swing components has a choice of look and feel. The classesshipped by Oracle provide a look and feel that matches that of the platform. The Synthpackage allows you to create your own look and feel. The GTK+ look and feel makeshundreds of existing look and feels available to Swing programs.A program can specify the look and feel of the platform it is running on, or it canspecify to always use the Java look and feel, and without recompiling, it will just work.Or, you can ignore the issue and let the UI manager sort it out.Data TransferData transfer, via cut, copy, paste, and drag and drop, is essential to almost any applica-tion. Support for data transfer is built into Swing and works between Swing componentswithin an application, between Java applications, and between Java and native applica-tions.InternationalizationThis feature allows developers to build applications that can interact with users world-wide in their own languages and cultural conventions. Applications can be created thataccept input in languages that use thousands of different characters, such as Japanese,Chinese, or Korean.Swing’s layout managers make it easy to honor a particular orientation required by theUI. For example, the UI will appear right to left in a locale where the text flows right toDept. of CS & E, GEC, Hassan 15
  22. 22. Digital Reader Chapter 3left. This support is automatic: You need only code the UI once and then it will workfor left to right and right to left, as well as honor the appropriate size of components thatchange as you localize the text.Accessibility APIPeople with disabilities use special software assistive technologies that mediates the userexperience for them. Such software needs to obtain a wealth of information about therunning application in order to represent it in alternate media: for a screen reader to readthe screen with synthetic speech or render it via a Braille display, for a screen magnifierto track the caret and keyboard focus, for on-screen keyboards to present dynamic key-boards of the menu choices and toolbar items and dialog controls, and for voice controlsystems to know what the user can control with his or her voice. The accessibility APIenables these assistive technologies to get the information they need, and to program-matically manipulate the elements that make up the graphical user interface.Undo Framework APISwing’s undo framework allows developers to provide support for undo and redo. Undosupport is built in to Swing’s text component. For other components, Swing supportsan unlimited number of actions to undo and redo, and is easily adapted to an applica-tion. For example, you could easily enable undo to add and remove elements from a table.Flexible Deployment SupportIf you want your program to run within a browser window, you can create it as an appletand run it using Java Plug-in, which supports a variety of browsers, such as InternetExplorer, Firefox, and Safari. If you want to create a program that can be launched froma browser, you can do this with Java Web Start. Of course, your application can also runoutside of browser as a standard desktop application.3.3 JPedal LibraryJPedal is a Java library for viewing and manipulating PDF files. It aims to provide Javadevelopers with complete PDF reader functionality as well as PDF content extraction.The full version of JPedal is distributed under a commercial license with a cut downversion available under a LGPL license.Dept. of CS & E, GEC, Hassan 16
  23. 23. Digital Reader Chapter 3The full version of JPedal provides a PDF viewer with multiple page viewing modes,PDF to image conversion, printing, searching and text/image extraction. It can be usedas part of a client or server Swing or SWT application, thin client, applet, JavaFX, JSPor webstart.The LGPL versions lacks some of the functionality of the commercial versionsuch as printing, extraction and different page display options.3.4 EclipseEclipse is a multi-language software development environment comprising an integrateddevelopment environment (IDE) and an extensible plug-insystem. It is written mostlyin Java. It can be used to develop applications in Java and, by means of various plug-ins, other programming languagesincluding Ada, C, C++, COBOL, Haskell, Perl, PHP,Python, R, Ruby (including Ruby on Rails framework), Scala, Clojure, Groovy andScheme. It can also be used to develop packages for the software Mathematica. Develop-ment environments include the Eclipse Java development tools (JDT) for Java, EclipseCDT for C/C++, and Eclipse PDT for PHP, among others.3.4.1 Running EclipseAfter installing the Eclipse SDK in a directory, you can start the Workbench by runningthe Eclipse executable included with the release (you also need a 1.4.2 JRE). On Win-dows, the executable file is called eclipse.exe, and is located in the eclipse sub-directoryof the install.To start Eclipse double-click on the file eclipse.exe (Microsoft Windows) or eclipse (Linux/ Mac) in the directory where you unpacked Eclipse. The system will prompt you fora workspace. The workspace is the place where you store your Java projects (more onworkspaces later).Select an empty directory and press Ok.Figure 3.1 shows workspacelauncher.3.4.2 Creating New ProjectSelect from the menu File then New then project.Enter Digital Reader as the projectname.Select the flag Create separate folders for sources and class files.Figure 3.2 showscreating of new project.Dept. of CS & E, GEC, Hassan 17
  24. 24. Digital Reader Chapter 3 Figure 3.1: Workspace launcher Figure 3.2: Creating New ProjectPress finish to create the project. A new project is created and displayed as a folder.Open the Digital Reader folder and explore the content of this folder.Dept. of CS & E, GEC, Hassan 18
  25. 25. Digital Reader Chapter 33.4.3 Create packageCreate a new package. A good convention is to use the same name for the top packageand the project. Create therefore the package controller.Select the folder src, right click on it and select New then Package.Figure 3.3 shows cre-ating package. Figure 3.3: Creating Package3.4.4 Create Java classWe will now create a Java class. Right click on your package and select New then Class.Enter in a class name and click finish. Eclipse may yell at you and say ”The use of thedefault package is discouraged.” Packages in Java provide a nice means for organizingyour classes and this is a good thing. Nevertheless, for just getting this example working,we can skip thinking about packages.Figure 3.4 creating java class.Dept. of CS & E, GEC, Hassan 19
  26. 26. Digital Reader Chapter 3 Figure 3.4: Create Java class3.4.5 Run your project in EclipseNow run your code. Right click on your Java class and select Run-as then Java applica-tion.Figure 3.5 shows this.Finished! You can see the output in the console. Figure 3.5: Run Java ProjectDept. of CS & E, GEC, Hassan 20
  27. 27. Digital Reader Chapter 33.4.6 Prepare to run program outside Eclipse (create JAR file)To run your Java program outside of Eclipse you need to export it as a jar file. A jar fileis the standard distribution format for Java applications.Select your project, right click on it and select Export.Figure 3.6 shows creating of JARfile. Figure 3.6: Creating the JAR fileSelect JAR file, select Next and then Select your project and maintain the export desti-nation and a name for the jar file. we named it Digital Reader.jar.3.4.7 Adding a library (.jar ) to your projectThe following describes how to add Java libraries to your project. Java libraries aredistributed via ”jar” files. It assumes that you have a jar file available; if not feel free toskip this step.Create a new Java project Digital Reader Then, create a new folder called lib, by rightclicking on your project and selecting New then Folder.From the menu select File then Import then General then File System. Select yourDept. of CS & E, GEC, Hassan 21
  28. 28. Digital Reader Chapter 3jar and select the folder lib as target. Alternatively, just copy and paste your jar file intothe ”lib” folder.Right click on your project and select Properties. Under Java Build Path then Librariesselect the button ”Add JARs”.The following example shows how the result would looklike, if the junit-4.4.jar had been added to the project.Figure 3.7 shows the adding oflibrary to the project. Figure 3.7: Adding a library(.jar) to projectAfterwards you can use the classes contained in the jar file in your Java source code.Pressfinish. This creates a jar file in your selected output directory.3.5 Java Runtime Environment(JRE)The Java Runtime Environment (JRE) provides the libraries, the Java Virtual Machine,and other components to run applets and applications written in the Java programminglanguage. In addition, two key deployment technologies are part of the JRE: Java Plug-in, which enables applets to run in popular browsers; and Java Web Start, whichdeploysstandalone applications over a network.The Java Runtime Environment (JRE) includesthe JVM, as the JRE provides some standard libraries and the JVM which can be usedDept. of CS & E, GEC, Hassan 22
  29. 29. Digital Reader Chapter 3to execute a Java program.A Java virtual machine (JVM) is a virtual machine that canexecute Java bytecode.It is the code execution component of the Java software platform.A Java virtual ma-chine is software that is implemented on virtual and non-virtual hardware and on stan-dard operating systems. A JVM provides an environment in which Java bytecode canbe executed, enabling such features as automated exception handling, which providesroot- cause debugging information for every software error (exception), independent ofthe source code.A JVM is distributed along with a set of standard class libraries that implement the Javaapplication programming interface (API). Appropriate APIs bundled together with JVMform the Java Runtime Environment (JRE).JVMs are available for many hardware andsoftware platforms.Thus, the JVM is a crucial component of the Java platform.The Java Development Kit (JDK) is an Oracle Corporation product aimed at Javadevelopers. Since the introduction of Java, it has been by far the most widely used JavaSoftware Development Kit (SDK).The JDK has as its primary components a collection of programming tools, including:java :The loader for Java applications. This tool is an interpreter and can interpret theclass files generated by the javac compiler.javac :The compiler, which converts source code into Java bytecode.appletviewer :This tool can be used to run and debug Java applets without a web browser. The JDK also comes with a complete Java Runtime Environment, usually called aprivate runtime, due to the fact that it is separated from the regular JRE and has extracontents. It consists of a Java Virtual Machine and all of the class libraries present in theproduction environment, as well as additional libraries only useful to developers, such asthe internationalization libraries and the IDL libraries.The Java Developers Kit (JDK) also includes the JVM, standard class libraries, and sev-eral other tools that a developer needs in order to create a Java program.JRE containsthe runtime environment such as JVM and other Java classes (AWT, SWING), but doesnot contain any development tools such as a compiler or a debugger.Both JDK and JREcontains the JVM.3.6 Hardware RequirementsProcessor :Pentium III onwordsDept. of CS & E, GEC, Hassan 23
  30. 30. Digital Reader Chapter 3RAM:128MBHard disk space:5GBBest view in 1024x768 screen resolution3.7 Software RequirementsOperating System: Windows/Linux/MacRuntime Environment:JRE or JDKDept. of CS & E, GEC, Hassan 24
  31. 31. Chapter 4Design and Implementation4.1 Model-View-Controller(MVC)Model-view-controller (MVC) design is most commonly used with swings because of theirconnection and since weve implemented this project in swings, we chose MVC for ourdesign of User Interface. MVC was first introduced by Trygve Reenskaug, a Smalltalkdeveloper at the Xerox Palo Alto Research Center in 1979, and helps to decouple dataaccess and business logic from the manner in which it is displayed to the user. Moreprecisely, MVC can be broken down into three elements: • Model: The model represents data and the rules that govern access to and updates of this data. In enterprise software, a model often serves as a software approximation of a real-world process. • View: The view renders the contents of a model. It specifies exactly how the model data should be presented. If the model data changes, the view must update its presentation as needed. This can be achieved by using a push model, in which the view registers itself with the model for change notifications, or a pull model in which the view is responsible for calling the model when it needs to retrieve the most current data. • Controller: The controller translates the user’s interactions with the view into actions that the model will perform. In a stand-alone GUI client, user interactions could be button clicks or menu selections, whereas in an enterprise web application, they appear as GET and POST HTTP requests. Depending on the context, a controller may also select a new view for example, a web page of results to present back to the user.The figure 4.1 shows the MVC architecture. 25
  32. 32. Digital Reader Chapter 4 Figure 4.1: MVC Architecture4.2 Developing an HTML ViewerThe HTML Viewer can be developed using JTextPane and HTMLEditorKit to display andedit HTML documents. The following features are included: • Creating a new HTML document • Opening an existing HTML document • Saving changes • Saving the document under a new name/location • Prompting the user to save changes before loading a new document or exiting the application Class HtmlViewer This class extends JFrame to provide the supporting frame for this example. Severalinstance variables are declared: • JTextPane m editor: main text component.Dept. of CS & E, GEC, Hassan 26
  33. 33. Digital Reader Chapter 4 • StyleContext m context: a group of styles and their associated resources for the documents in this example. • HTMLDocument m doc: current document model. • HTMLEditorKit m kit: editor kit that knows how to read/write HTML documents. • SimpleFilter m HTMLFilter: file filter for .HTML files. • JToolBar m toolBar: toolbar containing New, Open, Save buttons. • JFileChooser m chooser: file chooser used to load and save HTML files. • File m currentFile: currently opened HTML file (if any). • boolean m textChanged: keeps track of whether any changes have been made since the document was last saved.The HtmlViewer constructor first instantiates our JTextPane and HTMLEditorKit, andassigns the editor kit to the text pane (it is important that this is done before any docu-ments are created). The editor component is then placed in a JScrollPane which is placedin the center of the frame. The JFileChooser component is created and an instance ofour SimpleFilter class is used as a filter to only allow the choice of HTML documents.A WindowListener is added to call our custom promptToSave() method to ensure thatthe user has the opportunity to save any changes before closing the application. ThisWindowListener also ensures that our editor component automatically receives the focuswhen this application regains the focus.The createMenuBar() method creates a menu bar with a single menu titled File anda tool bar with three buttons. Actions for New, Open, Save, Save As and Exit are cre-ated and added to the File menu. The New, Open and Save actions are also added tothe tool bar The important difference is that we use InputStreams and OutputStreamsrather than Readers and Writers. The reason for this is that HTML uses 1-byte encodingwhich is incompatible with the 2-byte encoding used by readers and writers. The getDocumentName() method simply returns the name of the file correspond-ing to the current document, or untitled if it hasnt been saved to disk.The newDocu-ment() method is responsible for creating a new HTMLDocument instance using HTM-LEditorKits createDefaultDocument() method. Once created our StyleContext variable,m context, is assiged to this new documents stylesheet with HTMLDocuments get-StyleSheet() method. The title of the frame is then updated and a Runnable instanceis created and sent to SwingUtilities.invokeLater() method to scroll the document to thebeginning when it is finished loading. Finally, an instance of our custom UpdateListenerDept. of CS & E, GEC, Hassan 27
  34. 34. Digital Reader Chapter 4class (described below) is added as a DocumentListener, and the m textChanged variableis set to false to indicate that no changes to the document have been made yet.The openDocument() is similar to the newDocument() method but uses the JFileChooserto allow selection of an existing HTML file to load, and uses an InputStream object toread the contents of that file. The saveFile() method takes a boolean parameter specify-ing whether the method should act as a Save As process or just a regular Save. If true,indicating a Save As process, the JFileChooser is displayed to allow the user to specifythe file and location to save the document to. An OutputStream is used to write thecontents of the document to the destination File.The promptToSave() method checks the m textChanged flag and, if true, displays aJOptionPane asking whether or not the current document should be saved. This methodis called before a new document is created, a document is opened or the application isclosed to ensure that the user has a chance to save any changes to the current documentbefore losing them.The showError() method is used to display error messages in a JOptionPane. It is oftenuseful to display exceptions to users so that they know an error happened and so thatthey may eventually report errors back to you if they are in fact bugs.Class UpdateListener This DocumentListener subclass is used to modify the state of our m textChangedvariable. Whenever an insertion, removal or document change is made this variable is setto true. This allows HtmlProcessors promptToSave() method to ensure the user has theoption of saving any changes before loading a new document or exiting the application.The figure 4.2 shows the snapshot of HTML Viewer.4.3 Developing an XML ViewerExtensible Markup Language (XML) is a markup language that defines a set of rules forencoding documents in a format that is both human-readable and machine-readable. Itis defined in the XML 1.0 Specification produced by the W3C, and several other relatedspecifications ,all gratis open standards.The design goals of XML emphasize simplicity,generality, and usability over the Internet. It is a textual data format with strong sup-port via Unicode for the languages of the world. Although the design of XML focuseson documents, it is widely used for the representation of arbitrary data structures, forDept. of CS & E, GEC, Hassan 28
  35. 35. Digital Reader Chapter 4 Figure 4.2: HTML Viewerexample in web services.Many application programming interfaces (APIs) have been de-veloped for software developers to use to process XML data, and several schema systemsexist to aid in the definition of XML-based languages.As of 2009, hundreds of XML-basedlanguages have been developed,including RSS, Atom, SOAP, and XHTML. XML-basedformats have become the default for many office-productivity tools, including MicrosoftOffice (Office Open XML), and LibreOffice (OpenDocument), and Apple’siWork. XML has also been employed as the base language for communication protocols,such as XMPP.This shows how to display an XML document using a JTree and Javasbuilt-in XML support. Because XML documents are heirarchical in nature, JTree is anatural fit for the task.Class XmlViewerTwo packages that are required to implement this viewer: • javax.xml.parsers: This package consists of classes used to process XML documents. • org.w3c.dom: This package consists of a set of interfaces that define the DOM (Document Object Model) which is an API that allows dynamic access to the structure and data of XML documents. Examples include Document, Element, Node, Attr, etc.Dept. of CS & E, GEC, Hassan 29
  36. 36. Digital Reader Chapter 4XmlViewer extends JFrame and represents the main application frame for this example.Five instance variables are defined: • Document m doc: the current XML document (note that this is an instance of org.w3c.dcom.Document; not a text document [javax.swing.text.Document]). • JTree m tree: the tree component used to display the current XML file. • DefaultTreeModel m model: tree model constructed to mimic the XML file. • JFileChooser m chooser: File chooser used for opening and saving XML files. • File m currentFile: reference to the current XML file. The XmlViewer constructor creates and installs a toolbar with our createToolbar()method, instantiates tree model m model with a top node containing No XML loaded asuser data, and instantiates tree m tree with m model.Selection is set to SINGLE TREE SELECTION and the tree is set to noneditable. Acustom tree cell renderer is created to display an appropriate icon based on whether anode represents an XML document element or not. This renderer is assign to our treeand the tree is then placed in a scroll pane which is added to the center of the frame.File chooser m chooser is instantiated and an xml file filter is applied to it so only XMLfiles will be displayed.The createToolbar() method creates a JToolBar with an Open button that invokes ouropenDocument() method.The getDocumentName() method retrieves the name of the current file referenced withour m currentFile variable.The openDocument() method shows our JFileChooser in a separate thread to allow se-lection of an XML file to open for viewing.javax.xml.parsers.DocumentBuilderFactorys static newInstance() is used to create a newinstance of DocumentBuilderFactory which is used to create an instance of Document-Builder.DocumentBuilder is used to parse the selected XML file with its parse() method,storing the resulting Document instance in our m doc variable. The root element of thisdocument is retrieved with Documents getDocumentElement() method. This element isused as the root node for our tree model, and our custom createTreeNode() creates thetree node heirarchy corresponding to the node passed to it. The resulting tree node isthen set as the root node of our tree and our custom expandTree() method is used toexpand all nodes to display the entire XML document.Dept. of CS & E, GEC, Hassan 30
  37. 37. Digital Reader Chapter 4The createTreeNode() method takes a root node (instance of org.w3c.dom.Node) as pa-rameter [note that org.w3c.dom.Element is a subinterface of Node]. Our canDisplayN-ode() method is used to find out whether the root node is either an element or a textnode. If so an instance of our custom XmlViewerNode is created to represent that node.Then a NodeList is created representing all child nodes of the root node. For each childnode a tree node is created by passing it recursively to createTreeNode().The canDisplayNode() method checks whether a given Node is of type ELEMENT NODEor TEXT NODE. If it isnt one of these types it should not be displayed in our tree.The showError() method is used to display exceptions in a JOptionPane dialog. Thestatic expandTree() methods are responsible for expanding each parent node so that theentire tree is expanded and visible in the viewer.Class XmlViewerNodeThis class extends DefaultMutableTreeNode to represent the XML nodes in our viewer.The main customization is in the overriden toString() implementation which returns atextual representation of the node depending on whether it is of type ELEMENT NODEor TEXT NODE. The figure 4.3 shows the snapshot of XML Viewer.4.4 Developing a Text ViewerClass BasicTextEditorThis class extends JFrame and provides the parent frame for our example. Two classvariables are declared: • String APP NAME: name of this example used in title bar. • String FONTS[]: an array of font family names.Instance variables: • Font[]m fonts: an array of Font instances which can be used to render our JTextArea editor. • JTextArea m editor: used as our text editor. • JMenuItem[] m fontMenus: an array of menu items representing available fonts. • JCheckBoxMenuItem m bold: menu item which sets/unsets the bold property of the current font.Dept. of CS & E, GEC, Hassan 31
  38. 38. Digital Reader Chapter 4 Figure 4.3: Xml Viewer • JCheckBoxMenuItem m italic: menu item which sets/unsets the italic property of the current font. • JFileChooser m chooser: used to load and save simple text files. • File m currentFile: the current File instance corresponding to the current docu- ment. • boolean m textChanged: will be set to true if the current document has been changed; will be set to false if the document was just opened or saved. This flag is used in combination with a DocumentListener to determine whether or not to save the current document before dismissing it.The BasicTextEditor constructor populates our m fonts array with Font instances corre-sponding to the names provided in FONTS[]. The m editor JTextArea is then createdand placed in a JScrollPane. This scroll pane is added to the center of our frames con-tent pane and we append some simple text to m editor for display at startup. OurDept. of CS & E, GEC, Hassan 32
  39. 39. Digital Reader Chapter 4createMenuBar() method is called to create the menu bar to manage this application,and this menu bar is then added to our frame using the setJMenuBar() method. ThecreateMenuBar() method creates and returns a JMenuBar. Each menu item receives anActionListener to handle it’s selection. Two menus are added titled File and Font. TheFile menu is assigned a mnemonic character, f, and by pressing ALT+F while the ap-plication frame is active, its popup will be displayed allowing navigation with either themouse or keyboard. The Font menu is assigned the mnemonic character o.The New menu item in the File menu is responsible for creating a new (empty) document.It doesnt really replace JTextAreas Document. Instead it simply clears the contents ofour editor component. Before it does so, however, it calls our custom promptToSave()method to determine whether we want to continue without saving the current changes (ifany) or not. Note that an icon is used for this menu item. Also note that this menu itemcan be selected with the keyboard by pressing n when the File menus popup is visible,because we assigned it n as a mnemonic. We also assigned it the accelerator CTRL+N.Therefore, this menus action will be directly invoked whenever that key combinationis pressed. (All other menus and menu items in this example also receive appropriatemnemonics and accelerators.)The Open menu item brings up our m chooser JFileChooser component to allow se-lection of a text file to open. Once a text file is selected, we open a FileReader on itand invoke read() on our JTextArea component to read the file’s content (which creates anew PlainDocument containing the selected files content to replace the current JTextAreadocument ). The Save menu item brings up m chooser to select a destination and filename to save the current text to (if previously not set). Once a text file is selected, weopen a FileWriter on it and invoke write() on our JTextArea component to write its con-tent to the destination file. The Save As menu is similar to the Save menu, but promptsthe user to select a new file. The Exit menu item terminates program execution. This isseparated from the first three menu items with a menu separator to create a more logicaldisplay. The Font menu consists of several menu items used to select the font and font styleused in our editor. All of these items receive the same ActionListener which invokes ourupdateEditor() method. To give the user an idea of how each font looks, each font isused to render the corresponding menu item text. Since only one font can be selectedat any given time, we use JRadioButtonMenuItems for these menu items, and add themall to a ButtonGroup instance which manages a single selection. To create each menuitem we iterate through our FONTS array and create a JRadioButtonMenuItem corre-sponding to each entry. Each item is set to unselected (except for the first one), assignedDept. of CS & E, GEC, Hassan 33
  40. 40. Digital Reader Chapter 4a numerical mnemonic corresponding to the current FONTS array index, assigned theappropriate Font instance for rendering its text, assigned our multi-purpose ActionLis-tener, and added to our ButtonGroup along with the others.The two other menu items in the Font menu manage the bold and italic font properties.They are implemented as JCheckBoxMenuItems since these properties can be selected orunselected independently. These items also are assigned the same ActionListener as theradio button items to process changes in their selected state.The updateEditor() method updates the current font used to render the editing com-ponent by checking the state of each check box item and determining which radio buttonitem is currently selected. The m bold and m italic components are disabled and uns-elected if the Courier font is selected, and enabled otherwise. The appropriate m fontsarray element is selected and a Font instance is derived from it corresponding to thecurrent state of the check box items using Fonts deriveFont() method.The figure 4.4 shows the snapshot of Text Viewer.4.5 Developing an RTF ViewerUsing Styles to manage a set of attributes as a single named entity can greatly simplifytext editing. The user only has to apply a known style to a selected region of text ratherthan selecting all appropriate text attributes from the provided toolbar components. Byadding a combo box allowing the choice of styles, we can not only save the user time andeffort, but we can also provide more uniform text formatting throughout the resultingdocument (or potentially set of documents). In this section we’ll add style managementto our word processor. Well also show how it is possible to create a new style, modify anexisting style, or reapply a style to modified text.Class WordProcessorOne new instance variable has been added: JComboBox m cbStyles: toolbar componentto manage styles.Note that a new custom method showStyles() (see below) is now called after creating anew document or after loading an existing one. The createMenuBar() method creates a new menu with two new menu items for up-dating and reapplying styles, and a new combo box for style selection. The editablestyles combobox, m cbStyles, will hold a list of styles declared in the current document.Dept. of CS & E, GEC, Hassan 34
  41. 41. Digital Reader Chapter 4 Figure 4.4: Text Viewer and editorIt receives an ActionListener which checks whether the currently selected style name ispresent among the existing styles. If not, we add it to the drop-down list and retrievea new Style instance for the selected name using StyledDocuments addStyle() method.This new Style instance is associated with the text attributes of the character elementat the current caret position. Otherwise, if the given style name is known already, weretrieve the selected style using StyledDocuments getStyle() method and apply it to theselected text by passing it to our custom setAttributeSet() method.An ambiguous situation occurs when the user selects a style for text which already has thesame style, but whose attributes have been modified. The user may either want to updatethe selected style using the selected text as a base, or reapply the existing style to theselected text. To resolve this situation we need to ask the user what to do. We chose toadd two menu items which allow the user to either update or reapply the current selection.Dept. of CS & E, GEC, Hassan 35
  42. 42. Digital Reader Chapter 4 The menu items to perform these tasks are titled Update and Reapply, and aregrouped into the Style menu. The Style menu is added to the Format menu. TheUpdate menu item receives an ActionListener which retrieves the text attributes of thecharacter element at the current caret position, and assigns them to the selected style.The Reapply menu item receives an ActionListener which applies the selected style to theselected text (one might argue that this menu item would be more appropriately titledApply – the implications are ambiguous either way). Our showAttributes() method receives additional code to manage the new styles com-bobox, m cbStyles, when the caret moves through the document. It retrieves the style cor-responding to the current caret position with StyledDocuments getLogicalStyle() method,and selects the appropriate entry in the combobox. The new showStyles() method is called to populate the m cbStyles combobox withthe style names from a newly created or loaded document. First it removes the currentcontent of the combobox if it is not empty (another work around due to the fact thatif you call removeAllItems() on an empty JComboBox, an exception will be thrown).An Enumeration of style names is then retrieved with StyledDocuments getStyleNames()method, and these names are added to the combobox. Open an existing RTF file, and note how the styles combobox is populated by thestyle names defined in this document. Verify that the selected style is automaticallyupdated while the caret moves through the document. Select a portion of text and selecta different style from the styles combobox. Note how all text properties are updatedaccording to the new style. Try selecting a portion of text and modifying its attributes (for instance, foregroundcolor). Type a new name in the styles combobox and press Enter. This will create a newstyle which can be applied to any other document text. The figure 4.5 shows the snapshot of RTF Viewer.4.6 PDF ViewerPortable Document Format (PDF) is a file format used to represent documents in a man-ner independent of application software, hardware, and operating systems. Each PDFfile encapsulates a complete description of a fixed-layout flat document, including theDept. of CS & E, GEC, Hassan 36
  43. 43. Digital Reader Chapter 4 Figure 4.5: RTF Viewertext, fonts, graphics, and other information needed to display it. In 1991, Adobe Systemsco-founder John Warnock outlined a system called ”Camelot” that evolved into PDF.A Java PDF Viewer which is powerful and fully-featured, but simple to setup, inte-grate and customise. JPedal offers everything you need from a multi-platform Java PDFviewer, including PDF Search and extraction. It also offers full PDF to Print support.Viewing PDF is not an add-on, nor an afterthought with the JPedal Java PDF library.The PDF Viewer is the central feature, and we have spent over 10 years developing ourfully-featured PDF viewer in Java.Class PDFViewerconstruct a pdf viewer, passing in the full file name using JPanelDemo(String name).FontMappings.setFontReplacements() Ensure non-embedded font map to sensible replace-ments and store file name for use in page changer after that it opens the PDF and readsits internal details. check if encryption present and acertain password, return true ifDept. of CS & E, GEC, Hassan 37
  44. 44. Digital Reader Chapter 4content accessable using boolean checkEncryption() method.check if file is encrypted if file has a null password it will have been decoded and is-FileViewable will return true and popup window if password needed after that try andreopen with new password and create a standalone program. User may pass in name offile as option. The figure 4.6 shows the snapshot of PDF Viewer. Figure 4.6: PDF Viewer4.7 Text SearchThe Text Search utility is constructed by making use of java regular expression librarycalled Swinggrep.Dept. of CS & E, GEC, Hassan 38
  45. 45. Digital Reader Chapter 4Functinality1.Showing each file path only once, giving more spaces for the lines found.2.Searching all subdirectories recursively (this feature can be disabled with a checkbox.3.Highlighting every match in a line, not just the first one.4.Better colors.5.Statistics about matches.6.Use of swing rather than awt for graphics.7.The ability to save results.Class GrepFrameFirst we will create a main class to provide a platform input actions through Jbut-tons,Jfilechooser and Jtextarea. A frame to search the contents of a set of files withpathnames based on the current directory (as per ’grep’) and display the results. Regex SearchPattern;The above statement indicates that The pattern we’re searching for.after that we willcreate a main window to perform search action.GrepFrame MainFrame;String FilePathnameString = new String();The above staement indicates that The pathname of the file currently being used.whichprovides the absolute path of the file.GrepDirectoryWalker GrepSubDirWalker = new GrepDirectoryWalker();The walker that performs the search on each directory.The document into which the re-sults of our search are placed. Note that this must be declared after GrepSubDirWalker,because it uses the latter’sJTextPane ResultsTextPane = new JTextPane(ResultsDocument);The results display the area itself.Container buildFirstLine() method Construct a con-tainer for the main components of the first line of the return A new container of all themain first line components. Container buildSecondLine() method Construct a containerfor the main components of the second line of the screen. return A new container of all themain second line components. setResultsFont() method Set the font used for displayingthe results. The size of the font is determined from that of the current metal user textfont. Construct file name of the form ’grepyyyymmddpattern.txt’. return Default filename into which to save the current results using createDefaultSaveFileName() method.Dept. of CS & E, GEC, Hassan 39
  46. 46. Digital Reader Chapter 4Pop up a dialog to display the given exception. param InputException. The excep-tion whose message is to be displayed using showExceptionDialog(Exception InputEx-ception) method.Get the results from the last search in the form of a string. return Astring representation of the results using String getResultsString() method.create a classGrepFrameListener to receive frame events, to allow us the opportunity to set the focusto the right field.The figure 4.7 shows the snapshot of Text Search. Figure 4.7: Text search4.8 File SearchThe file search utility searches only related files which the user has requested it also provesthe absolute path of the file.Dept. of CS & E, GEC, Hassan 40
  47. 47. Digital Reader Chapter 4Class FileSearchA search engine is an information retrieval system designed to help find informationstored on a computer system. The search results are usually presented in a list and arecommonly called hits. Search engines help to minimize the time required to find informa-tion and the amount of information which must be consulted, akin to other techniquesfor managing information overload.First we will create a main class to provide a platform input actions through Jbut-tons,Jfilechooser and Jtextarea.we will set the main path for the file to be searched.The figure 4.8 shows the snapshot of File Search. Figure 4.8: File searchDept. of CS & E, GEC, Hassan 41
  48. 48. Digital Reader Chapter 44.9 Image ViewerClass ImageViewerAn image viewer or image browser is a computer program that can display stored graphi-cal image; it can often handle various graphics file formats. Such software usually rendersthe image according to properties of the display such as color depth, display resolution,and color profile.Image image;boolean scaled;private JTextArea log;These are the variables which are necessary to display images.filter = new ExtensionFileFilter(new String[] ”gif”, ”GIF”, ”jpg”, ”JPG”, ”jpeg”, ”JPEG”,”GIF and JPG image files”);chooser.addChoosableFileFilter(filter);chooser.removeChoosableFileFilter(chooser.getAcceptAllFileFilter());The figure 4.9 shows the snapshot of Image Viewer. Figure 4.9: Image ViewerDept. of CS & E, GEC, Hassan 42
  49. 49. Chapter 5Results and analysisAs the digital reader supports multiple document formats under a single platform that isworking in architecture independent nature. This is because of the language we have usedto develop this application is Java.inorder to analyse our application the has to undergovarious testing and analysis on the application.5.1 TestingSoftware testing is an investigation conducted to provide stakeholders with informationabout the quality of the product or service under test. Software testing can also providean objective, independent view of the software to allow the business to appreciate andunderstand the risks of software implementation. Test techniques include, but are notlimited to, the process of executing a program or application with the intent of findingsoftware bugs (errors or other defects).Software testing can be stated as the process of validating and verifying that a softwareprogram/application/product: 1.meets the requirements that guided its design and as expected.3.can be implemented with the same characteristics.4.satisfies the needs of stakeholders. Software testing, depending on the testing method employed, can be implementedat any time in the development process. However, most of the test effort traditionallyoccurs after the requirements have been defined and the coding process has been com-pleted. Although in the Agile approaches most of the test effort is, conversely, on-going.As such, the methodology of the test is governed by the software development method-ology adopted. 43
  50. 50. Digital Reader Chapter 5Different software development models will focus the test effort at different points inthe development process. Newer development models, such as Agile, often employ testdriven development and place an increased portion of the testing in the hands of thedeveloper, before it reaches a formal team of testers. In a more traditional model, mostof the test execution occurs after the requirements have been defined and the codingprocess has been completed.5.2 Types of Testing • Black box testing • White box testing • Unit testing • Integration testing • Functional testing • System testing • Regression testing • Acceptance testing • Load testing • Alpha testing • Beta testing5.3 Testing with JUnit using EclipseAs our application has been developed using Java so we use JUnit to perform testing us-ing Unit testing. the following steps are performed to make testing using JUnit in Eclipse.5.3.1 Create a Java classIn the ”src” folder, we will create the digitalreader.test package and the following class wewill create the class for PDFViewer to check if encryption present and acertain password,return true if content accessable.Dept. of CS & E, GEC, Hassan 44
  51. 51. Digital Reader Chapter 5package org.jpedal.examples.jpaneldemo; import java.awt.BorderLayout;import java.awt.Component;import java.awt.Container;public class PDFViewer extends JFrame {....privatebooleancheckEncryption();....}5.3.2 Create a JUnit testRight click on new class in the Package Explorer and select New then JUnit Test Case.Select ”New JUnit 4 test” and set the source folder to ”test”, so that test class getscreated in this folder.Press ”Next” and select the methods which have to be tested. If the JUnit library in notpart of the classpath, Eclipse will prompt you to do so.Create a test with the following code.package digitalreader.test;import org.junit.Test;import static org.junit.Assert.assertEquals;public class PDFViewerTest{@T estpublicvoidtestcheckEncryption(){P DF V iewertester = newP DF V iewer();assertEquals(”Result”, password, tester.checkEncryption());}}5.3.3 Run test via EclipseRight click on new test class and select Run-As then JUnit Test The result of the testswill be displayed in the JUnit View. The test should be failing (indicated via a red bar)because it did not displayed any prompts to enter the password.Dept. of CS & E, GEC, Hassan 45
  52. 52. Digital Reader Chapter 5 After that we had fixed that problem and re-run the test we got a green bar. If youhave several tests we can combine them into a test suite. Running a test suite will executeall tests in that suite.To create a test suite, select the test classes then right click on it then New then Otherthen JUnit then Test Suite. Select ”Next” and select the methods for to create a test.Similarly we have done tests with other codes.Dept. of CS & E, GEC, Hassan 46
  53. 53. Chapter 6Conclusions and FutureEnhancementsDigital Reader application enables users to view more document formats using a singleapplication of software. The application has two utilities File Search and Text SearchUsing these the users can able search there required files and they can also search fora particular word among large number of file contained in a directory The applicationdeveloped using Java adds an advantage of using it virtually in any platform. Finally we conclude that our application is unique as there are very few or none of theapplications available to view more than one document format using a single applicationsoftware. 47
  54. 54. References[1] Description- URL 48