1 19 20 package org.netbeans.modules.project.libraries; 21 22 import java.net.URL ; 23 import java.util.ArrayList ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 28 import org.netbeans.spi.project.libraries.LibraryImplementation; 29 import org.netbeans.spi.project.libraries.LibraryTypeProvider; 30 import org.openide.ErrorManager; 31 import org.xml.sax.Attributes ; 32 import org.xml.sax.SAXException ; 33 import org.xml.sax.SAXParseException ; 34 35 40 public class LibraryDeclarationHandlerImpl implements LibraryDeclarationHandler { 41 42 43 private LibraryImplementation library; 44 45 private String libraryType; 46 47 private String libraryDescription; 48 49 private String libraryName; 50 51 private String localizingBundle; 52 53 private Map <String ,List <URL >> contentTypes = new HashMap <String ,List <URL >>(); 54 55 private List <URL > cpEntries; 57 58 private String contentType; 60 61 private boolean inVolume = false; 63 64 public static final boolean DEBUG = false; 65 66 67 69 public LibraryDeclarationHandlerImpl() { 70 } 71 72 public void start_volume(final Attributes meta) throws SAXException { 73 cpEntries = new ArrayList <URL >(); 74 this.inVolume = true; 75 } 76 77 public void end_volume() throws SAXException { 78 contentTypes.put (contentType, cpEntries); 79 this.inVolume = false; 80 this.contentType = null; 81 } 82 83 public void handle_type(final String data, final Attributes meta) throws SAXException { 84 if (data == null || data.length () == 0) { 85 throw new SAXException ("Empty value of type element"); } 87 if (this.inVolume) { 88 this.contentType = data; 89 } 90 else { 91 this.libraryType = data; 92 } 93 } 94 95 public void start_library(final Attributes meta) throws SAXException { 96 if ("1.0".equals(meta.getValue("version")) == false) { throw new SAXException ("Invalid librray descriptor version"); } 99 } 100 101 public void end_library() throws SAXException { 102 boolean update; 103 if (this.library != null) { 104 if (this.libraryType == null || !this.libraryType.equals(this.library.getType())) { 105 throw new SAXParseException ("Changing library type of library: "+this.libraryName+" from: "+ 106 library.getType()+" to: " + libraryType, null); } 108 update = true; 109 } 110 else { 111 LibraryTypeProvider provider = LibraryTypeRegistry.getDefault().getLibraryTypeProvider(this.libraryType); 112 if (provider == null) { 113 ErrorManager.getDefault().log (ErrorManager.WARNING, "LibraryDeclarationHandlerImpl: Cannot create library: "+this.libraryName+" of unknown type: " + this.libraryType); 114 return; 115 } 116 this.library = provider.createLibrary(); 117 update = false; 118 } 119 if (!update || !safeEquals(this.library.getLocalizingBundle(), localizingBundle)) { 120 this.library.setLocalizingBundle (this.localizingBundle); 121 } 122 if (!update || !safeEquals(this.library.getName(), libraryName)) { 123 this.library.setName (this.libraryName); 124 } 125 if (!update || !safeEquals(this.library.getDescription(), libraryDescription)) { 126 this.library.setDescription (this.libraryDescription); 127 } 128 for (Map.Entry <String ,List <URL >> entry : contentTypes.entrySet()) { 129 String contentType = entry.getKey(); 130 List <URL > cp = entry.getValue(); 131 try { 132 if (!update || !safeEquals (this.library.getContent(contentType),cp)) { 133 this.library.setContent(contentType, cp); 134 } 135 } catch (IllegalArgumentException e) { 136 throw (SAXException ) new SAXException (e.toString()).initCause(e); 137 } 138 } 139 this.libraryName = null; 140 this.libraryDescription = null; 141 this.libraryType = null; 142 this.localizingBundle = null; 143 this.contentTypes.clear (); 144 } 145 146 public void handle_resource(URL data, final Attributes meta) throws SAXException { 147 if (data != null) { 148 cpEntries.add(data); 149 } 150 } 151 152 public void handle_name(final String data, final Attributes meta) throws SAXException { 153 this.libraryName = data; 154 } 155 156 public void handle_description (final String data, final Attributes meta) throws SAXException { 157 libraryDescription = data; 158 } 159 160 public void handle_localizingBundle (final String data, final Attributes meta) throws SAXException { 161 this.localizingBundle = data; 162 } 163 164 public void setLibrary (LibraryImplementation library) { 165 this.library = library; 166 } 167 168 public LibraryImplementation getLibrary () { 169 LibraryImplementation lib = this.library; 170 this.library = null; 171 return lib; 172 } 173 174 175 private static boolean safeEquals (Object o1, Object o2) { 176 return o1 == null ? o2 == null : o1.equals (o2); 177 } 178 179 } 180 | Popular Tags |