KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > jmi > mapping > FileStreamFactory


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 package org.netbeans.lib.jmi.mapping;
20
21 import java.io.File JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import org.netbeans.api.mdr.JMIStreamFactory;
28
29 /**
30  *
31  * @author Martin Matula
32  */

33 public class FileStreamFactory extends JMIStreamFactory {
34     private final File JavaDoc targetDir;
35     private final long modelLastModified;
36
37     /** Creates a new instance of FileStreamFactory. Using this constructor is
38      * equivalent to using <code>FileStreamFactory(targetDir, null)</code>.
39      * @see #FileStreamFactory(File, java.util.Date)
40      * @param targetDir The target directory
41      * @throws IllegalArgumentException If <code>targetDir == null</code>,
42      * if the target directory does not already exist, or if the
43      * target file is not a directory.
44      */

45     public FileStreamFactory(File JavaDoc targetDir) {
46         this(targetDir, null);
47     }
48     
49     /** Creates a new instance of FileStreamFactory. FileStreamFactory will
50      * compare the last modified date of any existing target files against
51      * the value of the <code>modelLastModified</code> paramter. If the target
52      * file's last modified date is later than <code>modelLastModified/code>
53      * then {@link #createStream(List, String, String)} will return
54      * <code>null</code> to prevent the {@link JMIMapper} from overwriting
55      * the file. If <code>modelLastModified == null</code>, then
56      * <code>createStream</code> will never return <code>null</code>.
57      *
58      * @param targetDir The target directory
59      * @param modelLastModified The date that the MOF model was last changed,
60      * or <code>null</code>.
61      * @throws IllegalArgumentException If <code>targetDir == null</code>,
62      * if the target directory does not already exist, or if the
63      * target file is not a directory.
64      */

65     public FileStreamFactory(File JavaDoc targetDir, java.util.Date JavaDoc modelLastModified) {
66         if (targetDir == null)
67             throw new IllegalArgumentException JavaDoc("ERROR: targetDir is null");
68         else if (!targetDir.exists())
69             throw new IllegalArgumentException JavaDoc("ERROR: targetDir does not exist");
70         else if (!targetDir.isDirectory()) {
71             throw new IllegalArgumentException JavaDoc("ERROR: targetDir has to be a directory");
72         }
73         this.targetDir = targetDir;
74         this.modelLastModified = modelLastModified != null ? modelLastModified.getTime() : 0L;
75     }
76     
77     public OutputStream JavaDoc createStream(List JavaDoc pkg, String JavaDoc className, String JavaDoc extension) throws IOException JavaDoc {
78         File JavaDoc current = targetDir;
79         for (Iterator JavaDoc it = pkg.iterator(); it.hasNext();) {
80             current = new File JavaDoc(current, (String JavaDoc) it.next());
81             if (!current.exists()) {
82                 current.mkdir();
83             }
84         }
85         return createStream(new File JavaDoc(current, className + "." + extension));
86     }
87
88     /** This method is called by {@link #createStream(List,String,String)} to
89      * really create the stream. It return creates the FileOutputSream
90      * corresponding to the given file if upToDate is false; otherwise it
91      * returns null. This is a convenient place for subclasses to hook
92      * into the stream creation process for logging or checking different
93      * criteria besides just the file's last modified date.
94      */

95     protected OutputStream JavaDoc createStream(java.io.File JavaDoc file) throws IOException JavaDoc {
96         return modelLastModified == 0 || modelLastModified > file.lastModified()
97             ? new FileOutputStream JavaDoc(file) : null;
98     }
99     
100 }
101
Popular Tags