Source Code Cross Referenced for ValueTests.java in  » Apache-Harmony-Java-SE » javax-package » javax » print » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » javax package » javax.print 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:        /** 
018:         * @author Irina A. Arkhipets 
019:         * @version $Revision: 1.3 $ 
020:         */package javax.print;
021:
022:        import java.io.ByteArrayOutputStream;
023:        import java.io.FileReader;
024:        import java.io.InputStream;
025:        import java.util.Locale;
026:        import java.util.Vector;
027:
028:        import javax.print.attribute.Attribute;
029:        import javax.print.attribute.DocAttribute;
030:        import javax.print.attribute.DocAttributeSet;
031:        import javax.print.attribute.HashDocAttributeSet;
032:        import javax.print.attribute.HashPrintJobAttributeSet;
033:        import javax.print.attribute.HashPrintRequestAttributeSet;
034:        import javax.print.attribute.HashPrintServiceAttributeSet;
035:        import javax.print.attribute.PrintJobAttribute;
036:        import javax.print.attribute.PrintJobAttributeSet;
037:        import javax.print.attribute.PrintRequestAttribute;
038:        import javax.print.attribute.PrintRequestAttributeSet;
039:        import javax.print.attribute.PrintServiceAttribute;
040:        import javax.print.attribute.PrintServiceAttributeSet;
041:        import javax.print.attribute.standard.ColorSupported;
042:        import javax.print.attribute.standard.Copies;
043:        import javax.print.attribute.standard.MediaName;
044:        import javax.print.attribute.standard.OrientationRequested;
045:        import javax.print.attribute.standard.PrintQuality;
046:        import javax.print.attribute.standard.PrinterLocation;
047:
048:        import junit.framework.TestCase;
049:
050:        public class ValueTests extends TestCase {
051:
052:            private static String OS = null;
053:
054:            static {
055:                try {
056:                    OS = System.getProperty("os.name");
057:                    if (OS.startsWith("Windows")) {
058:                        System.loadLibrary("ValueTestsLibrary");
059:                    }
060:                } catch (Exception e) {
061:                    if (OS == null) {
062:                        System.out
063:                                .println("WARNING: Can not get Operation System name!");
064:                    } else {
065:                        if (OS.startsWith("Windows")) {
066:                            System.out
067:                                    .println("WARNING! Can not load ValueTestsLibrary library!");
068:                        }
069:                    }
070:                    System.out
071:                            .println("Some testcases probably will not be started!");
072:                }
073:            }
074:
075:            public void testDocFlavor() {
076:                startTest("DocFlavor class testing...");
077:                DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
078:                assertEquals(flavor, new DocFlavor.INPUT_STREAM("image/gif"));
079:                assertEquals(flavor.getMediaSubtype(), "gif");
080:                assertEquals(flavor.getMediaType(), "image");
081:                assertEquals(flavor.getMimeType(), "image/gif");
082:                assertEquals(flavor.getRepresentationClassName(),
083:                        "java.io.InputStream");
084:                assertEquals(flavor.toString(),
085:                        "image/gif; class=\"java.io.InputStream\"");
086:            }
087:
088:            public void testSimpleDoc() {
089:                startTest("SimpleDoc class testing...");
090:
091:                DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
092:                InputStream reader = getClass().getResourceAsStream(
093:                        "/Resources/picture.gif");
094:                if (reader == null) {
095:                    fail("/Resources/picture.gif resource is not found!");
096:                }
097:
098:                DocAttributeSet aSet = new HashDocAttributeSet();
099:                Doc doc = new SimpleDoc(reader, flavor, aSet);
100:
101:                assertEquals(doc.getAttributes(), aSet);
102:                aSet.add(OrientationRequested.LANDSCAPE);
103:                aSet.add(MediaName.NA_LETTER_WHITE);
104:                assertEquals(doc.getAttributes(), aSet);
105:                assertEquals(doc.getDocFlavor(), DocFlavor.INPUT_STREAM.GIF);
106:                try {
107:                    assertTrue(doc.getPrintData() instanceof  java.io.InputStream);
108:                    assertNull(doc.getReaderForText());
109:                    assertEquals(doc.getStreamForBytes(), reader);
110:                } catch (Exception e) {
111:                    e.printStackTrace();
112:                    fail("Exception found: " + e);
113:                }
114:            }
115:
116:            public void testDefaultPrintService() {
117:                boolean flg = false;
118:
119:                startTest("PrintService class testing...");
120:                DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
121:                PrintRequestAttributeSet printRequestSet = new HashPrintRequestAttributeSet();
122:                PrintService service = getPrintService(flavor, printRequestSet);
123:                DocFlavor[] flavors;
124:                String myName = getDefaultPrintService();
125:
126:                System.out.println("OS is " + OS);
127:                if (service == null) {
128:                    System.out
129:                            .println("WARNING: Can not get print service which supports INPUT_STREAM.GIF!");
130:                } else {
131:                    assertTrue(service.isDocFlavorSupported(flavor));
132:                    flavors = service.getSupportedDocFlavors();
133:                    for (int i = 0; i < flavors.length; i++) {
134:                        if (flavors[i].equals(flavor)) {
135:                            flg = true;
136:                            break;
137:                        }
138:                    }
139:                    assertTrue(flg);
140:                    /*        if (myName != null) {
141:                     assertEquals(service.getName(), myName);
142:                     }
143:                     */
144:                }
145:            }
146:
147:            public void testPrintJob() {
148:                startTest("PrintJob class testing...");
149:                DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
150:                PrintRequestAttributeSet printRequestSet = new HashPrintRequestAttributeSet();
151:                PrintService service = getPrintService(flavor, printRequestSet);
152:                if (service != null) {
153:                    DocPrintJob job = service.createPrintJob();
154:                    assertEquals(job.getPrintService(), service);
155:                } else {
156:                    System.out
157:                            .println("WARNING: Can not get print service which supports INPUT_STREAM.GIF!");
158:                }
159:            }
160:
161:            public void testStreamServiceFactory() {
162:                startTest("StreamPrintServiceFactory class testing...");
163:                boolean flg = false;
164:                DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
165:                StreamPrintServiceFactory[] aFactory = StreamPrintServiceFactory
166:                        .lookupStreamPrintServiceFactories(flavor,
167:                                "application/postscript");
168:                StreamPrintServiceFactory streamFactory = null;
169:                StreamPrintService streamService;
170:                DocFlavor[] flavors;
171:
172:                if ((aFactory == null) || (aFactory.length == 0)) {
173:                    fail("Can not find stream print service factory!");
174:                } else {
175:                    streamFactory = aFactory[0];
176:                }
177:                streamService = streamFactory
178:                        .getPrintService(new ByteArrayOutputStream());
179:                assertEquals(streamFactory.getOutputFormat(),
180:                        "application/postscript");
181:                assertEquals(streamFactory
182:                        .getPrintService(new ByteArrayOutputStream()),
183:                        streamService);
184:                flavors = streamFactory.getSupportedDocFlavors();
185:                for (int i = 0; i < flavors.length; i++) {
186:                    if (flavors[i].equals(flavor)) {
187:                        flg = true;
188:                        break;
189:                    }
190:                }
191:                assertTrue(flg);
192:            }
193:
194:            public void testStreamPrintService() {
195:                startTest("StreamPrintService class testing...");
196:
197:                DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
198:                StreamPrintServiceFactory[] aFactory = StreamPrintServiceFactory
199:                        .lookupStreamPrintServiceFactories(flavor,
200:                                "application/postscript");
201:                StreamPrintServiceFactory streamFactory = null;
202:                StreamPrintService streamService;
203:                DocFlavor[] flavors;
204:                boolean flg = false;
205:
206:                if ((aFactory == null) || (aFactory.length == 0)) {
207:                    fail("Can not find stream print service factory!");
208:                } else {
209:                    streamFactory = aFactory[0];
210:                }
211:                streamService = streamFactory
212:                        .getPrintService(new ByteArrayOutputStream());
213:
214:                assertEquals(streamService.getOutputFormat(),
215:                        "application/postscript");
216:                assertTrue(streamService.isDocFlavorSupported(flavor));
217:                flavors = streamService.getSupportedDocFlavors();
218:                for (int i = 0; i < flavors.length; i++) {
219:                    if (flavors[i].equals(flavor)) {
220:                        flg = true;
221:                        break;
222:                    }
223:                }
224:                assertTrue(flg);
225:                streamService.dispose();
226:                assertTrue(streamService.isDisposed());
227:            }
228:
229:            public void testStreamServicePrinting() throws Exception {
230:                startTest("StreamPrintServiceFactory class testing...");
231:
232:                byte[] forChecking = { '%', '!', 'P', 'S', '-', 'A', 'd', 'o',
233:                        'b', 'e' };
234:                DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
235:                StreamPrintServiceFactory[] aFactory = StreamPrintServiceFactory
236:                        .lookupStreamPrintServiceFactories(flavor,
237:                                "application/postscript");
238:                StreamPrintServiceFactory streamFactory = null;
239:                StreamPrintService streamService;
240:                DocPrintJob aJob;
241:                InputStream aStream = getClass().getResourceAsStream(
242:                        "/Resources/picture.gif");
243:                Doc doc;
244:                byte[] arr;
245:                boolean flg = true;
246:
247:                if ((aFactory == null) || (aFactory.length == 0)) {
248:                    fail("Can not find stream print service factory!");
249:                } else {
250:                    streamFactory = aFactory[0];
251:                }
252:                if (aStream == null) {
253:                    fail("/Resources/picture.gif resource is not found!");
254:                }
255:
256:                streamService = streamFactory
257:                        .getPrintService(new ByteArrayOutputStream());
258:                aJob = streamService.createPrintJob();
259:                doc = new SimpleDoc(aStream, flavor, null);
260:
261:                aJob.print(doc, null);
262:
263:                arr = ((ByteArrayOutputStream) (streamService.getOutputStream()))
264:                        .toByteArray();
265:                for (int i = 0; i < 10; i++) {
266:                    if (arr[i] != forChecking[i]) {
267:                        flg = false;
268:                        break;
269:                    }
270:                }
271:                assertTrue(flg);
272:            }
273:
274:            public void testHashDocAttributeSet() {
275:                startTest("HashDocAttributeSet class testing...");
276:
277:                DocAttributeSet set1 = new HashDocAttributeSet();
278:                DocAttributeSet set2 = new HashDocAttributeSet(
279:                        OrientationRequested.LANDSCAPE);
280:                DocAttributeSet set3 = new HashDocAttributeSet(set2);
281:                DocAttribute[] arr = { OrientationRequested.LANDSCAPE,
282:                        MediaName.NA_LETTER_WHITE };
283:                DocAttributeSet set4 = new HashDocAttributeSet(arr);
284:                Attribute[] attrArr;
285:
286:                assertTrue(set1.isEmpty());
287:                assertFalse(set2.isEmpty());
288:                assertTrue(set3.equals(set2));
289:                assertFalse(set3.equals(set1));
290:                set3.clear();
291:                assertEquals(set3, set1);
292:                set3.add(OrientationRequested.LANDSCAPE);
293:                set3.add(MediaName.NA_LETTER_WHITE);
294:                assertTrue(set3.containsKey(OrientationRequested.LANDSCAPE
295:                        .getClass()));
296:                assertFalse(set2.containsKey(MediaName.NA_LETTER_WHITE
297:                        .getClass()));
298:                assertTrue(set3.containsValue(OrientationRequested.LANDSCAPE));
299:                assertFalse(set3.containsValue(OrientationRequested.PORTRAIT));
300:                assertFalse(set3.containsValue(PrintQuality.DRAFT));
301:                assertEquals(set1.size(), 0);
302:                assertEquals(set2.size(), 1);
303:                assertEquals(set3.size(), 2);
304:                assertTrue(set4.equals(set3));
305:                assertEquals(
306:                        set3.get(OrientationRequested.PORTRAIT.getClass()),
307:                        OrientationRequested.LANDSCAPE);
308:                assertFalse((set3.get(OrientationRequested.PORTRAIT.getClass()))
309:                        .equals(OrientationRequested.PORTRAIT));
310:                set1.addAll(set3);
311:                assertEquals(set3, set1);
312:                set1.remove(OrientationRequested.PORTRAIT.getClass());
313:                assertEquals(set1.size(), 1);
314:                attrArr = set1.toArray();
315:                assertEquals(attrArr.length, 1);
316:                assertEquals(attrArr[0], MediaName.NA_LETTER_WHITE);
317:            }
318:
319:            public void testHashPrintJobAttributeSet() {
320:                startTest("HashPrintJobAttributeSet class testing...");
321:
322:                PrintJobAttributeSet set1 = new HashPrintJobAttributeSet();
323:                PrintJobAttributeSet set2 = new HashPrintJobAttributeSet(
324:                        OrientationRequested.LANDSCAPE);
325:                PrintJobAttributeSet set3 = new HashPrintJobAttributeSet(set2);
326:                PrintJobAttribute[] arr = { OrientationRequested.LANDSCAPE,
327:                        MediaName.NA_LETTER_WHITE };
328:                PrintJobAttributeSet set4 = new HashPrintJobAttributeSet(arr);
329:                Attribute[] attrArr;
330:
331:                assertTrue(set1.isEmpty());
332:                assertFalse(set2.isEmpty());
333:                assertTrue(set3.equals(set2));
334:                assertFalse(set3.equals(set1));
335:                set3.clear();
336:                assertEquals(set3, set1);
337:                set3.add(OrientationRequested.LANDSCAPE);
338:                set3.add(MediaName.NA_LETTER_WHITE);
339:                assertTrue(set3.containsKey(OrientationRequested.LANDSCAPE
340:                        .getClass()));
341:                assertFalse(set2.containsKey(MediaName.NA_LETTER_WHITE
342:                        .getClass()));
343:                assertTrue(set3.containsValue(OrientationRequested.LANDSCAPE));
344:                assertFalse(set3.containsValue(OrientationRequested.PORTRAIT));
345:                assertFalse(set3.containsValue(PrintQuality.DRAFT));
346:                assertEquals(set1.size(), 0);
347:                assertEquals(set2.size(), 1);
348:                assertEquals(set3.size(), 2);
349:                assertTrue(set4.equals(set3));
350:                assertEquals(
351:                        set3.get(OrientationRequested.PORTRAIT.getClass()),
352:                        OrientationRequested.LANDSCAPE);
353:                assertFalse((set3.get(OrientationRequested.PORTRAIT.getClass()))
354:                        .equals(OrientationRequested.PORTRAIT));
355:                set1.addAll(set3);
356:                assertEquals(set3, set1);
357:                set1.remove(OrientationRequested.PORTRAIT.getClass());
358:                assertEquals(set1.size(), 1);
359:                attrArr = set1.toArray();
360:                assertEquals(attrArr.length, 1);
361:                assertEquals(attrArr[0], MediaName.NA_LETTER_WHITE);
362:            }
363:
364:            public void testHashPrintRequestAttributeSet() {
365:                startTest("HashPrintRequestAttributeSet class testing...");
366:
367:                Copies copies = new Copies(2);
368:                PrintRequestAttributeSet set1 = new HashPrintRequestAttributeSet();
369:                PrintRequestAttributeSet set2 = new HashPrintRequestAttributeSet(
370:                        copies);
371:                PrintRequestAttributeSet set3 = new HashPrintRequestAttributeSet(
372:                        set2);
373:                PrintRequestAttribute[] arr = { copies,
374:                        MediaName.NA_LETTER_WHITE };
375:                PrintRequestAttributeSet set4 = new HashPrintRequestAttributeSet(
376:                        arr);
377:                Attribute[] attrArr = set1.toArray();
378:
379:                assertTrue(set1.isEmpty());
380:                assertFalse(set2.isEmpty());
381:                assertTrue(set3.equals(set2));
382:                assertFalse(set3.equals(set1));
383:                set3.clear();
384:                assertEquals(set3, set1);
385:                set3.add(copies);
386:                set3.add(MediaName.NA_LETTER_WHITE);
387:                assertTrue(set3.containsKey(copies.getClass()));
388:                assertFalse(set2.containsKey(MediaName.NA_LETTER_WHITE
389:                        .getClass()));
390:                assertTrue(set3.containsValue(copies));
391:                assertFalse(set3.containsValue(OrientationRequested.PORTRAIT));
392:                assertFalse(set3.containsValue(PrintQuality.DRAFT));
393:                assertEquals(set1.size(), 0);
394:                assertEquals(set2.size(), 1);
395:                assertEquals(set3.size(), 2);
396:                assertTrue(set4.equals(set3));
397:                assertEquals(set3.get(copies.getClass()), copies);
398:                set1.addAll(set3);
399:                assertEquals(set3, set1);
400:                set1.remove(copies.getClass());
401:                assertEquals(set1.size(), 1);
402:                attrArr = set1.toArray();
403:                assertEquals(attrArr.length, 1);
404:                assertEquals(attrArr[0], MediaName.NA_LETTER_WHITE);
405:            }
406:
407:            public void testHashPrintServiceAttributeSet() {
408:                startTest("HashPrintJobAttributeSet class testing...");
409:
410:                PrintServiceAttributeSet set1 = new HashPrintServiceAttributeSet();
411:                PrintServiceAttributeSet set2 = new HashPrintServiceAttributeSet(
412:                        ColorSupported.SUPPORTED);
413:                PrintServiceAttributeSet set3 = new HashPrintServiceAttributeSet(
414:                        set2);
415:                PrinterLocation location = new PrinterLocation("room 222",
416:                        Locale.ENGLISH);
417:                PrintServiceAttribute[] arr = { location,
418:                        ColorSupported.SUPPORTED };
419:                PrintServiceAttributeSet set4 = new HashPrintServiceAttributeSet(
420:                        arr);
421:
422:                assertTrue(set1.isEmpty());
423:                assertFalse(set2.isEmpty());
424:                assertTrue(set3.equals(set2));
425:                assertFalse(set3.equals(set1));
426:                set3.clear();
427:                assertEquals(set3, set1);
428:                set3.add(ColorSupported.SUPPORTED);
429:                set3.add(location);
430:                assertTrue(set3.containsKey(location.getClass()));
431:                assertFalse(set2.containsKey(MediaName.NA_LETTER_WHITE
432:                        .getClass()));
433:                assertTrue(set4.equals(set3));
434:                assertEquals(set3.get(location.getClass()), location);
435:                set1.addAll(set3);
436:                assertEquals(set3, set1);
437:                set1.remove(location.getClass());
438:                assertEquals(set1.size(), 1);
439:            }
440:
441:            // ---------------------------------------------------------------------------------
442:            /* 
443:             * This function search a PrintService which supports given 
444:             * DocFlavor and PrintRequestAttributeSet.
445:             * If default printer supports them, this function returns default printer.
446:             * Overwise it returna first printer from all print services list.
447:             */
448:            private PrintService getPrintService(DocFlavor aFlavor,
449:                    PrintRequestAttributeSet aSet) {
450:                PrintService[] services = PrintServiceLookup
451:                        .lookupPrintServices(aFlavor, aSet);
452:                PrintService defaultService = PrintServiceLookup
453:                        .lookupDefaultPrintService();
454:
455:                if (services.length <= 0) {
456:                    System.out.println("Can not find default print service!");
457:                    return null;
458:                }
459:                for (int i = 0; i < services.length; i++) {
460:                    if (services[i].equals(defaultService)) {
461:                        return services[i];
462:                    }
463:                }
464:                System.out
465:                        .println("System Default PrintService does not support given attributes!");
466:                return services[0];
467:            }
468:
469:            /*
470:             *  This functions returns a list of the available printers
471:             */
472:            private String[] getPrintServices() {
473:                return (OS.startsWith("Windows") ? getWindowsPrintServices()
474:                        : getLinuxPrintServices());
475:            }
476:
477:            /*
478:             * This native function returns Windows printers list
479:             */
480:            private native String[] getWindowsPrintServices();
481:
482:            /*
483:             * This function returns Linus printers list.
484:             * On Linux all CUPS printers are listed on /etc/printcap file
485:             */
486:            private String[] getLinuxPrintServices() {
487:                Vector services = new Vector();
488:                String str = "";
489:                short state = 0;
490:                char c;
491:                String[] res;
492:
493:                try {
494:                    boolean flg = true;
495:                    FileReader reader = new FileReader("/etc/printcap");
496:                    while (flg) {
497:                        c = (char) reader.read();
498:                        if ((c <= 0) || (c == 65535)) {
499:                            if (state == 2) {
500:                                services.add(str);
501:                            }
502:                            flg = false;
503:                        }
504:                        switch (state) {
505:                        case 0:
506:                            if (c == '#') {
507:                                state = 1;
508:                            } else if (Character.isLetter(c)) {
509:                                str = str + c;
510:                                state = 2;
511:                            }
512:                            break;
513:                        case 1:
514:                            if (c == '\n') {
515:                                state = 0;
516:                                str = "";
517:                            }
518:                            break;
519:                        case 2:
520:                            if ((c == '|') || (c == '\n') || (c == ' ')
521:                                    || (c == 9)) {
522:                                services.add(str);
523:                                str = "";
524:                                state = (c == '\n') ? (short) 0 : (short) 1;
525:                            } else {
526:                                str = str + c;
527:                            }
528:                            break;
529:                        }
530:                    }
531:                } catch (Exception e) {
532:                    e.printStackTrace();
533:                    return null;
534:                }
535:
536:                res = new String[services.size()];
537:                for (int i = 0; i < services.size(); i++) {
538:                    res[i] = (String) services.get(i);
539:                }
540:
541:                return res;
542:            }
543:
544:            /*
545:             * This function returns system default printer
546:             */
547:            private String getDefaultPrintService() {
548:                return OS.startsWith("Windows") ? getDefaultWindowsPrintService()
549:                        : getDefaultLinuxPrintService();
550:            }
551:
552:            /*
553:             * This function returns Windows default printer name
554:             */
555:            private native String getDefaultWindowsPrintService();
556:
557:            /*
558:             * This function returns Linux default printer name
559:             * On Linux lpstat -d returns string like the following:
560:             * "system default destination: test"
561:             */
562:            private String getDefaultLinuxPrintService() {
563:                try {
564:                    Process process = Runtime.getRuntime().exec(
565:                            "lpstat -d | grep \"system default destination\"");
566:                    InputStream iStream;
567:                    byte[] res;
568:                    String resStr;
569:
570:                    process.waitFor();
571:                    if (process.exitValue() != 0) {
572:                        System.out.println("Can not exec \"lpstat -d\"");
573:                        return null;
574:                    }
575:                    iStream = process.getInputStream();
576:                    res = new byte[iStream.available()];
577:                    iStream.read(res);
578:                    resStr = new String(res);
579:                    if (!resStr.startsWith("system default destination: ")) {
580:                        System.out
581:                                .println("WARNING: Can not recognize \"lpstat -d\" output:");
582:                        System.out.println(resStr + "\n");
583:                        return null;
584:                    }
585:                    // Last symbol in resStr is "\n"!
586:                    return resStr.substring(28, resStr.length() - 1);
587:                } catch (Exception e) {
588:                    e.printStackTrace();
589:                    return null;
590:                }
591:            }
592:
593:            private void startTest(String s) {
594:                System.out.println("----------------------------------------");
595:                System.out.println(s);
596:            }
597:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.