KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > UserLibrary


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.jdt.internal.core;
12
13 import java.io.ByteArrayOutputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.OutputStreamWriter JavaDoc;
16 import java.io.Reader JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import javax.xml.parsers.DocumentBuilder JavaDoc;
20 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
21 import javax.xml.parsers.ParserConfigurationException JavaDoc;
22
23 import org.eclipse.core.runtime.Assert;
24 import org.eclipse.core.runtime.IPath;
25 import org.eclipse.core.runtime.Path;
26 import org.eclipse.jdt.core.IAccessRule;
27 import org.eclipse.jdt.core.IClasspathAttribute;
28 import org.eclipse.jdt.core.IClasspathEntry;
29 import org.eclipse.jdt.core.JavaCore;
30 import org.eclipse.jdt.internal.core.util.Messages;
31 import org.w3c.dom.Element JavaDoc;
32 import org.w3c.dom.Node JavaDoc;
33 import org.w3c.dom.NodeList JavaDoc;
34 import org.xml.sax.InputSource JavaDoc;
35 import org.xml.sax.SAXException JavaDoc;
36
37 /**
38  * Internal model element to represent a user library and code to serialize / deserialize.
39  */

40 public class UserLibrary {
41
42     private static final String JavaDoc CURRENT_VERSION= "1"; //$NON-NLS-1$
43

44     private static final String JavaDoc TAG_VERSION= "version"; //$NON-NLS-1$
45
private static final String JavaDoc TAG_USERLIBRARY= "userlibrary"; //$NON-NLS-1$
46
private static final String JavaDoc TAG_SOURCEATTACHMENT= "sourceattachment"; //$NON-NLS-1$
47
private static final String JavaDoc TAG_SOURCEATTACHMENTROOT= "sourceattachmentroot"; //$NON-NLS-1$
48
private static final String JavaDoc TAG_PATH= "path"; //$NON-NLS-1$
49
private static final String JavaDoc TAG_ARCHIVE= "archive"; //$NON-NLS-1$
50
private static final String JavaDoc TAG_SYSTEMLIBRARY= "systemlibrary"; //$NON-NLS-1$
51

52     private boolean isSystemLibrary;
53     private IClasspathEntry[] entries;
54
55     public UserLibrary(IClasspathEntry[] entries, boolean isSystemLibrary) {
56         Assert.isNotNull(entries);
57         this.entries= entries;
58         this.isSystemLibrary= isSystemLibrary;
59     }
60     
61     public IClasspathEntry[] getEntries() {
62         return this.entries;
63     }
64     
65     public boolean isSystemLibrary() {
66         return this.isSystemLibrary;
67     }
68
69     /* (non-Javadoc)
70      * @see java.lang.Object#equals(java.lang.Object)
71      */

72     public boolean equals(Object JavaDoc obj) {
73         if (obj != null && obj.getClass() == getClass()) {
74             UserLibrary other= (UserLibrary) obj;
75             if (this.entries.length == other.entries.length && this.isSystemLibrary == other.isSystemLibrary) {
76                 for (int i= 0; i < this.entries.length; i++) {
77                     if (!this.entries[i].equals(other.entries[i])) {
78                         return false;
79                     }
80                 }
81                 return true;
82             }
83         }
84         return false;
85     }
86     
87     /* (non-Javadoc)
88      * @see java.lang.Object#hashCode()
89      */

90     public int hashCode() {
91         int hashCode= 0;
92         if (this.isSystemLibrary) {
93             hashCode++;
94         }
95         for (int i= 0; i < this.entries.length; i++) {
96             hashCode= hashCode * 17 + this.entries.hashCode();
97         }
98         return hashCode;
99     }
100     
101     public static String JavaDoc serialize(IClasspathEntry[] entries, boolean isSystemLibrary) throws IOException JavaDoc {
102         ByteArrayOutputStream JavaDoc s = new ByteArrayOutputStream JavaDoc();
103         OutputStreamWriter JavaDoc writer = new OutputStreamWriter JavaDoc(s, "UTF8"); //$NON-NLS-1$
104
XMLWriter xmlWriter = new XMLWriter(writer, null/*use the workspace line delimiter*/, true/*print XML version*/);
105         
106         HashMap JavaDoc library = new HashMap JavaDoc();
107         library.put(TAG_VERSION, String.valueOf(CURRENT_VERSION));
108         library.put(TAG_SYSTEMLIBRARY, String.valueOf(isSystemLibrary));
109         xmlWriter.printTag(TAG_USERLIBRARY, library, true, true, false);
110         
111         for (int i = 0, length = entries.length; i < length; ++i) {
112             ClasspathEntry cpEntry = (ClasspathEntry) entries[i];
113         
114             HashMap JavaDoc archive = new HashMap JavaDoc();
115             archive.put(TAG_PATH, cpEntry.getPath().toString());
116             IPath sourceAttach= cpEntry.getSourceAttachmentPath();
117             if (sourceAttach != null)
118                 archive.put(TAG_SOURCEATTACHMENT, sourceAttach);
119             IPath sourceAttachRoot= cpEntry.getSourceAttachmentRootPath();
120             if (sourceAttachRoot != null)
121                 archive.put(TAG_SOURCEATTACHMENTROOT, sourceAttachRoot);
122
123             boolean hasExtraAttributes = cpEntry.extraAttributes != null && cpEntry.extraAttributes.length != 0;
124             boolean hasRestrictions = cpEntry.getAccessRuleSet() != null; // access rule set is null if no access rules
125
xmlWriter.printTag(TAG_ARCHIVE, archive, true, true, !(hasExtraAttributes || hasRestrictions));
126
127             // write extra attributes if necessary
128
if (hasExtraAttributes) {
129                 cpEntry.encodeExtraAttributes(xmlWriter, true, true);
130             }
131
132             // write extra attributes and restriction if necessary
133
if (hasRestrictions) {
134                 cpEntry.encodeAccessRules(xmlWriter, true, true);
135             }
136
137             // write archive end tag if necessary
138
if (hasExtraAttributes || hasRestrictions) {
139                 xmlWriter.endTag(TAG_ARCHIVE, true/*insert tab*/, true/*insert new line*/);
140             }
141         }
142         xmlWriter.endTag(TAG_USERLIBRARY, true/*insert tab*/, true/*insert new line*/);
143         writer.flush();
144         writer.close();
145         return s.toString("UTF8");//$NON-NLS-1$
146
}
147     
148     public static UserLibrary createFromString(Reader JavaDoc reader) throws IOException JavaDoc {
149         Element JavaDoc cpElement;
150         try {
151             DocumentBuilder JavaDoc parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
152             cpElement = parser.parse(new InputSource JavaDoc(reader)).getDocumentElement();
153         } catch (SAXException JavaDoc e) {
154             throw new IOException JavaDoc(Messages.file_badFormat);
155         } catch (ParserConfigurationException JavaDoc e) {
156             throw new IOException JavaDoc(Messages.file_badFormat);
157         } finally {
158             reader.close();
159         }
160         
161         if (!cpElement.getNodeName().equalsIgnoreCase(TAG_USERLIBRARY)) {
162             throw new IOException JavaDoc(Messages.file_badFormat);
163         }
164         // String version= cpElement.getAttribute(TAG_VERSION);
165
// in case we update the format: add code to read older versions
166

167         boolean isSystem= Boolean.valueOf(cpElement.getAttribute(TAG_SYSTEMLIBRARY)).booleanValue();
168         
169         NodeList JavaDoc list= cpElement.getChildNodes();
170         int length = list.getLength();
171         
172         ArrayList JavaDoc res= new ArrayList JavaDoc(length);
173         for (int i = 0; i < length; ++i) {
174             Node JavaDoc node = list.item(i);
175             
176             if (node.getNodeType() == Node.ELEMENT_NODE) {
177                 Element JavaDoc element= (Element JavaDoc) node;
178                 if (element.getNodeName().equals(TAG_ARCHIVE)) {
179                     String JavaDoc path = element.getAttribute(TAG_PATH);
180                     IPath sourceAttach= element.hasAttribute(TAG_SOURCEATTACHMENT) ? new Path(element.getAttribute(TAG_SOURCEATTACHMENT)) : null;
181                     IPath sourceAttachRoot= element.hasAttribute(TAG_SOURCEATTACHMENTROOT) ? new Path(element.getAttribute(TAG_SOURCEATTACHMENTROOT)) : null;
182                     NodeList JavaDoc children = element.getElementsByTagName("*"); //$NON-NLS-1$
183
boolean[] foundChildren = new boolean[children.getLength()];
184                     NodeList JavaDoc attributeList = ClasspathEntry.getChildAttributes(ClasspathEntry.TAG_ATTRIBUTES, children, foundChildren);
185                     IClasspathAttribute[] extraAttributes = ClasspathEntry.decodeExtraAttributes(attributeList);
186                     attributeList = ClasspathEntry.getChildAttributes(ClasspathEntry.TAG_ACCESS_RULES, children, foundChildren);
187                     IAccessRule[] accessRules = ClasspathEntry.decodeAccessRules(attributeList);
188                     IClasspathEntry entry = JavaCore.newLibraryEntry(new Path(path), sourceAttach, sourceAttachRoot, accessRules, extraAttributes, false/*not exported*/);
189                     res.add(entry);
190                 }
191             }
192         }
193         
194         IClasspathEntry[] entries= (IClasspathEntry[]) res.toArray(new IClasspathEntry[res.size()]);
195         
196         return new UserLibrary(entries, isSystem);
197     }
198     
199     public String JavaDoc toString() {
200         if (this.entries == null)
201             return "null"; //$NON-NLS-1$
202
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
203         int length = this.entries.length;
204         for (int i=0; i<length; i++) {
205             buffer.append(this.entries[i].toString()+'\n');
206         }
207         return buffer.toString();
208     }
209 }
210
Popular Tags