1 17 package org.eclipse.emf.importer.rose.parser; 18 19 import java.io.BufferedReader ; 20 import java.io.File ; 21 import java.io.FileReader ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.InputStreamReader ; 25 import java.util.regex.Matcher ; 26 import java.util.regex.Pattern ; 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 37 public class RoseLoader extends RoseComponent 38 { 39 public static final String PROGRESS = "ROSE_LOADER_PROGRESS"; 40 41 protected boolean valid; 42 protected BufferedReader 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 fileName, URIConverter uriConverter) throws Exception 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 (new FileReader (fileName)); 62 valid = true; 63 length = (new File (fileName)).length(); 64 currentLength = 0; 65 oldValue = lower; 66 } 67 catch (Exception exception) 68 { 69 Pattern pattern = Pattern.compile(".*/([^/]*?)(?:_)?[0-9.]*/((src/)?model/[^/]*\\.cat)$"); 70 Matcher matcher = pattern.matcher(fileName.replaceAll("\\\\", "/")); 71 if (matcher.matches()) 72 { 73 String name = matcher.group(1); 74 String tail = matcher.group(2); 75 InputStream inputStream = null; 76 try 77 { 78 inputStream = uriConverter.createInputStream(URI.createPlatformResourceURI(name + "/" + tail)); 79 } 80 catch (Exception resourceException) 81 { 82 try 83 { 84 inputStream = uriConverter.createInputStream(URI.createURI("platform:/plugin/" + name + "/" + tail)); 85 } 86 catch (Exception pluginException) 87 { 88 } 89 } 90 if (inputStream != null) 91 { 92 bufferedReader = new BufferedReader (new InputStreamReader (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 readLine() 142 { 143 try 144 { 145 String 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 e) 159 { 160 return null; 161 } 162 } 163 164 public void close() throws IOException 165 { 166 if (bufferedReader != null) 167 { 168 bufferedReader.close(); 169 } 170 } 171 } 172 | Popular Tags |