Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 12554078477ba780f797b10117d2dabc81964385 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/*
 * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.emf.cdo.releng.doc.article.impl;

import org.eclipse.emf.cdo.releng.doc.article.ArticlePackage;
import org.eclipse.emf.cdo.releng.doc.article.BodyElementContainer;
import org.eclipse.emf.cdo.releng.doc.article.Callout;
import org.eclipse.emf.cdo.releng.doc.article.Documentation;
import org.eclipse.emf.cdo.releng.doc.article.Embedding;
import org.eclipse.emf.cdo.releng.doc.article.Formatter;
import org.eclipse.emf.cdo.releng.doc.article.Snippet;
import org.eclipse.emf.cdo.releng.doc.article.StructuralElement;
import org.eclipse.emf.cdo.releng.doc.article.XmlFormatter;
import org.eclipse.emf.cdo.releng.doc.article.util.ArticleException;
import org.eclipse.emf.cdo.releng.doc.article.util.ArticleUtil;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.util.EObjectContainmentWithInverseEList;
import org.eclipse.emf.ecore.util.InternalEList;

import com.sun.javadoc.Doc;
import com.sun.javadoc.SeeTag;
import com.sun.javadoc.Tag;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.regex.Pattern;

/**
 * <!-- begin-user-doc --> An implementation of the model object '<em><b>Snippet</b></em>'. <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 * <li>{@link org.eclipse.emf.cdo.releng.doc.article.impl.SnippetImpl#getCallouts <em>Callouts</em>}</li>
 * <li>{@link org.eclipse.emf.cdo.releng.doc.article.impl.SnippetImpl#getFormatter <em>Formatter</em>}</li>
 * </ul>
 * </p>
 * 
 * @generated
 */
public class SnippetImpl extends EmbeddableElementImpl implements Snippet
{
  private static final Pattern PATTERN = Pattern.compile("<[^>]+?>", Pattern.MULTILINE | Pattern.DOTALL);

  /**
   * The cached value of the '{@link #getCallouts() <em>Callouts</em>}' containment reference list. <!-- begin-user-doc
   * --> <!-- end-user-doc -->
   * 
   * @see #getCallouts()
   * @generated
   * @ordered
   */
  protected EList<Callout> callouts;

  /**
   * The cached value of the '{@link #getFormatter() <em>Formatter</em>}' containment reference. <!-- begin-user-doc -->
   * <!-- end-user-doc -->
   * 
   * @see #getFormatter()
   * @generated
   * @ordered
   */
  protected Formatter formatter;

  /**
   * <!-- begin-user-doc --> <!-- end-user-doc -->
   * 
   * @generated
   */
  protected SnippetImpl()
  {
    super();
  }

  SnippetImpl(Documentation documentation, Doc doc)
  {
    super(documentation, doc);
    initFormatter(doc);
    initCallouts(doc);
  }

  private void initFormatter(Doc doc)
  {
    Tag[] tags = doc.tags("@snippet");
    if (tags.length > 1)
    {
      throw new ArticleException("More than one format not allowed: " + ArticleUtil.makeConsoleLink(doc));
    }

    if (tags.length == 1)
    {
      String text = tags[0].text();
      if (text.length() != 0)
      {
        String format;
        String args;

        int pos = text.indexOf(' ');
        if (pos != -1)
        {
          format = text.substring(0, pos).trim().toLowerCase();
          args = text.substring(pos + 1).trim();
        }
        else
        {
          format = text;
          args = "";
        }

        format = format.trim().toLowerCase();
        args = args.trim();

        if (format.equals("xml"))
        {
          try
          {
            File folder = doc.position().file().getParentFile();
            File file = new File(folder, args).getCanonicalFile();

            XmlFormatter formatter = new XmlFormatterImpl();
            formatter.setFile(file);

            setFormatter(formatter);
          }
          catch (IOException ex)
          {
            ex.printStackTrace();
          }
        }
      }
    }

    if (getFormatter() == null)
    {
      setFormatter(new JavaFormatterImpl());
    }
  }

