KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > importer > rose > parser > RoseLoader


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: RoseLoader.java,v 1.4 2005/06/08 06:20:36 nickb Exp $
16  */

17 package org.eclipse.emf.importer.rose.parser;
18
19 import java.io.BufferedReader JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.FileReader JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.util.regex.Matcher JavaDoc;
26 import java.util.regex.Pattern JavaDoc;
27
28 import org.eclipse.emf.common.util.URI;
29 import org.eclipse.emf.ecore.resource.URIConverter;
30 import org.eclipse.emf.importer.rose.RoseImporterPlugin;
31
32
33 /**
34  * A loader that creates Buffered Reader.
35  *
36  */

37 public class RoseLoader extends RoseComponent
38 {
39   public static final String JavaDoc PROGRESS = "ROSE_LOADER_PROGRESS";
40
41   protected boolean valid;
42   protected BufferedReader JavaDoc bufferedReader;
43   protected long length = 0;
44   protected long currentLength = 0;
45   protected int progressIncrement = 10;
46   protected int oldValue;
47   protected int lower = 0;
48   protected int upper = 100;
49
50   public RoseLoader(String JavaDoc fileName, URIConverter uriConverter) throws Exception JavaDoc
51   {
52     try
53     {
54       if (!fileName.startsWith("\\\\"))
55       {
56         fileName = Util.updateFileName(fileName, "\\\\");
57       }
58       fileName = Util.updateFileName(fileName, "\\");
59       fileName = Util.updateFileName(fileName, "/");
60
61       bufferedReader = new BufferedReader JavaDoc(new FileReader JavaDoc(fileName));
62       valid = true;
63       length = (new File JavaDoc(fileName)).length();
64       currentLength = 0;
65       oldValue = lower;
66     }
67     catch (Exception JavaDoc exception)
68     {
69       Pattern JavaDoc pattern = Pattern.compile(".*/([^/]*?)(?:_)?[0-9.]*/((src/)?model/[^/]*\\.cat)$");
70       Matcher JavaDoc matcher = pattern.matcher(fileName.replaceAll("\\\\", "/"));
71       if (matcher.matches())
72       {
73         String JavaDoc name = matcher.group(1);
74         String JavaDoc tail = matcher.group(2);
75         InputStream JavaDoc inputStream = null;
76         try
77         {
78           inputStream = uriConverter.createInputStream(URI.createPlatformResourceURI(name + "/" + tail));
79         }
80         catch (Exception JavaDoc resourceException)
81         {
82           try
83           {
84             inputStream = uriConverter.createInputStream(URI.createURI("platform:/plugin/" + name + "/" + tail));
85           }
86           catch (Exception JavaDoc pluginException)
87           {
88           }
89         }
90         if (inputStream != null)
91         {
92           bufferedReader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(inputStream));
93           valid = true;
94           length = inputStream.available();
95           currentLength = 0;
96           oldValue = lower;
97         }
98         else
99         {
100           RoseImporterPlugin.INSTANCE.log(exception);
101         }
102       }
103       else
104       {
105         RoseImporterPlugin.INSTANCE.log(exception);
106       }
107     }
108   }
109
110   public boolean isValid()
111   {
112     return valid;
113   }
114
115   public void setProgressIncrement(int progressIncrement)
116   {
117     this.progressIncrement = progressIncrement;
118   }
119
120   public void setLower(int lower)
121   {
122     this.lower = lower;
123     oldValue = lower;
124   }
125
126   public int getLower()
127   {
128     return lower;
129   }
130
131   public void setUpper(int upper)
132   {
133     this.upper = upper;
134   }
135
136   public int getUpper()
137   {
138     return upper;
139   }
140
141   public String JavaDoc readLine()
142   {
143     try
144     {
145       String JavaDoc line = bufferedReader.readLine();
146       currentLength += line.length();
147       if (length > 0)
148       {
149         int newValue = lower + (int)(currentLength * (upper - lower) / length);
150         if (newValue >= oldValue + progressIncrement && newValue < upper)
151         {
152           firePropertyChange(PROGRESS, oldValue, newValue);
153           oldValue = newValue;
154         }
155       }
156       return line;
157     }
158     catch (Exception JavaDoc e)
159     {
160       return null;
161     }
162   }
163
164   public void close() throws IOException JavaDoc
165   {
166     if (bufferedReader != null)
167     {
168       bufferedReader.close();
169     }
170   }
171 }
172
Popular Tags