KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > model > build > Build


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.model.build;
12
13 import java.io.*;
14 import java.util.*;
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.jface.text.*;
17 import org.eclipse.pde.core.*;
18 import org.eclipse.pde.core.build.*;
19 import org.eclipse.pde.internal.ui.model.*;
20
21 public class Build implements IBuild {
22     
23     private BuildModel fModel;
24     private HashMap fEntries = new HashMap();
25
26     public Build(BuildModel model) {
27         fModel = model;
28     }
29
30     /* (non-Javadoc)
31      * @see org.eclipse.pde.core.build.IBuild#add(org.eclipse.pde.core.build.IBuildEntry)
32      */

33     public void add(IBuildEntry entry) throws CoreException {
34         fEntries.put(entry.getName(), entry);
35         fModel.fireModelChanged(new ModelChangedEvent(fModel,
36                 IModelChangedEvent.INSERT, new Object JavaDoc[]{entry}, null));
37     }
38     /* (non-Javadoc)
39      * @see org.eclipse.pde.core.build.IBuild#getBuildEntries()
40      */

41     public IBuildEntry[] getBuildEntries() {
42         return (IBuildEntry[])fEntries.values().toArray(new IBuildEntry[fEntries.size()]);
43     }
44     /* (non-Javadoc)
45      * @see org.eclipse.pde.core.build.IBuild#getEntry(java.lang.String)
46      */

47     public IBuildEntry getEntry(String JavaDoc name) {
48         return (IBuildEntry)fEntries.get(name);
49     }
50     /* (non-Javadoc)
51      * @see org.eclipse.pde.core.build.IBuild#remove(org.eclipse.pde.core.build.IBuildEntry)
52      */

53     public void remove(IBuildEntry entry) throws CoreException {
54         fEntries.remove(entry.getName());
55         fModel.fireModelChanged(new ModelChangedEvent(fModel,
56                 IModelChangedEvent.REMOVE, new Object JavaDoc[]{entry}, null));
57     }
58     /*
59      * (non-Javadoc)
60      *
61      * @see org.eclipse.pde.core.IWritable#write(java.lang.String,
62      * java.io.PrintWriter)
63      */

64     public void write(String JavaDoc indent, PrintWriter writer) {
65     }
66     
67     public void load(InputStream source) throws IOException {
68         fEntries.clear();
69         Properties properties = new Properties();
70         properties.load(source);
71         Enumeration keys = properties.keys();
72         while (keys.hasMoreElements()) {
73             String JavaDoc name = keys.nextElement().toString();
74             BuildEntry entry = (BuildEntry)fModel.getFactory().createEntry(name);
75             entry.processEntry(properties.get(name).toString());
76             fEntries.put(name, entry);
77         }
78         adjustOffsets(fModel.getDocument());
79     }
80     
81     public void adjustOffsets(IDocument document) {
82         int lines = document.getNumberOfLines();
83         try {
84             IDocumentKey currentKey = null;
85             for (int i = 0; i < lines; i++) {
86                 int offset = document.getLineOffset(i);
87                 int length = document.getLineLength(i);
88                 String JavaDoc line = document.get(offset, length);
89                 if (line.startsWith("#") | line.startsWith("!")) { //$NON-NLS-1$ //$NON-NLS-2$
90
if (currentKey != null) {
91                         currentKey.setLength(offset - 1 - currentKey.getOffset());
92                         currentKey = null;
93                     }
94                     continue;
95                 }
96                 
97                 line = line.trim();
98                 if (line.length() == 0)
99                     continue;
100                 
101                 if (currentKey != null) {
102                     if (!line.endsWith("\\")) { //$NON-NLS-1$
103
currentKey.setLength(offset + document.getLineLength(i) - currentKey.getOffset());
104                         currentKey = null;
105                     }
106                 } else {
107                     int index = line.indexOf('=');
108                     if (index == -1)
109                         index = line.indexOf(':');
110                     if (index == -1)
111                         index = line.indexOf(' ');
112                     if (index == -1)
113                         index = line.indexOf('\t');
114                     String JavaDoc name = (index != -1) ? line.substring(0, index).trim() : line;
115                     currentKey = (IDocumentKey)getEntry(name);
116                     if (currentKey != null) {
117                         while (Character.isSpaceChar(document.getChar(offset))) {
118                             offset += 1;
119                         }
120                         currentKey.setOffset(offset);
121                         if (!line.endsWith("\\")) { //$NON-NLS-1$
122
currentKey.setLength(document.getLineOffset(i) + document.getLineLength(i) - currentKey.getOffset());
123                             currentKey = null;
124                         }
125                     }
126                 }
127             }
128         } catch (BadLocationException e) {
129         }
130     }
131
132 }
133
Popular Tags