-
Notifications
You must be signed in to change notification settings - Fork 14
/
PlayerAction.java
81 lines (69 loc) · 2.67 KB
/
PlayerAction.java
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
/*
* SPDX-License-Identifier: CC0-1.0
*
* Copyright 2018-2019 Will Sargent.
*
* Licensed under the CC0 Public Domain Dedication;
* You may obtain a copy of the License at
*
* http://creativecommons.org/publicdomain/zero/1.0/
*/
package com.tersesystems.logback.audio;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.joran.action.Action;
import ch.qos.logback.core.joran.spi.ActionException;
import ch.qos.logback.core.joran.spi.InterpretationContext;
import ch.qos.logback.core.spi.ContextAwareBase;
import ch.qos.logback.core.spi.LifeCycle;
import ch.qos.logback.core.util.OptionHelper;
import org.xml.sax.Attributes;
public class PlayerAction extends Action {
Player player;
private boolean inError = false;
@Override
public void begin(InterpretationContext ic, String localName, Attributes attributes) throws ActionException {
Object o = ic.peekObject();
if (!(o instanceof PlayerAttachable)) {
String errMsg = "Could not find an CensorAttachable at the top of execution stack. Near [" + localName + "] line " + getLineNumber(ic);
inError = true;
addInfo(errMsg); // This can trigger in an "if" block from janino, so it may not be serious...
return;
}
PlayerAttachable playerAttachable = (PlayerAttachable) o;
String className = attributes.getValue(CLASS_ATTRIBUTE);
if (OptionHelper.isEmpty(className)) {
addError("Missing class name for player. Near [" + localName + "] line " + getLineNumber(ic));
inError = true;
return;
}
try {
addInfo("About to instantiate player of type [" + className + "]");
player = (Player) OptionHelper.instantiateByClassName(className, Player.class, context);
Context icContext = ic.getContext();
if (player instanceof ContextAwareBase) {
((ContextAwareBase) player).setContext(icContext);
}
ic.pushObject(player);
} catch (Exception oops) {
inError = true;
addError("Could not create player.", oops);
throw new ActionException(oops);
}
playerAttachable.addPlayer(player);
}
@Override
public void end(InterpretationContext ic, String name) throws ActionException {
if (inError) {
return;
}
if (player instanceof LifeCycle) {
((LifeCycle) player).start();
}
Object o = ic.peekObject();
if (o != player) {
addWarn("The object at the end of the stack is not the player pushed earlier.");
} else {
ic.popObject();
}
}
}