|
| 1 | + |
| 2 | +package com.bandwidth.voice.bxml.verbs; |
| 3 | + |
| 4 | +import lombok.Builder; |
| 5 | + |
| 6 | +import java.io.ByteArrayOutputStream; |
| 7 | +import java.io.OutputStream; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.Arrays; |
| 10 | +import java.util.List; |
| 11 | +import java.util.regex.Matcher; |
| 12 | +import java.util.regex.Pattern; |
| 13 | +import javax.xml.bind.JAXBContext; |
| 14 | +import javax.xml.bind.JAXBException; |
| 15 | +import javax.xml.bind.Marshaller; |
| 16 | +import javax.xml.bind.annotation.XmlElement; |
| 17 | +import javax.xml.bind.annotation.XmlElements; |
| 18 | +import javax.xml.bind.annotation.XmlRootElement; |
| 19 | + |
| 20 | +@Builder |
| 21 | +@XmlRootElement(name = Bxml.TYPE_NAME) |
| 22 | +public class Bxml { |
| 23 | + public static final String TYPE_NAME = "Bxml"; |
| 24 | + |
| 25 | + private static JAXBContext jaxbContext; |
| 26 | + |
| 27 | + @XmlElements({ |
| 28 | + @XmlElement(name = Gather.TYPE_NAME, type = Gather.class), |
| 29 | + @XmlElement(name = Hangup.TYPE_NAME, type = Hangup.class), |
| 30 | + @XmlElement(name = Redirect.TYPE_NAME, type = Redirect.class), |
| 31 | + @XmlElement(name = PlayAudio.TYPE_NAME, type = PlayAudio.class), |
| 32 | + @XmlElement(name = SpeakSentence.TYPE_NAME, type = SpeakSentence.class), |
| 33 | + @XmlElement(name = Transfer.TYPE_NAME, type = Transfer.class), |
| 34 | + @XmlElement(name = Pause.TYPE_NAME, type = Pause.class), |
| 35 | + @XmlElement(name = Forward.TYPE_NAME, type = Forward.class), |
| 36 | + @XmlElement(name = SendDtmf.TYPE_NAME, type = SendDtmf.class), |
| 37 | + @XmlElement(name = Record.TYPE_NAME, type = Record.class), |
| 38 | + @XmlElement(name = StartRecording.TYPE_NAME, type = StartRecording.class), |
| 39 | + @XmlElement(name = StopRecording.TYPE_NAME, type = StopRecording.class), |
| 40 | + @XmlElement(name = PauseRecording.TYPE_NAME, type = PauseRecording.class), |
| 41 | + @XmlElement(name = ResumeRecording.TYPE_NAME, type = ResumeRecording.class), |
| 42 | + @XmlElement(name = Conference.TYPE_NAME, type = Conference.class), |
| 43 | + @XmlElement(name = Bridge.TYPE_NAME, type = Bridge.class), |
| 44 | + @XmlElement(name = Ring.TYPE_NAME, type = Ring.class), |
| 45 | + @XmlElement(name = StartGather.TYPE_NAME, type = StartGather.class), |
| 46 | + @XmlElement(name = StopGather.TYPE_NAME, type = StopGather.class), |
| 47 | + @XmlElement(name = Tag.TYPE_NAME, type = Tag.class), |
| 48 | + @XmlElement(name = SipUri.TYPE_NAME, type = SipUri.class) |
| 49 | + }) |
| 50 | + private final List<Verb> verbs = new ArrayList<>(); |
| 51 | + |
| 52 | + private static final Pattern XML_PATTERN = Pattern.compile("<([a-zA-Z//].*?)>"); |
| 53 | + |
| 54 | + private static final Pattern SPEAK_SENTENCE_PATTERN = Pattern.compile("<SpeakSentence.*?>(.*?)<\\/SpeakSentence>"); |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + public Bxml(){ |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + /** |
| 65 | + * Returns list of verbs in this Bxml |
| 66 | + */ |
| 67 | + public List<Verb> getVerbs() { |
| 68 | + return verbs; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Adds a Verb to the list of Verbs in this Bxml |
| 73 | + */ |
| 74 | + public Bxml add(Verb verb) { |
| 75 | + this.verbs.add(verb); |
| 76 | + return this; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Adds Verbs to the list of Verbs in this Bxml |
| 81 | + */ |
| 82 | + public Bxml addAll(Verb... verbs) { |
| 83 | + this.verbs.addAll(Arrays.asList(verbs)); |
| 84 | + return this; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Convert this Bxml and its verbs into their corresponding XML and get the result as a String |
| 89 | + * |
| 90 | + */ |
| 91 | + public String toBXML() { |
| 92 | + OutputStream outputStream = new ByteArrayOutputStream(); |
| 93 | + try { |
| 94 | + getMarshaller().marshal(this, outputStream); |
| 95 | + } catch (JAXBException e) { |
| 96 | + throw new RuntimeException("Error creating BXML marshaller", e); |
| 97 | + } |
| 98 | + |
| 99 | + Matcher matcherSpeakSentence = SPEAK_SENTENCE_PATTERN.matcher(outputStream.toString()); |
| 100 | + |
| 101 | + StringBuffer sb = new StringBuffer(); |
| 102 | + |
| 103 | + while(matcherSpeakSentence.find()){ |
| 104 | + Matcher matcher = XML_PATTERN.matcher(matcherSpeakSentence.group()); |
| 105 | + String replaced = matcher.replaceAll("<$1>").replaceAll("\\?", "\\?"); |
| 106 | + matcherSpeakSentence.appendReplacement(sb, replaced); |
| 107 | + } |
| 108 | + |
| 109 | + matcherSpeakSentence.appendTail(sb); |
| 110 | + |
| 111 | + return sb.toString(); |
| 112 | + |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | + private Marshaller getMarshaller() { |
| 117 | + try { |
| 118 | + // This isn't thread-unsafe, it's just expensive to create. So no locking necessary. |
| 119 | + if (jaxbContext == null) { |
| 120 | + jaxbContext = JAXBContext.newInstance(Bxml.class); |
| 121 | + } |
| 122 | + Marshaller marshaller = jaxbContext.createMarshaller(); |
| 123 | + marshaller.setProperty("com.sun.xml.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); |
| 124 | + marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); |
| 125 | + |
| 126 | + return marshaller; |
| 127 | + } catch (JAXBException e) { |
| 128 | + throw new RuntimeException("Error creating JAXB context for BXML serialization", e); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments