]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.export.core/src/org/simantics/export/core/impl/ExportExtensionPointImpl.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.export.core / src / org / simantics / export / core / impl / ExportExtensionPointImpl.java
1 package org.simantics.export.core.impl;
2
3 import java.io.File;
4 import java.lang.ref.WeakReference;
5 import java.net.MalformedURLException;
6 import java.net.URL;
7 import java.util.Arrays;
8 import java.util.Collection;
9 import java.util.Comparator;
10 import java.util.List;
11 import java.util.Map;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IConfigurationElement;
15 import org.eclipse.core.runtime.Platform;
16 import org.eclipse.jface.resource.ImageDescriptor;
17 import org.osgi.service.prefs.Preferences;
18 import org.simantics.databoard.binding.mutable.Variant;
19 import org.simantics.databoard.type.RecordType;
20 import org.simantics.export.core.ExportContext;
21 import org.simantics.export.core.ExportExtensionPoint;
22 import org.simantics.export.core.error.ExportException;
23 import org.simantics.export.core.intf.ContentType;
24 import org.simantics.export.core.intf.ContentTypeAction;
25 import org.simantics.export.core.intf.DiscoverAction;
26 import org.simantics.export.core.intf.Discoverer;
27 import org.simantics.export.core.intf.ExportClass;
28 import org.simantics.export.core.intf.Exporter;
29 import org.simantics.export.core.intf.Format;
30 import org.simantics.export.core.intf.FormatClass;
31 import org.simantics.export.core.intf.IconResolver;
32 import org.simantics.export.core.intf.ImportAction;
33 import org.simantics.export.core.intf.Importer;
34 import org.simantics.export.core.intf.Publisher;
35 import org.simantics.export.core.intf.PublisherClass;
36 import org.simantics.export.core.manager.Content;
37 import org.simantics.utils.datastructures.ToStringComparator;
38
39 /**
40  * Light-weight registry implementation to export extension point.
41  *
42  * @author toni.kalajainen@semantum.fi
43  */
44 public class ExportExtensionPointImpl implements ExportExtensionPoint {
45
46         public static final String EP = "org.simantics.export.core.export";
47         
48         Importer[] importers = createImporters();
49         Exporter[] exporters = createExporters();
50         Format[] formats = createFormats();
51         ContentType[] contentTypes = createContentTypes();
52         Discoverer[] discoverers = createDiscoverers();
53         Publisher[] publishers = createPublishers();
54         
55         public ExportExtensionPointImpl() {}
56
57         public Discoverer[] getDiscoverers(String contentTypeId) {
58                 int count = 0;
59                 for ( Discoverer discoverer : discoverers ) {
60                         if ( contentTypeId.equals( discoverer.contentTypeId() ) ) count++;
61                 }
62                 
63                 Discoverer[] result = new Discoverer[ count ];
64                 int i = 0;
65                 for ( Discoverer discoverer : discoverers ) {
66                         if ( contentTypeId.equals( discoverer.contentTypeId() ) ) {                             
67                                 result[ i++ ] = discoverer;
68                         }
69                 }
70                 return result;
71                 
72         }
73         
74         public Format getFormat(String formatId) 
75         {
76                 for ( Format format : formats ) {
77                         if ( formatId.equals( format.id() ) ) return format;
78                 }
79                 return null;
80         }
81
82         @Override
83         public Format getFormatByExt(String fileExt) {
84                 for ( Format format : formats ) {
85                         if ( fileExt.equals( format.fileext() ) ) return format;
86                 }
87                 return null;
88         }       
89         public ContentType getContentType(String contentTypeId) 
90                         //throws ExportException 
91         {
92                 for ( ContentType contentType : contentTypes ) {
93                         if ( contentTypeId.equals( contentType.id() ) ) return contentType;
94                 }
95                 //throw new ExportException("ContentType "+contentTypeId+" does not exist");
96                 return null;
97         }
98         
99         public Importer getImporter(String formatId) 
100                         //throws ExportException 
101         {
102                 for ( Importer importer : importers ) {
103                         if ( formatId.equals( importer.formatId() ) ) return importer;
104                 }
105                 //throw new ExportException("Importer "+formatId+" does not exist");
106                 return null;
107         }
108         
109         public Exporter[] getExporters(String formatId, String contentTypeId) 
110         {
111                 int count = 0;
112                 for ( Exporter exporter : exporters ) {
113                         if ( exporter.formatId().equals(formatId) &&
114                              exporter.contentTypeId().equals(contentTypeId) ) count++;
115                 }
116                 Exporter[] result = new Exporter[ count ];
117                 int index = 0;
118                 for ( Exporter exporter : exporters ) {
119                         if ( formatId.equals( exporter.formatId() ) && contentTypeId.equals( exporter.contentTypeId() ) ) {
120                                 result[ index++ ] = exporter;
121                         }
122                 }
123                 
124                 return result;
125         }
126         
127         public Exporter[] getExportersForContentType(String contentTypeId) 
128                         //throws ExportException 
129         {
130                 int count = 0;
131                 for ( Exporter exporter : exporters ) {
132                         if ( contentTypeId.equals( exporter.contentTypeId() ) ) count++;
133                 }
134                 
135                 Exporter[] result = new Exporter[ count ];
136                 int i = 0;
137                 for ( Exporter exporter : exporters ) {
138                         if ( contentTypeId.equals( exporter.contentTypeId() ) ) {
139                                 result[ i++ ] = exporter;
140                         }
141                 }
142                 
143                 Arrays.sort(result, exporterPrioritySorter);
144                 
145                 return result;
146         }
147
148         public Exporter[] getExportersForFormat(String formatId) 
149         {
150                 int count = 0;
151                 for ( Exporter exporter : exporters ) {
152                         if ( formatId.equals( exporter.formatId() ) ) count++;
153                 }
154                 
155                 Exporter[] result = new Exporter[ count ];
156                 int i = 0;
157                 for ( Exporter exporter : exporters ) {
158                         if ( formatId.equals( exporter.formatId() ) ) {
159                                 result[ i++ ] = exporter;
160                         }
161                 }
162                 
163                 Arrays.sort(result, exporterPrioritySorter);
164                 
165                 return result;
166         }
167         
168         public Publisher getPublisher(String publisherId) {
169                 for ( Publisher publisher : publishers ) {
170                         if ( publisherId.equals( publisher.id() ) ) return publisher;
171                 }
172                 return null;
173         };
174         
175         public int getPublisherIndex(String publisherId) {
176                 for (int i=0; i<publishers.length; i++) {
177                         if ( publisherId.equals( publishers[i].id() ) ) return i;
178                 }
179                 return -1;
180         }
181         
182         public Publisher getPublisherByLabel(String publisherLabel) {
183                 for ( Publisher publisher : publishers ) {
184                         if ( publisher.label().equals(publisherLabel ) ) return publisher;
185                 }
186                 return null;
187         }
188         
189         ContentType[] createContentTypes() {
190                 IConfigurationElement ces[] = Platform.getExtensionRegistry().getConfigurationElementsFor( EP ); 
191                 int count = 0;
192                 for (IConfigurationElement ce : ces) {
193                         if ( "content_type".equals(ce.getName() ) ) count++;                    
194                 }
195                 
196                 ContentType[] result = new ContentType[count];
197                 int i=0;
198                 for (IConfigurationElement ce : ces) {
199                         if ( "content_type".equals(ce.getName() ) ) {
200                                 result[i++] = new ContentTypeImpl( ce );
201                         }
202                 }
203                 
204                 return result;
205         }
206
207         Discoverer[] createDiscoverers() {
208                 IConfigurationElement ces[] = Platform.getExtensionRegistry().getConfigurationElementsFor( EP ); 
209                 int count = 0;
210                 for (IConfigurationElement ce : ces) {
211                         if ( "discoverer".equals(ce.getName() ) ) count++;                      
212                 }
213                 
214                 Discoverer[] result = new Discoverer[count];
215                 int i=0;
216                 for (IConfigurationElement ce : ces) {
217                         if ( "discoverer".equals(ce.getName() ) ) {
218                                 result[i++] = new DiscovererImpl( ce );
219                         }
220                 }
221                 
222                 return result;
223         }
224                 
225         Format[] createFormats() {
226                 IConfigurationElement ces[] = Platform.getExtensionRegistry().getConfigurationElementsFor( EP ); 
227                 int count = 0;
228                 for (IConfigurationElement ce : ces) {
229                         if ( "format".equals(ce.getName() ) ) count++;                  
230                 }
231                 
232                 Format[] result = new Format[count];
233                 int i=0;
234                 for (IConfigurationElement ce : ces) {
235                         if ( "format".equals(ce.getName() ) ) {
236                                 result[i++] = new FormatImpl( ce );
237                         }
238                 }
239                 
240                 return result;
241         }
242         
243         Exporter[] createExporters() {
244                 IConfigurationElement ces[] = Platform.getExtensionRegistry().getConfigurationElementsFor( EP ); 
245                 int count = 0;
246                 for (IConfigurationElement ce : ces) {
247                         if ( "exporter".equals(ce.getName() ) ) count++;                        
248                 }
249                 
250                 Exporter[] result = new Exporter[count];
251                 int i=0;
252                 for (IConfigurationElement ce : ces) {
253                         if ( "exporter".equals(ce.getName() ) ) {
254                                 result[i++] = new ExporterImpl( ce );
255                         }
256                 }
257                 
258                 return result;
259         }
260         
261         Importer[] createImporters() {
262                 IConfigurationElement ces[] = Platform.getExtensionRegistry().getConfigurationElementsFor( EP ); 
263                 int count = 0;
264                 for (IConfigurationElement ce : ces) {
265                         if ( "importer".equals(ce.getName() ) ) count++;                        
266                 }
267                 
268                 Importer[] result = new Importer[count];
269                 int i=0;
270                 for (IConfigurationElement ce : ces) {
271                         if ( "importer".equals(ce.getName() ) ) {
272                                 result[i++] = new ImporterImpl( ce );
273                         }
274                 }
275                 
276                 return result;
277         }
278         
279         Publisher[] createPublishers() {
280                 IConfigurationElement ces[] = Platform.getExtensionRegistry().getConfigurationElementsFor( EP ); 
281                 int count = 0;
282                 for (IConfigurationElement ce : ces) {
283                         if ( "publisher".equals(ce.getName() ) ) count++;                       
284                 }
285                 
286                 Publisher[] result = new Publisher[count];
287                 int i=0;
288                 for (IConfigurationElement ce : ces) {
289                         if ( "publisher".equals(ce.getName() ) ) {
290                                 result[i++] = new PublisherImpl( ce );
291                         }
292                 }
293                 
294                 Arrays.sort(result, new ToStringComparator.ByLength());
295                 
296                 return result;
297         }
298         
299         static class ContentTypeImpl implements ContentType {
300                 IConfigurationElement ce;
301                 ContentTypeAction action;
302                 WeakReference<IconResolver> resolverCache = null;
303                 boolean invalidIconResolver = false;
304
305                 public ContentTypeImpl(IConfigurationElement ce) {
306                         this.ce = ce;
307                 }
308                 
309                 ContentTypeAction getOrCreate() throws ExportException {
310                         if ( action == null ) {
311                                 try {
312                                         String value = ce.getAttribute("contentTypeAction");
313                                         if ( value == null ) return new DefaultContentTypeAction();
314                                         action = (ContentTypeAction) ce.createExecutableExtension("contentTypeAction");
315                                 } catch (CoreException e) {
316                                         throw new ExportException( e );
317                                 } 
318                         }
319                         return action;
320                 }               
321
322                 @Override
323                 public String id() {                    
324                         return ce.getAttribute("id");
325                 }
326
327                 @Override
328                 public String label() {
329                         return ce.getAttribute("label");
330                 }
331
332                 @Override
333                 public String plural() {
334                         return ce.getAttribute("plural");
335                 }
336
337                 public ImageDescriptor icon(String contentUri) {
338                         if (!invalidIconResolver && ce.getAttribute("iconResolver") != null) {
339                                 try {
340                                         IconResolver resolver = resolverCache != null ? resolverCache.get() : null;
341                                         if (resolver == null) {
342                                                 resolver = (IconResolver) ce.createExecutableExtension("iconResolver");
343                                                 resolverCache = new WeakReference<IconResolver>(resolver);
344                                         }
345                                         return resolver.get(contentUri);
346                                 } catch (CoreException e) {
347                                         // Invalid iconResolver, could not instantiate.
348                                         // Don't try to resolve it anymore if it fails once.
349                                         invalidIconResolver = true;
350                                         return null;
351                                 }
352                         }
353                         return icon();
354                 }
355                 
356                 @Override
357                 public ImageDescriptor icon() {
358                         try {
359                                 String _url = ce.getAttribute("icon");
360                                 URL url = new URL(_url);
361                                 return ImageDescriptor.createFromURL(url);
362                         } catch (MalformedURLException e) {
363                                 return null;
364                         }
365                 }
366
367                 @Override
368                 public boolean isModel() {
369                         return "true".equalsIgnoreCase(ce.getAttribute("model"));
370                 }
371                 
372                 @Override
373                 public String toString() {
374                         return "ContentType, id="+id()+", label="+label();
375                 }
376
377                 @Override
378                 public Map<String, String> getLabels(ExportContext ctx, Collection<String> contents) throws ExportException {
379                         ContentTypeAction action = getOrCreate();
380                         return action.getLabels(ctx, contents);
381                 }
382                 
383         }
384         
385         static class DiscovererImpl implements Discoverer {
386                 IConfigurationElement ce;
387                 DiscoverAction action;
388
389                 public DiscovererImpl(IConfigurationElement ce) {
390                         this.ce = ce;
391                 }
392
393                 @Override
394                 public String contentTypeId() {
395                         return ce.getAttribute("content_type_id");
396                 }
397                 
398                 DiscoverAction getOrCreate() throws ExportException {
399                         if ( action == null ) {
400                                 try {
401                                         action = (DiscoverAction) ce.createExecutableExtension("discoverAction");
402                                 } catch (CoreException e) {
403                                         throw new ExportException( e );
404                                 } 
405                         }
406                         return action;
407                 }
408
409 //              public Read<Collection<String>> discoverRequest(
410 //                              Collection<String> startLocations) throws ExportException {
411 //                      return getOrCreate().discoverRequest(startLocations);
412 //              }
413                 
414                 @Override
415                 public String toString() {
416                         try {
417                                 return "Discoverer, contentTypeId="+contentTypeId()+", class="+getOrCreate().getClass().getCanonicalName();
418                         } catch (ExportException e) {
419                                 return "Discoverer, contentTypeId="+contentTypeId();
420                         }
421                 }
422
423                 @Override
424                 public Collection<String> discoverContent(ExportContext ctx, Collection<String> startLocations) throws ExportException {
425                         return getOrCreate().discoverContent(ctx, startLocations);
426                 }
427                 
428         }
429
430         static class ExporterImpl implements Exporter {
431                 IConfigurationElement ce;
432                 ExportClass action;
433                 int priority = 0;
434
435                 public ExporterImpl(IConfigurationElement ce) {
436                         this.ce = ce;
437                         String priorityStr = ce.getAttribute("exportPriority");
438                         if ( priorityStr!=null ) priority = Integer.valueOf(priorityStr);
439                 }
440
441                 ExportClass getOrCreate() throws ExportException {
442                         if ( action == null ) {
443                                 try {
444                                         action = (ExportClass) ce.createExecutableExtension("exportAction");
445                                 } catch (CoreException e) {
446                                         throw new ExportException( e );
447                                 } 
448                         }
449                         return action;
450                 }
451                 
452                 @Override
453                 public String formatId() {                      
454                         return ce.getAttribute("formatId");
455                 }
456                 
457                 @Override
458                 public String contentTypeId() {
459                         return ce.getAttribute("content_type_id");
460                 }
461
462                 @Override
463                 public ExportClass exportAction() throws ExportException {
464                         return getOrCreate();
465                 }
466                 
467                 @Override
468                 public String toString() {
469                         try {
470                                 return "Exporter, formatId="+formatId()+", contentTypeId="+contentTypeId()+", class="+getOrCreate().getClass().getCanonicalName();
471                         } catch (ExportException e) {
472                                 return "Exporter, formatId="+formatId()+", contentTypeId="+contentTypeId();
473                         }
474                 }
475
476                 @Override
477                 public int getExportPriority() {
478                         return priority;
479                 }
480                 
481         }
482
483         static class ImporterImpl implements Importer {
484                 IConfigurationElement ce;
485
486                 public ImporterImpl(IConfigurationElement ce) {
487                         this.ce = ce;
488                 }
489
490                 @Override
491                 public String formatId() {                      
492                         return ce.getAttribute("formatId");
493                 }
494                 
495                 @Override
496                 public String contentTypeId() {
497                         return ce.getAttribute("content_type_id");
498                 }
499
500                 @Override
501                 public ImportAction importAction() throws ExportException {
502                         try {
503                                 return (ImportAction) ce.createExecutableExtension("exportAction");
504                         } catch (CoreException e) {
505                                 throw new ExportException(e);
506                         }
507                 }
508                 
509                 @Override
510                 public String toString() {
511                         return "Importer, formatId="+formatId()+", contentTypeId="+contentTypeId();
512                 }
513                 
514         }
515         
516         
517         static class FormatImpl implements Format {
518                 IConfigurationElement ce;
519                 FormatClass formatClass;
520
521                 public FormatImpl(IConfigurationElement ce) {
522                         this.ce = ce;
523                 }
524                 
525                 FormatClass getOrCreateFormatClass() throws ExportException {
526                         if ( formatClass == null ) {
527                                 try {
528                                         formatClass = (FormatClass) ce.createExecutableExtension("formatClass");
529                                 } catch (CoreException e) {
530                                         e.printStackTrace();                            
531                                         throw new ExportException( e );
532                                 } 
533                         }
534                         return formatClass;
535                 }
536
537                 @Override
538                 public String id() {                    
539                         return ce.getAttribute("id");
540                 }
541
542                 @Override
543                 public String label() {
544                         return ce.getAttribute("label");
545                 }
546
547                 @Override
548                 public String plural() {
549                         return ce.getAttribute("plural");
550                 }
551
552                 @Override
553                 public ImageDescriptor icon() {
554                         try {
555                                 String _url = ce.getAttribute("icon");
556                                 URL url = new URL(_url);
557                                 return ImageDescriptor.createFromURL(url);
558                         } catch (MalformedURLException e) {
559                                 return null;
560                         }
561                 }
562
563                 @Override
564                 public String fileext() {
565                         return ce.getAttribute("fileext");              
566                 }
567
568                 @Override
569                 public Class<?> writerClass() throws ClassNotFoundException {
570                         String className = ce.getAttribute("writerClass");
571                         return Class.forName(className);
572                 }
573
574                 @Override
575                 public Class<?> readerClass() throws ClassNotFoundException {
576                         String className = ce.getAttribute("readerClass");
577                         return Class.forName(className);
578                 }
579
580                 @Override
581                 public boolean isGroupFormat() {                        
582                         return "true".equals(ce.getAttribute("isGroupFormat"));
583                 }
584                 
585                 @Override
586                 public boolean isContainerFormat() {
587                         return "true".equals(ce.getAttribute("isContainerFormat"));
588                 }
589                 
590                 @Override
591                 public boolean isAttachable() {
592                         return "true".equals(ce.getAttribute("isAttachable"));
593                 }               
594
595                 @Override
596                 public boolean mergeGroupFormatDefault() {
597                         return "true".equals(ce.getAttribute("mergeGroupDefault"));             
598                 }
599
600                 @Override
601                 public boolean isAlwaysPublished() {
602                         return "true".equals(ce.getAttribute("isAlwaysPublished"));
603                 }
604
605                 @Override
606                 public boolean isLinkContainer() {
607                         return "true".equals(ce.getAttribute("isLinkContainer"));
608                 }       
609                 @Override
610                 public RecordType options( ExportContext context ) throws ExportException {
611                         return getOrCreateFormatClass().options(context);
612                 }
613
614                 @Override
615                 public List<String> validate(ExportContext context, Variant options) throws ExportException {
616                         return getOrCreateFormatClass().validate(context, options);
617                 }
618                 
619                 @Override
620                 public void fillDefaultPrefs( ExportContext ctx, Variant options ) throws ExportException {
621                         getOrCreateFormatClass().fillDefaultPrefs( ctx, options );
622                 }
623
624
625                 @Override
626                 public String toString() {                      
627                         try {
628                                 return "Format, id="+id()+", fileext="+fileext()+", label="+label()+", class="+getOrCreateFormatClass().getClass().getCanonicalName();
629                         } catch (ExportException e) {
630                                 return "Format, id="+id()+", fileext="+fileext()+", label="+label();
631                         }                       
632                 }
633
634                 @Override
635                 public Object createFile(ExportContext context, File outputFile, Variant options) throws ExportException {
636                         return getOrCreateFormatClass().createFile(context, outputFile, options);
637                 }
638
639                 @Override
640                 public Object openFile(ExportContext context, File inputFile, Variant options) throws ExportException {
641                         return getOrCreateFormatClass().openFile(context, inputFile, options);
642                 }
643                 
644                 @Override
645                 public void closeFile(ExportContext context, Object handle) throws ExportException {
646                         getOrCreateFormatClass().closeFile(context, handle);
647                 }
648
649                 @Override
650                 public void addAttachment(ExportContext context, Object handle, List<Content> attachments) throws ExportException {
651                         getOrCreateFormatClass().addAttachment(context, handle, attachments);
652                 }
653                 
654                 @Override
655                 public FormatClass formatActions() throws ExportException {
656                         return getOrCreateFormatClass();
657                 }
658
659                 @Override
660                 public void savePref(Variant options, Preferences contentScopeNode, Preferences workbenchScopeNode) throws ExportException {
661                         getOrCreateFormatClass().savePref(options, contentScopeNode, workbenchScopeNode);
662                 }
663
664                 @Override
665                 public void loadPref(Variant options, Preferences contentScopeNode, Preferences workbenchScopeNode) throws ExportException {
666                         getOrCreateFormatClass().loadPref(options, contentScopeNode, workbenchScopeNode);                       
667                 }
668
669         }
670         
671         public static class PublisherImpl implements Publisher {
672
673                 IConfigurationElement ce;
674                 PublisherClass publisherClass;
675
676                 public PublisherImpl(IConfigurationElement ce) {
677                         this.ce = ce;
678                 }
679                 
680                 PublisherClass getOrCreatePublisherClass() throws ExportException {
681                         if ( publisherClass == null ) {
682                                 try {
683                                         publisherClass = (PublisherClass) ce.createExecutableExtension("publisherClass");
684                                 } catch (CoreException e) {
685                                         e.printStackTrace();                            
686                                         throw new ExportException( e );
687                                 } 
688                         }
689                         return publisherClass;
690                 }
691                 
692                 @Override
693                 public String id() {
694                         return ce.getAttribute("id");
695                 }
696
697                 @Override
698                 public String label() {
699                         return ce.getAttribute("label");
700                 }
701
702                 @Override
703                 public org.simantics.export.core.intf.PublisherClass publisherClass() throws ExportException {
704                         return getOrCreatePublisherClass();
705                 }
706                 
707                 @Override
708                 public String toString() {
709                         return label();
710                 }
711                 
712         }
713
714         @Override
715         public ContentType[] contentTypes() {
716                 return contentTypes;
717         }
718
719         @Override
720         public Discoverer[] discoverers() {
721                 return discoverers;
722         }
723
724         @Override
725         public Format[] formats() {
726                 return formats;
727         }
728
729         @Override
730         public Importer[] importers() {
731                 return importers;
732         }
733
734         @Override
735         public Exporter[] exporters() {
736                 return exporters;
737         }
738         
739         public Publisher[] publishers() {
740                 return publishers;
741         };
742         
743         Comparator<Exporter> exporterPrioritySorter = new Comparator<Exporter>() {
744                 @Override
745                 public int compare(Exporter o1, Exporter o2) {                  
746                         return Integer.signum( o2.getExportPriority() - o1.getExportPriority() );
747                 }
748         };
749
750         
751 }