KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > text > build > Build


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.core.text.build;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.io.PrintWriter JavaDoc;
16 import java.util.Enumeration JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Properties JavaDoc;
19
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.jface.text.BadLocationException;
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.pde.core.IModelChangedEvent;
24 import org.eclipse.pde.core.ModelChangedEvent;
25 import org.eclipse.pde.core.build.IBuild;
26 import org.eclipse.pde.core.build.IBuildEntry;
27 import org.eclipse.pde.internal.core.text.IDocumentKey;
28 import org.eclipse.pde.internal.core.util.PropertiesUtil;
29
30 public class Build implements IBuild {
31     
32     private BuildModel fModel;
33     private HashMap JavaDoc fEntries = new HashMap JavaDoc();
34
35     public Build(BuildModel model) {
36         fModel = model;
37     }
38
39     /* (non-Javadoc)
40      * @see org.eclipse.pde.core.build.IBuild#add(org.eclipse.pde.core.build.IBuildEntry)
41      */

42     public void add(IBuildEntry entry) throws CoreException {
43         fEntries.put(entry.getName(), entry);
44         fModel.fireModelChanged(new ModelChangedEvent(fModel,
45                 IModelChangedEvent.INSERT, new Object JavaDoc[]{entry}, null));
46     }
47     /* (non-Javadoc)
48      * @see org.eclipse.pde.core.build.IBuild#getBuildEntries()
49      */

50     public IBuildEntry[] getBuildEntries() {
51         return (IBuildEntry[])fEntries.values().toArray(new IBuildEntry[fEntries.size()]);
52     }
53     /* (non-Javadoc)
54      * @see org.eclipse.pde.core.build.IBuild#getEntry(java.lang.String)
55      */

56     public IBuildEntry getEntry(String JavaDoc name) {
57         return (IBuildEntry)fEntries.get(name);
58     }
59     /* (non-Javadoc)
60      * @see org.eclipse.pde.core.build.IBuild#remove(org.eclipse.pde.core.build.IBuildEntry)
61      */

62     public void remove(IBuildEntry entry) throws CoreException {
63         if (fEntries.remove(entry.getName()) != null)
64             fModel.fireModelChanged(new ModelChangedEvent(fModel,
65                     IModelChangedEvent.REMOVE, new Object JavaDoc[]{entry}, null));
66     }
67     /*
68      * (non-Javadoc)
69      *
70      * @see org.eclipse.pde.core.IWritable#write(java.lang.String,
71      * java.io.PrintWriter)
72      */

73     public void write(String JavaDoc indent, PrintWriter JavaDoc writer) {
74     }
75     
76     public void load(InputStream JavaDoc source) throws IOException JavaDoc {
77         fEntries.clear();
78         Properties JavaDoc properties = new Properties JavaDoc();
79         properties.load(source);
80         Enumeration JavaDoc keys = properties.keys();
81         while (keys.hasMoreElements()) {
82             String JavaDoc name = keys.nextElement().toString();
83             BuildEntry entry = (BuildEntry)fModel.getFactory().createEntry(name);
84             entry.processEntry(properties.get(name).toString());
85             fEntries.put(name, entry);
86         }
87         adjustOffsets(fModel.getDocument());
88     }
89     
90     public void adjustOffsets(IDocument document) {
91         int lines = document.getNumberOfLines();
92         try {
93             IDocumentKey currentKey = null;
94             for (int i = 0; i < lines; i++) {
95                 int offset = document.getLineOffset(i);
96                 int length = document.getLineLength(i);
97                 String JavaDoc line = document.get(offset, length);
98                 if (line.startsWith("#") | line.startsWith("!")) { //$NON-NLS-1$ //$NON-NLS-2$
99
if (currentKey != null) {
100                         currentKey.setLength(offset - 1 - currentKey.getOffset());
101                         currentKey = null;
102                     }
103                     continue;
104                 }
105                 
106                 line = line.trim();
107                 if (line.length() == 0) {
108                     // we are at last line, set the length of the last key.
109
// we are here because the properties file ends with a trailing unnecessary backslash
110
if (currentKey != null && i == lines - 1) {
111                         currentKey.setLength(offset - 1 - currentKey.getOffset());
112                         currentKey = null;
113                     }
114                     continue;
115                 }
116                 
117                 if (currentKey != null) {
118                     if (!line.endsWith("\\")) { //$NON-NLS-1$
119
currentKey.setLength(offset + document.getLineLength(i) - currentKey.getOffset());
120                         currentKey = null;
121                     }
122                 } else {
123                     int index = line.indexOf('=');
124                     if (index == -1)
125                         index = line.indexOf(':');
126                     if (index == -1)
127                         index = line.indexOf(' ');
128                     if (index == -1)
129                         index = line.indexOf('\t');
130                     String JavaDoc name = (index != -1) ? line.substring(0, index).trim() : line;
131                     String JavaDoc propertyKey;
132                     try{
133                         propertyKey=PropertiesUtil.windEscapeChars(name);
134                     }catch(IllegalArgumentException JavaDoc iae){
135                         propertyKey = name;
136                     }
137                     currentKey = (IDocumentKey)getEntry(propertyKey);
138                     if (currentKey != null) {
139                         while (Character.isSpaceChar(document.getChar(offset))) {
140                             offset += 1;
141                         }
142                         currentKey.setOffset(offset);
143                         if (!line.endsWith("\\")) { //$NON-NLS-1$
144
currentKey.setLength(document.getLineOffset(i) + document.getLineLength(i) - currentKey.getOffset());
145                             currentKey = null;
146                         }
147                     }
148                 }
149             }
150         } catch (BadLocationException e) {
151         }
152     }
153
154     public BuildModel getModel() {
155         return fModel;
156     }
157 }
158
Popular Tags