1 11 package org.eclipse.jdt.internal.core; 12 13 import java.io.ByteArrayOutputStream ; 14 import java.io.IOException ; 15 import java.io.OutputStreamWriter ; 16 import java.io.Reader ; 17 import java.util.ArrayList ; 18 import java.util.HashMap ; 19 import javax.xml.parsers.DocumentBuilder ; 20 import javax.xml.parsers.DocumentBuilderFactory ; 21 import javax.xml.parsers.ParserConfigurationException ; 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 ; 32 import org.w3c.dom.Node ; 33 import org.w3c.dom.NodeList ; 34 import org.xml.sax.InputSource ; 35 import org.xml.sax.SAXException ; 36 37 40 public class UserLibrary { 41 42 private static final String CURRENT_VERSION= "1"; 44 private static final String TAG_VERSION= "version"; private static final String TAG_USERLIBRARY= "userlibrary"; private static final String TAG_SOURCEATTACHMENT= "sourceattachment"; private static final String TAG_SOURCEATTACHMENTROOT= "sourceattachmentroot"; private static final String TAG_PATH= "path"; private static final String TAG_ARCHIVE= "archive"; private static final String TAG_SYSTEMLIBRARY= "systemlibrary"; 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 72 public boolean equals(Object 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 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 serialize(IClasspathEntry[] entries, boolean isSystemLibrary) throws IOException { 102 ByteArrayOutputStream s = new ByteArrayOutputStream (); 103 OutputStreamWriter writer = new OutputStreamWriter (s, "UTF8"); XMLWriter xmlWriter = new XMLWriter(writer, null, true); 105 106 HashMap library = new HashMap (); 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 archive = new HashMap (); 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; xmlWriter.printTag(TAG_ARCHIVE, archive, true, true, !(hasExtraAttributes || hasRestrictions)); 126 127 if (hasExtraAttributes) { 129 cpEntry.encodeExtraAttributes(xmlWriter, true, true); 130 } 131 132 if (hasRestrictions) { 134 cpEntry.encodeAccessRules(xmlWriter, true, true); 135 } 136 137 if (hasExtraAttributes || hasRestrictions) { 139 xmlWriter.endTag(TAG_ARCHIVE, true, true); 140 } 141 } 142 xmlWriter.endTag(TAG_USERLIBRARY, true, true); 143 writer.flush(); 144 writer.close(); 145 return s.toString("UTF8"); } 147 148 public static UserLibrary createFromString(Reader reader) throws IOException { 149 Element cpElement; 150 try { 151 DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 152 cpElement = parser.parse(new InputSource (reader)).getDocumentElement(); 153 } catch (SAXException e) { 154 throw new IOException (Messages.file_badFormat); 155 } catch (ParserConfigurationException e) { 156 throw new IOException (Messages.file_badFormat); 157 } finally { 158 reader.close(); 159 } 160 161 if (!cpElement.getNodeName().equalsIgnoreCase(TAG_USERLIBRARY)) { 162 throw new IOException (Messages.file_badFormat); 163 } 164 167 boolean isSystem= Boolean.valueOf(cpElement.getAttribute(TAG_SYSTEMLIBRARY)).booleanValue(); 168 169 NodeList list= cpElement.getChildNodes(); 170 int length = list.getLength(); 171 172 ArrayList res= new ArrayList (length); 173 for (int i = 0; i < length; ++i) { 174 Node node = list.item(i); 175 176 if (node.getNodeType() == Node.ELEMENT_NODE) { 177 Element element= (Element ) node; 178 if (element.getNodeName().equals(TAG_ARCHIVE)) { 179 String 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 children = element.getElementsByTagName("*"); boolean[] foundChildren = new boolean[children.getLength()]; 184 NodeList 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); 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 toString() { 200 if (this.entries == null) 201 return "null"; StringBuffer buffer = new StringBuffer (); 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 |