KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdrant > MapJava


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.mdrant;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.FileReader JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.StringWriter JavaDoc;
28
29 import javax.jmi.reflect.*;
30
31 import org.apache.tools.ant.BuildException;
32
33 import org.netbeans.api.mdr.*;
34 import org.netbeans.lib.jmi.mapping.FileStreamFactory;
35 import org.netbeans.lib.jmi.mapping.JMIMapperImpl;
36
37 /** Creates JMI maping in form of source files.
38  *
39  * @author Petr Hrebejk
40  */

41 public class MapJava extends MdrTask.Sub {
42
43     private File JavaDoc dir;
44     private File JavaDoc headerFile;
45     private String JavaDoc extent;
46     
47     /** Creates a new instance of PrintExtentsNames */
48     public MapJava() {
49     }
50     
51     public void execute() throws Exception JavaDoc {
52         
53         if ( dir == null ) {
54             throw new BuildException( "Destination directory has to be sppecified. Use the \"dir\" attribute" );
55         }
56         
57         if ( extent == null ) {
58             throw new BuildException( "Extent to map has to be sppecified. Use the \"extent\" attribute" );
59         }
60         
61         JMIStreamFactory streamFactory = new FileStreamFactory( dir );
62         
63         getMapper().generate( streamFactory, getRepository().getExtent( extent ) );
64         
65     }
66     
67     // ANT task attributes -----------------------------------------------------
68

69     public void setDir( File JavaDoc dir ) {
70       this.dir = dir;
71     }
72     
73     public void setExtent( String JavaDoc extent ) {
74         this.extent = extent;
75     }
76     
77     public void setHeaderFile(File JavaDoc headerFile) {
78         this.headerFile = headerFile;
79     }
80     
81     // Protected methods -------------------------------------------------------
82

83     protected JMIMapper getMapper() throws IOException JavaDoc {
84         JMIMapperImpl result = new JMIMapperImpl();
85         if (headerFile != null) {
86             BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new FileReader JavaDoc(headerFile));
87             StringWriter JavaDoc header = new StringWriter JavaDoc();
88             int ch;
89             while ((ch = reader.read()) != -1) {
90                 header.write(ch);
91             }
92             reader.close();
93             header.close();
94             result.setHeader(header.toString());
95         }
96         return result;
97     }
98     
99     
100 }
101
Popular Tags