public static MESSAGE createFiletransfer( List<String> receivers, byte[] data, Map<String, String> parameters) { ObjectFactory objectFactory = new ObjectFactory(); MESSAGE request = objectFactory.createMESSAGE(); request.setParameters(objectFactory.createMESSAGEParameters()); request.setReceivers(objectFactory.createMESSAGEReceivers()); request.setType(REQUEST); request.setName(Request.FILETRANSFER); if (data != null) { request.setData(data); } if (receivers != null) { for (String receiver : receivers) { request.getReceivers().getReceiver().add(receiver); } } if (parameters != null) { Set<String> paramKeys = parameters.keySet(); for (String key : paramKeys) { MESSAGE.Parameters.Parameter msgParamter = objectFactory.createMESSAGEParametersParameter(); msgParamter.setName(key); msgParamter.setValue(parameters.get(key)); request.getParameters().getParameter().add(msgParamter); } } return request; }
public static MESSAGE createMessage( String name, List<String> receivers, Map<String, String> parameters) { ObjectFactory objectFactory = new ObjectFactory(); MESSAGE request = objectFactory.createMESSAGE(); request.setParameters(objectFactory.createMESSAGEParameters()); request.setReceivers(objectFactory.createMESSAGEReceivers()); request.setContents(objectFactory.createMESSAGEContents()); request.setType(MESSAGE); if (name != null) { request.setName(name); } if (receivers != null) { for (String receiver : receivers) { request.getReceivers().getReceiver().add(receiver); } } if (parameters != null) { Set<String> paramKeys = parameters.keySet(); for (String key : paramKeys) { MESSAGE.Parameters.Parameter msgParamter = objectFactory.createMESSAGEParametersParameter(); msgParamter.setName(key); msgParamter.setValue(parameters.get(key)); request.getParameters().getParameter().add(msgParamter); } } return request; }
public static String messageToStringFormatted(MESSAGE message) { String toReturn = ""; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(message.getClass().getPackage().getName()); javax.xml.bind.Marshaller marshaller = jaxbCtx.createMarshaller(); marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING, "UTF-8"); // NOI18N marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.marshal(message, baos); // marshaller.marshal(message, System.out); } catch (javax.xml.bind.JAXBException ex) { // XXXTODO Handle exception log.error(ex.getMessage()); // NOI18N } try { String xml = baos.toString("UTF-8"); toReturn = "\n" + xml; // log.debug("xml to send : " + toReturn); return toReturn; } catch (UnsupportedEncodingException ex) { log.error(ex); return null; } }
public static boolean directWriteMessage(MESSAGE message, OutputStream os) throws SocketException { try { javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(message.getClass().getPackage().getName()); javax.xml.bind.Marshaller marshaller = jaxbCtx.createMarshaller(); marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING, "UTF-8"); // NOI18N marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE); synchronized (os) { marshaller.marshal(message, os); try { os.write("\n".getBytes(DEFAULTENCODING)); } catch (IOException ex) { log.debug(ex); } } } catch (javax.xml.bind.JAXBException ex) { // XXXTODO Handle exception log.error(ex); // NOI18N throw new SocketException(); } return false; }
public static void addContentAt(MESSAGE message, String name, byte[] data, int position) { ObjectFactory objectFactory = new ObjectFactory(); MESSAGE.Contents.Content content = objectFactory.createMESSAGEContentsContent(); content.setName(name); content.setValue(data); message.getContents().getContent().add(position, content); }
public static void addParameterAt(MESSAGE message, String name, String parameter, int position) { ObjectFactory objectFactory = new ObjectFactory(); MESSAGE.Parameters.Parameter msgParamter = objectFactory.createMESSAGEParametersParameter(); msgParamter.setName(name); msgParamter.setValue(parameter); message.getParameters().getParameter().add(position, msgParamter); }
public static MESSAGE parseXML(String xml) { ObjectFactory objectFactory = new ObjectFactory(); MESSAGE toReturn = objectFactory.createMESSAGE(); try { javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(toReturn.getClass().getPackage().getName()); javax.xml.bind.Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller(); toReturn = (MESSAGE) unmarshaller.unmarshal(new StreamSource(new StringReader(xml))); // NOI18N return toReturn; } catch (javax.xml.bind.JAXBException ex) { // XXXTODO Handle exception log.error(ex); // NOI18N return null; } }
public static MESSAGE createCommand(String name, Map<String, String> parameters) { ObjectFactory objectFactory = new ObjectFactory(); MESSAGE request = objectFactory.createMESSAGE(); request.setParameters(objectFactory.createMESSAGEParameters()); request.setReceivers(objectFactory.createMESSAGEReceivers()); request.setType(COMMAND); if (name != null) { request.setName(name); } // if (receivers != null) { // for (String receiver : receivers) { // request.getReceivers().getReceiver().add(receiver); // } // } if (parameters != null) { Set<String> paramKeys = parameters.keySet(); for (String key : paramKeys) { MESSAGE.Parameters.Parameter msgParamter = objectFactory.createMESSAGEParametersParameter(); msgParamter.setName(key); msgParamter.setValue(parameters.get(key)); request.getParameters().getParameter().add(msgParamter); } } return request; }
@Override public void startElement( final String namespaceURI, final String lName, final String qName, final Attributes attrs) throws SAXException { if (qName.equals("category")) { final String catName = attrs.getValue("name"); final String priorityStr = attrs.getValue("priority"); // int prio = 0; if (priorityStr != null) { category = new Category(catName, Integer.parseInt(priorityStr)); } else { category = new Category(catName); } if ("off".equals(attrs.getValue(DEFAULT))) { category.setDefaultOff(); } } else if (qName.equals("rules")) { final String languageStr = attrs.getValue("targetLang"); language = Language.getLanguageForShortName(languageStr); if (language == null) { throw new SAXException("Unknown language '" + languageStr + "'"); } } else if (qName.equals("rule")) { id = attrs.getValue("id"); if (inRuleGroup) subId++; if (!(inRuleGroup && defaultOff)) { defaultOff = "off".equals(attrs.getValue(DEFAULT)); } if (!(inRuleGroup && defaultOn)) { defaultOn = "on".equals(attrs.getValue(DEFAULT)); } if (inRuleGroup && id == null) { id = ruleGroupId; } description = attrs.getValue("name"); if (inRuleGroup && description == null) { description = ruleGroupDescription; } correctExamples = new ArrayList<StringPair>(); incorrectExamples = new ArrayList<IncorrectBitextExample>(); if (suggestionMatches != null) { suggestionMatches.clear(); } } else if (PATTERN.equals(qName) || "target".equals(qName)) { startPattern(attrs); } else if (AND.equals(qName)) { inAndGroup = true; } else if (UNIFY.equals(qName)) { inUnification = true; uniNegation = YES.equals(attrs.getValue(NEGATE)); } else if (qName.equals("feature")) { uFeature = attrs.getValue("id"); } else if (qName.equals(TYPE)) { uType = attrs.getValue("id"); uTypeList.add(uType); } else if (qName.equals(TOKEN)) { setToken(attrs); } else if (qName.equals(EXCEPTION)) { setExceptions(attrs); } else if (qName.equals(EXAMPLE) && attrs.getValue(TYPE).equals("correct")) { inCorrectExample = true; correctExample = new StringBuilder(); } else if (EXAMPLE.equals(qName) && attrs.getValue(TYPE).equals("incorrect")) { inIncorrectExample = true; incorrectExample = new StringBuilder(); exampleCorrection = new StringBuilder(); if (attrs.getValue("correction") != null) { exampleCorrection.append(attrs.getValue("correction")); } } else if (MESSAGE.equals(qName)) { inMessage = true; message = new StringBuilder(); } else if (qName.equals("short")) { inShortMessage = true; shortMessage = new StringBuilder(); } else if (qName.equals(RULEGROUP)) { ruleGroupId = attrs.getValue("id"); ruleGroupDescription = attrs.getValue("name"); defaultOff = "off".equals(attrs.getValue(DEFAULT)); defaultOn = "on".equals(attrs.getValue(DEFAULT)); inRuleGroup = true; subId = 0; } else if (qName.equals("suggestion") && inMessage) { message.append("<suggestion>"); inSuggestion = true; } else if (qName.equals("match")) { setMatchElement(attrs); } else if (qName.equals(MARKER) && inCorrectExample) { correctExample.append("<marker>"); } else if (qName.equals(MARKER) && inIncorrectExample) { incorrectExample.append("<marker>"); } else if (qName.equals("unification")) { uFeature = attrs.getValue("feature"); inUnificationDef = true; } else if (qName.equals("equivalence")) { uType = attrs.getValue(TYPE); } else if (qName.equals("phrases")) { inPhrases = true; } else if (qName.equals("includephrases")) { phraseElementInit(); } else if (qName.equals("phrase") && inPhrases) { phraseId = attrs.getValue("id"); } else if (qName.equals("phraseref") && (attrs.getValue("idref") != null)) { preparePhrase(attrs); } else if (qName.equals("source")) { srcLang = Language.getLanguageForShortName(attrs.getValue("lang")); } }
public static void addReceiverAt(MESSAGE message, String receiver, int position) { message.getReceivers().getReceiver().add(position, receiver); }
public static void addReceiver(MESSAGE message, String receiver) { message.getReceivers().getReceiver().add(receiver); }