  private void initCallouts(Doc doc)
  {
    int index = 0;
    for (Tag tag : doc.tags("@callout"))
    {
      new CalloutImpl(this, tag, ++index);
    }
  }

  /**
   * <!-- begin-user-doc --> <!-- end-user-doc -->
   * 
   * @generated
   */
  @Override
  protected EClass eStaticClass()
  {
    return ArticlePackage.Literals.SNIPPET;
  }

  /**
   * <!-- begin-user-doc --> <!-- end-user-doc -->
   * 
   * @generated
   */
  public EList<Callout> getCallouts()
  {
    if (callouts == null)
    {
      callouts = new EObjectContainmentWithInverseEList<Callout>(Callout.class, this, ArticlePackage.SNIPPET__CALLOUTS,
          ArticlePackage.CALLOUT__SNIPPET);
    }
    return callouts;
  }

  /**
   * <!-- begin-user-doc --> <!-- end-user-doc -->
   * 
   * @generated
   */
  public Formatter getFormatter()
  {
    return formatter;
  }

  /**
   * <!-- begin-user-doc --> <!-- end-user-doc -->
   * 
   * @generated
   */
  public NotificationChain basicSetFormatter(Formatter newFormatter, NotificationChain msgs)
  {
    Formatter oldFormatter = formatter;
    formatter = newFormatter;
    if (eNotificationRequired())
    {
      ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, ArticlePackage.SNIPPET__FORMATTER,
          oldFormatter, newFormatter);
      if (msgs == null)
      {
        msgs = notification;
      }
      else
      {
        msgs.add(notification);
      }
    }
    return msgs;
  }

  /**
   * <!-- begin-user-doc --> <!-- end-user-doc -->
   * 
   * @generated
   */
  public void setFormatter(Formatter newFormatter)
  {
    if (newFormatter != formatter)
    {
      NotificationChain msgs = null;
      if (formatter != null)
      {
        msgs = ((InternalEObject)formatter).eInverseRemove(this, ArticlePackage.FORMATTER__SNIPPET, Formatter.class,
            msgs);
      }
      if (newFormatter != null)
      {
        msgs = ((InternalEObject)newFormatter).eInverseAdd(this, ArticlePackage.FORMATTER__SNIPPET, Formatter.class,
            msgs);
      }
      msgs = basicSetFormatter(newFormatter, msgs);
      if (msgs != null)
      {
        msgs.dispatch();
      }
    }
    else if (eNotificationRequired())
    {
      eNotify(new ENotificationImpl(this, Notification.SET, ArticlePackage.SNIPPET__FORMATTER, newFormatter,
          newFormatter));
    }
  }

  /**
   * <!-- begin-user-doc --> <!-- end-user-doc -->
   * 
   * @generated
   */
  @SuppressWarnings("unchecked")
  @Override
  public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, NotificationChain msgs)
  {
    switch (featureID)
    {
    case ArticlePackage.SNIPPET__CALLOUTS:
      return ((InternalEList<InternalEObject>)(InternalEList<?>)getCallouts()).basicAdd(otherEnd, msgs);
    case ArticlePackage.SNIPPET__FORMATTER:
      if (formatter != null)
      {
        msgs = ((InternalEObject)formatter).eInverseRemove(this, EOPPOSITE_FEATURE_BASE
            - ArticlePackage.SNIPPET__FORMATTER, null, msgs);
      }
      return basicSetFormatter((Formatter)otherEnd, msgs);
    }
    return super.eInverseAdd(otherEnd, featureID, msgs);
  }

  /**
   * <!-- begin-user-doc --> <!-- end-user-doc -->
   * 
   * @generated
   */
  @Override
  public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs)
  {
    switch (featureID)
    {
    case ArticlePackage.SNIPPET__CALLOUTS:
      return ((InternalEList<?>)getCallouts()).basicRemove(otherEnd, msgs);
    case ArticlePackage.SNIPPET__FORMATTER:
      return basicSetFormatter(null, msgs);
    }
    return super.eInverseRemove(otherEnd, featureID, msgs);
  }

  /**
   * <!-- begin-user-doc --> <!-- end-user-doc -->
   * 
   * @generated
   */
  @Override
  public Object eGet(int featureID, boolean resolve, boolean coreType)
  {
    switch (featureID)
    {
    case ArticlePackage.SNIPPET__CALLOUTS:
      return getCallouts();
    case ArticlePackage.SNIPPET__FORMATTER:
      return getFormatter();
    }
    return super.eGet(featureID, resolve, coreType);
  }

  /**
   * <!-- begin-user-doc --> <!-- end-user-doc -->
   * 
   * @generated
   */
  @SuppressWarnings("unchecked")
  @Override
  public void eSet(int featureID, Object newValue)
  {
    switch (featureID)
    {
    case ArticlePackage.SNIPPET__CALLOUTS:
      getCallouts().clear();
      getCallouts().addAll((Collection<? extends Callout>)newValue);
      return;
    case ArticlePackage.SNIPPET__FORMATTER:
      setFormatter((Formatter)newValue);
      return;
    }
    super.eSet(featureID, newValue);
  }

  /**
   * <!-- begin-user-doc --> <!-- end-user-doc -->
   * 
   * @generated
   */
  @Override
  public void eUnset(int featureID)
  {
    switch (featureID)
    {
    case ArticlePackage.SNIPPET__CALLOUTS:
      getCallouts().clear();
      return;
    case ArticlePackage.SNIPPET__FORMATTER:
      setFormatter((Formatter)null);
      return;
    }
    super.eUnset(featureID);
  }

  /**
   * <!-- begin-user-doc --> <!-- end-user-doc -->
   * 
   * @generated
   */
  @Override
  public boolean eIsSet(int featureID)
  {
    switch (featureID)
    {
    case ArticlePackage.SNIPPET__CALLOUTS:
      return callouts != null && !callouts.isEmpty();
    case ArticlePackage.SNIPPET__FORMATTER:
      return formatter != null;
    }
    return super.eIsSet(featureID);
  }

  public void generate(PrintWriter out, Embedding embedder) throws IOException
  {
    Formatter formatter = getFormatter();

    String id = getDoc().name();
    int lastDot = id.lastIndexOf('.');
    if (lastDot != -1)
    {
      id = id.substring(lastDot + 1);
    }

    StructuralElement structuralElement = getStructuralElement(embedder);
    String imagePath = structuralElement.getImagePath() + "/";

    SeeTag embedderTag = (SeeTag)embedder.getTag();
    String title = embedderTag.label();
    if (title == null || title.length() == 0)
    {
      title = formatter.getDefaultTitle(embedderTag);
    }

    out.write("\n\n");

    String html = formatter.getSnippetHtml(out, id, title);
    html = processCallouts(id, html, imagePath);

    out.write("<div class=\"snippet\" style=\"margin-left:24px;\" align=\"left\">\n");
    out.write("  <a name=\"snippet_" + id + "\"></a>\n");
    out.write("  <table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n");
    out.write("    <tr>\n");
    out.write("      <td><img src=\"" + formatter.getTopLeftEditorIcon(imagePath) + "\"></td>\n");
    out.write("      <td style=\"background-image:url(" + imagePath
        + "editor-top1.png); background-repeat:repeat-x;\" width=\"1px\"><font face=\"Segoe UI,Arial\" size=\"-1\">"
        + title + "</font></td>\n");
    out.write("      <td width=\"1px\"><img src=\"" + imagePath + "editor-close.png\"></td>\n");
    out.write("      <td style=\"background-image:url(" + imagePath
        + "editor-top2.png); background-repeat:repeat-x;\">&nbsp;</td>\n");
    out.write("      <td><img src=\"" + imagePath + "editor-top-right.png\"></td>\n");
    out.write("    </tr>\n");
    out.write("    <tr>\n");
    out.write("      <td style=\"background-image:url(" + imagePath
        + "editor-left.png); background-repeat:repeat-y;\">&nbsp;</td>\n");
    out.write("      <td colspan=\"3\" align=\"left\" valign=\"top\" nowrap>\n");
    out.write("        <div style=\"margin:10px 0px 10px 0px;\">\n");
    out.write("          <code>\n");

    out.write(html);
    out.write("\n");

    out.write("          </code>\n");
    out.write("        </div>\n");
    out.write("      </td>\n");
    out.write("      <td style=\"background-image:url(" + imagePath
        + "editor-right.png); background-repeat:repeat-y;\">&nbsp;</td>\n");
    out.write("    </tr>\n");
    out.write("    <tr>\n");
    out.write("      <td><img src=\"" + imagePath + "editor-bottom-left.png\"></td>\n");
    out.write("      <td style=\"background-image:url(" + imagePath
        + "editor-bottom.png); background-repeat:repeat-x;\" colspan=\"3\">&nbsp;</td>\n");
    out.write("      <td><img src=\"" + imagePath + "editor-bottom-right.png\"></td>\n");
    out.write("    </tr>\n");
    out.write("  </table>\n");
    out.write("</div>\n");

    EList<Callout> callouts = getCallouts();
    if (!callouts.isEmpty())
    {
      out.write("<p>\n");
      for (Callout callout : callouts)
      {
        String image = getCalloutImage(id, callout.getIndex(), false, "Jump to snippet...", imagePath);
        out.write("<div style=\"margin-left:24px;\">" + image + "&nbsp;");

        try
        {
          BodyElementContainerImpl.generate(out, structuralElement, callout.getElements());
        }
        catch (IOException ex)
        {
          ex.printStackTrace();
        }

        out.write("</div>\n");
      }
    }

    out.write("<p>\n");
  }

  private StructuralElement getStructuralElement(Embedding embedder)
  {
    BodyElementContainer container = embedder.getContainer();
    if (container instanceof StructuralElement)
    {
      return (StructuralElement)container;
    }

    throw new ArticleException(ArticleUtil.makeConsoleLink("Nested embedding in ", embedder.getTag().position()));
  }

  private String processCallouts(String id, String html, String imagePath)
  {
    EList<Callout> callouts = getCallouts();
    int size = callouts.size();
    int callout = 0;

    String calloutMarker = getFormatter().getCalloutMarker();

    for (;;)
    {
      int pos = html.indexOf(calloutMarker);
      if (pos == -1)
      {
        break;
      }

      ++callout;

      String start = html.substring(0, pos);
      String rest = html.substring(pos + calloutMarker.length());
      if (!rest.startsWith("&nbsp;"))
      {
        rest = "&nbsp;" + rest;
      }

      String alt = "";
      int index = callout - 1;
      if (index < size)
      {
        Tag tag = callouts.get(index).getTag();
        for (Tag inlineTag : tag.inlineTags())
        {
          alt += inlineTag.text();
        }
      }

      String image = getCalloutImage(id, callout, true, alt, imagePath);
      html = start + image + rest;
    }

    if (callout != size)
    {
      if (callout < size)
      {
        Tag tag = callouts.get(size).getTag();
        String link = ArticleUtil.makeConsoleLink(tag.holder(), tag.position());
        System.err.println("Too many callout descriptions: " + link);
      }
      else
      {
        String link = ArticleUtil.makeConsoleLink(getDoc(), getDoc().position());
        System.err.println("Callout descriptions missing: " + link);
      }
    }

    return html;
  }

  private String getCalloutImage(String prefix, int number, boolean code, String alt, String imagePath)
  {
    String name = "callout_" + prefix + "_" + number;
    String nameSuffix = code ? "_code" : "";
    String hrefSuffix = code ? "" : "_code";

    String image = "<img src=\"" + imagePath + "callout-" + number
        + ".png\" width=\"16\" height=\"16\" border=\"0\" align=\"top\">";

    alt = PATTERN.matcher(alt).replaceAll("");
    alt = alt.replaceAll("\"", "&quot;");

    return "<a name=\"" + name + nameSuffix + "\" href=\"#" + name + hrefSuffix + "\" alt=\"" + alt + "\" title=\""
        + alt + "\">" + image + "</a>";
  }

} // SnippetImpl

Back to the top