PageRenderTime 34ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/testability-explorer/src/main/java/com/google/test/metric/report/SourceLoader.java

http://testability-explorer.googlecode.com/
Java | 69 lines | 45 code | 9 blank | 15 comment | 10 complexity | fa106a52fa0ba55e3c5e9bf94d980690 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Copyright 2007 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.google.test.metric.report;
  17. import java.io.BufferedReader;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.InputStreamReader;
  21. import java.util.ArrayList;
  22. import com.google.classpath.ClassPath;
  23. public class SourceLoader {
  24. private final ClassPath classPath;
  25. public SourceLoader(ClassPath classPath) {
  26. this.classPath = classPath;
  27. }
  28. public Source load(String name) {
  29. InputStream is = getStream(name);
  30. if (is == null) {
  31. return new Source(new ArrayList<Source.Line>());
  32. }
  33. BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  34. ArrayList<Source.Line> lines = new ArrayList<Source.Line>();
  35. String line;
  36. int lineNumber = 1;
  37. try {
  38. while ((line = reader.readLine()) != null) {
  39. lines.add(new Source.Line(lineNumber, line));
  40. lineNumber ++;
  41. }
  42. reader.close();
  43. return new Source(lines);
  44. } catch (IOException e) {
  45. throw new RuntimeException(e);
  46. }
  47. }
  48. private InputStream getStream(String name) {
  49. String resource = name.replace(".", "/");
  50. InputStream is = null;
  51. while (is == null && !resource.equals("")) {
  52. is = classPath.getResourceAsStream(resource + ".java");
  53. if (is == null) {
  54. int index = resource.lastIndexOf('/');
  55. resource = index == -1 ? "" : resource.substring(0, index);
  56. }
  57. }
  58. return is;
  59. }
  60. }