1 33 34 package net.percederberg.grammatica.code; 35 36 import java.io.File ; 37 import java.io.IOException ; 38 import java.io.PrintWriter ; 39 import java.util.Collections ; 40 import java.util.LinkedList ; 41 42 49 public abstract class CodeElementContainer extends CodeElement { 50 51 55 private LinkedList contents = new LinkedList (); 56 57 62 protected void addElement(CodeElement elem) { 63 if (!contents.contains(elem)) { 64 contents.add(elem); 65 } 66 } 67 68 77 protected void printContents(PrintWriter out, 78 CodeStyle style, 79 int indent) { 80 81 CodeElement prev = null; 82 CodeElement next; 83 84 Collections.sort(contents); 85 for (int i = 0; i < contents.size(); i++) { 86 next = (CodeElement) contents.get(i); 87 printSeparator(out, style, prev, next); 88 next.print(out, style, indent); 89 prev = next; 90 } 91 } 92 93 103 protected void printSeparator(PrintWriter out, 104 CodeStyle style, 105 CodeElement prev, 106 CodeElement next) { 107 108 if (prev == null || next == null) { 109 } else if (prev.category() != next.category()) { 111 out.println(); 112 } 113 } 114 115 122 protected void createFile(File file) throws IOException { 123 File dir = file.getParentFile(); 124 125 if (!dir.exists()) { 126 dir.mkdirs(); 127 } 128 if (!file.exists()) { 129 try { 130 file.createNewFile(); 131 } catch (IOException e) { 132 throw new IOException ("couldn't create " + file + ": " + 133 e.getMessage()); 134 } 135 } 136 } 137 } 138 | Popular Tags |