|
| 1 | +/** |
| 2 | + * VisuObject: ein Java-Projekt aus mehreren Dateien, wie im Package Explorer von Eclipse. |
| 3 | + * Jede Datei liegt in einem Package (Ordner); die Zeile `package …;` im Code folgt dem Ordner. |
| 4 | + * Das Klassendiagramm zeigt die Klassen aller Dateien. |
| 5 | + */ |
| 6 | + |
| 7 | +import { parseClasses, type UmlClass } from './classModel' |
| 8 | +import type { UmlInfo } from './umlRelations' |
| 9 | + |
| 10 | +export interface ProjectFile { |
| 11 | + /** Pfad unter `src/`, z. B. `tiere/Hund.java`; ohne Ordner im Standard-Package. */ |
| 12 | + path: string |
| 13 | + text: string |
| 14 | +} |
| 15 | + |
| 16 | +export interface JavaProject { |
| 17 | + name: string |
| 18 | + files: ProjectFile[] |
| 19 | + /** Auch leere Packages, z. B. gleich nach „+ Package“. */ |
| 20 | + packages: string[] |
| 21 | + /** Was nur UML kennt (Aggregation, Name der Beziehung …), siehe `umlRelations.ts`. */ |
| 22 | + uml?: UmlInfo |
| 23 | +} |
| 24 | + |
| 25 | +/** Klasse mit Datei und Package, für Diagramm und Explorer. */ |
| 26 | +export interface ProjectClass extends UmlClass { |
| 27 | + path: string |
| 28 | + pkg: string |
| 29 | +} |
| 30 | + |
| 31 | +export const packageOf = (path: string) => (path.includes('/') ? path.slice(0, path.lastIndexOf('/')).replace(/\//g, '.') : '') |
| 32 | +export const fileNameOf = (path: string) => path.slice(path.lastIndexOf('/') + 1) |
| 33 | +export const pathFor = (pkg: string, file: string) => (pkg ? `${pkg.replace(/\./g, '/')}/${file}` : file) |
| 34 | + |
| 35 | +const packageName = /^[a-z_$][\p{L}\p{N}_$]*(\.[a-z_$][\p{L}\p{N}_$]*)*$/u |
| 36 | +export const isPackageName = (name: string) => packageName.test(name) |
| 37 | + |
| 38 | +/** Alle Packages: aus den Dateien und die leeren, sortiert; das Standard-Package nur, wenn Dateien darin liegen. */ |
| 39 | +export function packagesOf(project: JavaProject): string[] { |
| 40 | + const all = new Set(project.packages) |
| 41 | + for (const f of project.files) all.add(packageOf(f.path)) |
| 42 | + return [...all].filter((p) => p !== '' || project.files.some((f) => packageOf(f.path) === '')).sort((a, b) => (a === '' ? 1 : b === '' ? -1 : a.localeCompare(b))) |
| 43 | +} |
| 44 | + |
| 45 | +export function filesIn(project: JavaProject, pkg: string): ProjectFile[] { |
| 46 | + return project.files.filter((f) => packageOf(f.path) === pkg).sort((a, b) => a.path.localeCompare(b.path)) |
| 47 | +} |
| 48 | + |
| 49 | +/** `package …;` am Anfang setzen, ändern oder (Standard-Package) entfernen. */ |
| 50 | +export function withPackageLine(text: string, pkg: string): string { |
| 51 | + const line = /^(\s*(?:\/\/[^\n]*\n|\/\*[\s\S]*?\*\/\s*)*)package\s+[\w.]+\s*;[ \t]*\n?/ |
| 52 | + const match = line.exec(text) |
| 53 | + if (match) { |
| 54 | + if (!pkg) return match[1] + text.slice(match[0].length).replace(/^\n/, '') |
| 55 | + return `${match[1]}package ${pkg};\n` + text.slice(match[0].length) |
| 56 | + } |
| 57 | + if (!pkg) return text |
| 58 | + return `package ${pkg};\n\n${text.replace(/^\n+/, '')}` |
| 59 | +} |
| 60 | + |
| 61 | +/** Package aus der Zeile `package …;` lesen (für geöffnete Dateien). */ |
| 62 | +export function declaredPackage(text: string): string { |
| 63 | + return /^\s*(?:(?:\/\/[^\n]*|\/\*[\s\S]*?\*\/)\s*)*package\s+([\w.]+)\s*;/.exec(text)?.[1] ?? '' |
| 64 | +} |
| 65 | + |
| 66 | +export function projectClasses(project: JavaProject): ProjectClass[] { |
| 67 | + return project.files.flatMap((f) => parseClasses(f.text).map((c) => ({ ...c, path: f.path, pkg: packageOf(f.path) }))) |
| 68 | +} |
| 69 | + |
| 70 | +export function updateFile(project: JavaProject, path: string, text: string): JavaProject { |
| 71 | + return { ...project, files: project.files.map((f) => (f.path === path ? { ...f, text } : f)) } |
| 72 | +} |
| 73 | + |
| 74 | +export function addFile(project: JavaProject, path: string, text: string): JavaProject { |
| 75 | + return { ...project, files: [...project.files.filter((f) => f.path !== path), { path, text }] } |
| 76 | +} |
| 77 | + |
| 78 | +export function removeFile(project: JavaProject, path: string): JavaProject { |
| 79 | + // Das Package bleibt sichtbar, auch wenn es jetzt leer ist. |
| 80 | + const pkg = packageOf(path) |
| 81 | + return { ...project, files: project.files.filter((f) => f.path !== path), packages: pkg && !project.packages.includes(pkg) ? [...project.packages, pkg] : project.packages } |
| 82 | +} |
| 83 | + |
| 84 | +/** Datei umbenennen oder in ein anderes Package schieben; die `package`-Zeile passt sich an. */ |
| 85 | +export function moveFile(project: JavaProject, path: string, to: string): JavaProject { |
| 86 | + const pkg = packageOf(to) |
| 87 | + return { |
| 88 | + ...project, |
| 89 | + files: project.files.map((f) => (f.path === path ? { path: to, text: packageOf(path) === pkg ? f.text : withPackageLine(f.text, pkg) } : f)), |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +export function addPackage(project: JavaProject, pkg: string): JavaProject { |
| 94 | + return project.packages.includes(pkg) ? project : { ...project, packages: [...project.packages, pkg] } |
| 95 | +} |
| 96 | + |
| 97 | +/** Package umbenennen (auch Unter-Packages wie `tiere.haus`). */ |
| 98 | +export function renamePackage(project: JavaProject, from: string, to: string): JavaProject { |
| 99 | + const rename = (p: string) => (p === from ? to : p.startsWith(from + '.') ? to + p.slice(from.length) : p) |
| 100 | + return { |
| 101 | + ...project, |
| 102 | + packages: [...new Set(project.packages.map(rename))], |
| 103 | + files: project.files.map((f) => { |
| 104 | + const pkg = packageOf(f.path) |
| 105 | + const next = rename(pkg) |
| 106 | + return next === pkg ? f : { path: pathFor(next, fileNameOf(f.path)), text: withPackageLine(f.text, next) } |
| 107 | + }), |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +/** Package samt Dateien löschen. */ |
| 112 | +export function removePackage(project: JavaProject, pkg: string): JavaProject { |
| 113 | + return { ...project, packages: project.packages.filter((p) => p !== pkg), files: project.files.filter((f) => packageOf(f.path) !== pkg) } |
| 114 | +} |
| 115 | + |
| 116 | +/** Neue Datei für eine Klasse: `package …;` und ein leerer Rumpf. */ |
| 117 | +export function classFileText(pkg: string, name: string, kind: 'class' | 'interface'): string { |
| 118 | + return withPackageLine(`public ${kind} ${name} {\n}\n`, pkg) |
| 119 | +} |
| 120 | + |
| 121 | +/** Eine einzelne Datei (z. B. aus Stufe 1 oder ein geöffnetes `.java`) als Projekt. */ |
| 122 | +export function projectFromFiles(name: string, files: { name: string; text: string }[]): JavaProject { |
| 123 | + const out: ProjectFile[] = [] |
| 124 | + for (const f of files) { |
| 125 | + const text = f.text.replace(/\r\n/g, '\n') |
| 126 | + const pkg = declaredPackage(text) |
| 127 | + const publicClass = /\bpublic\s+(?:abstract\s+|final\s+)*(?:class|interface|enum)\s+(\w+)/.exec(text)?.[1] |
| 128 | + const fileName = /\.java$/i.test(f.name) ? fileNameOf(f.name) : `${publicClass ?? parseClasses(text)[0]?.name ?? 'Main'}.java` |
| 129 | + out.push({ path: pathFor(pkg, fileName), text }) |
| 130 | + } |
| 131 | + return { name, files: out, packages: [] } |
| 132 | +} |
| 133 | + |
| 134 | +/** Speichern im Browser und als `.visuobject`-Datei. */ |
| 135 | +export function projectToJSON(project: JavaProject): string { |
| 136 | + return JSON.stringify({ format: 'visuobject', version: 1, ...project }) |
| 137 | +} |
| 138 | + |
| 139 | +export function projectFromJSON(json: string): JavaProject | undefined { |
| 140 | + try { |
| 141 | + const data = JSON.parse(json) as Partial<JavaProject> |
| 142 | + if (!Array.isArray(data.files)) return undefined |
| 143 | + const files = data.files.filter((f): f is ProjectFile => typeof f?.path === 'string' && typeof f?.text === 'string') |
| 144 | + const project: JavaProject = { name: typeof data.name === 'string' && data.name ? data.name : 'Projekt', files, packages: Array.isArray(data.packages) ? data.packages.filter((p) => typeof p === 'string') : [] } |
| 145 | + const uml = umlFromJSON(data.uml) |
| 146 | + if (uml) project.uml = uml |
| 147 | + return project |
| 148 | + } catch { |
| 149 | + return undefined |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +/** Nur Einträge mit Objekt als Wert übernehmen (Datei kann von außen kommen). */ |
| 154 | +export function umlFromJSON(data: unknown): UmlInfo | undefined { |
| 155 | + if (!data || typeof data !== 'object' || Array.isArray(data)) return undefined |
| 156 | + const out: UmlInfo = {} |
| 157 | + for (const [key, value] of Object.entries(data as Record<string, unknown>)) if (value && typeof value === 'object' && !Array.isArray(value)) out[key] = value as UmlInfo[string] |
| 158 | + return Object.keys(out).length ? out : undefined |
| 159 | +} |
| 160 | + |
| 161 | +// MARK: - Beispiel |
| 162 | + |
| 163 | +/** Beispiel wie im Spickzettel Java OOP & UML: Tier und Hund im Package `tiere`, Besitzer in `personen`. */ |
| 164 | +export function exampleProject(): JavaProject { |
| 165 | + return { |
| 166 | + name: 'Zoo', |
| 167 | + packages: [], |
| 168 | + uml: { 'Besitzer.hunde': { kind: 'aggregation', name: 'führt aus', reading: 'forward', sourceMultiplicity: '1' } }, |
| 169 | + files: [ |
| 170 | + { |
| 171 | + path: 'tiere/Tier.java', |
| 172 | + text: `package tiere; |
| 173 | +
|
| 174 | +public class Tier { |
| 175 | + private String name; |
| 176 | + private int alter; |
| 177 | +
|
| 178 | + public Tier(String name, int alter) { |
| 179 | + this.name = name; |
| 180 | + this.alter = alter; |
| 181 | + } |
| 182 | +
|
| 183 | + public String getName() { |
| 184 | + return name; |
| 185 | + } |
| 186 | +
|
| 187 | + public void lautGeben() { |
| 188 | + System.out.println("..."); |
| 189 | + } |
| 190 | +} |
| 191 | +`, |
| 192 | + }, |
| 193 | + { |
| 194 | + path: 'tiere/Hund.java', |
| 195 | + text: `package tiere; |
| 196 | +
|
| 197 | +public class Hund extends Tier { |
| 198 | + private String rasse; |
| 199 | +
|
| 200 | + public Hund(String name, int alter, String rasse) { |
| 201 | + super(name, alter); |
| 202 | + this.rasse = rasse; |
| 203 | + } |
| 204 | +
|
| 205 | + public void lautGeben() { |
| 206 | + System.out.println("Wuff"); |
| 207 | + } |
| 208 | +} |
| 209 | +`, |
| 210 | + }, |
| 211 | + { |
| 212 | + path: 'personen/Besitzer.java', |
| 213 | + text: `package personen; |
| 214 | +
|
| 215 | +import java.util.ArrayList; |
| 216 | +import tiere.Hund; |
| 217 | +
|
| 218 | +public class Besitzer { |
| 219 | + private String name; |
| 220 | + private ArrayList<Hund> hunde = new ArrayList<>(); |
| 221 | +
|
| 222 | + public void gassiGehen() { |
| 223 | + for (Hund h : hunde) { |
| 224 | + h.lautGeben(); |
| 225 | + } |
| 226 | + } |
| 227 | +} |
| 228 | +`, |
| 229 | + }, |
| 230 | + ], |
| 231 | + } |
| 232 | +} |
0 commit comments