List of usage examples for java.io Writer append
public Writer append(char c) throws IOException
From source file:annis.visualizers.iframe.CorefVisualizer.java
/** * writes Output for the CorefVisualizer * @param writer writer to write with//from www. j a v a 2s . co m */ @Override public void writeOutput(VisualizerInput input, Writer w) { // root html element Html html = new Html(); Head head = new Head(); Body body = new Body(); html.removeXmlns(); html.appendChild(head); html.appendChild(body); try { LinkedList<String> fonts = new LinkedList<String>(); if (input.getFont() != null) { Link linkFont = new Link(); linkFont.setHref(input.getFont().getUrl()); head.appendChild(linkFont); fonts.add(input.getFont().getName()); } fonts.add("serif"); Link linkTooltip = new Link(); linkTooltip.setHref(input.getResourcePath("coref/jquery.tooltip.css")); linkTooltip.setRel("stylesheet"); linkTooltip.setType("text/css"); head.appendChild(linkTooltip); Script scriptJquery = new Script("text/javascript"); scriptJquery.setSrc(input.getResourcePath("coref/jquery-1.6.2.min.js")); head.appendChild(scriptJquery); Script scriptTooltip = new Script("text/javascript"); scriptTooltip.setSrc(input.getResourcePath("coref/jquery.tooltip.min.js")); head.appendChild(scriptTooltip); Link linkCoref = new Link(); linkCoref.setHref(input.getResourcePath("coref/coref.css")); linkCoref.setRel("stylesheet"); linkCoref.setType("text/css"); head.appendChild(linkCoref); Script scriptCoref = new Script("text/javascript"); scriptCoref.setSrc(input.getResourcePath("coref/CorefVisualizer.js")); head.appendChild(scriptCoref); body.setStyle("font-family: '" + StringUtils.join(fonts, "', '") + "';"); //get Info globalIndex = 0; tokensOfNode = new HashMap<String, List<String>>(); referentList = new LinkedList<TReferent>(); komponent = new LinkedList<TComponent>(); referentOfToken = new HashMap<String, HashMap<Long, Integer>>(); componentOfToken = new HashMap<String, List<Long>>(); componenttype = new LinkedList<TComponenttype>(); SDocument saltDoc = input.getDocument(); SDocumentGraph saltGraph = saltDoc.getSDocumentGraph(); if (saltGraph == null) { body.setText("An Error occured: Could not get Graph of Result (Graph == null)."); return; } List<SRelation> edgeList = saltGraph.getSRelations(); if (edgeList == null) { return; } for (SRelation rawRel : edgeList) { if (includeEdge(rawRel, input.getNamespace())) { SPointingRelation rel = (SPointingRelation) rawRel; String relType = componentNameForRelation(rel); visitedNodes = new LinkedList<String>(); //got type for this? boolean gotIt = false; int componentnr; for (componentnr = 0; componentnr < componenttype.size(); componentnr++) { if (componenttype.get(componentnr) != null && componenttype.get(componentnr).type != null && componenttype.get(componentnr).nodeList != null && componenttype.get(componentnr).type.equals(relType) && componenttype.get(componentnr).nodeList .contains(rel.getSStructuredSource().getSId())) { gotIt = true; break; } } TComponent currentComponent; TComponenttype currentComponenttype; if (gotIt) { currentComponent = komponent.get(componentnr); currentComponenttype = componenttype.get(componentnr); } else { currentComponenttype = new TComponenttype(); currentComponenttype.type = relType; componenttype.add(currentComponenttype); componentnr = komponent.size(); currentComponent = new TComponent(); currentComponent.type = relType; currentComponent.tokenList = new LinkedList<String>(); komponent.add(currentComponent); currentComponenttype.nodeList.add(rel.getSStructuredSource().getSId()); } TReferent ref = new TReferent(); ref.annotations = new HashSet<SAnnotation>(); ref.annotations.addAll(rel.getSAnnotations()); ref.component = componentnr; referentList.add(ref); List<String> currentTokens = getAllTokens(rel.getSStructuredSource(), componentNameForRelation(rel), currentComponenttype, componentnr, input.getNamespace()); setReferent(rel.getSStructuredTarget(), globalIndex, 0);//neu setReferent(rel.getSStructuredSource(), globalIndex, 1);//neu for (String s : currentTokens) { if (!currentComponent.tokenList.contains(s)) { currentComponent.tokenList.add(s); } } globalIndex++; } } colorlist = new HashMap<Integer, Integer>(); // A list containing all the generated HTML elements, one list entry // for each text. List<List<Node>> nodesPerText = new LinkedList<List<Node>>(); // write output for each text separatly EList<STextualDS> texts = saltGraph.getSTextualDSs(); if (texts != null && !texts.isEmpty()) { for (STextualDS t : texts) { SDataSourceSequence sequence = SaltFactory.eINSTANCE.createSDataSourceSequence(); sequence.setSSequentialDS(t); sequence.setSStart(0); sequence.setSEnd((t.getSText() != null) ? t.getSText().length() : 0); EList<SToken> token = saltGraph.getSTokensBySequence(sequence); if (token != null) { boolean validText = true; if (Boolean.parseBoolean(input.getMappings().getProperty("hide_empty", "false"))) { validText = false; // check if the text contains any matching annotations for (SToken tok : token) { /* * The token is only added to this map if an valid edge * (according to the resolver trigger) conntected to * this token was found. */ if (referentOfToken.get(tok.getSId()) != null && !referentOfToken.get(tok.getSId()).isEmpty()) { validText = true; break; } } } if (validText) { List<Node> nodes = outputSingleText(token, input); nodesPerText.add(nodes); } } } // end for each STexutalDS /* * Append the generated output to the body, wrap in table if necessary. */ // present all texts as columns side by side if using multiple texts Table tableTexts = new Table(); Tr trTextRow = new Tr(); trTextRow.setCSSClass("textRow"); // only append wrapper table if we have multiple texts if (nodesPerText.size() > 1) { body.appendChild(tableTexts); tableTexts.appendChild(trTextRow); } for (List<Node> nodes : nodesPerText) { // multi-text mode? if (nodesPerText.size() > 1) { Td tdSingleText = new Td(); trTextRow.appendChild(tdSingleText); tdSingleText.setCSSClass("text"); tdSingleText.appendChild(nodes); } else { body.appendChild(nodes); } } } else { Text errorTxt = new Text( "Could not find any texts for the " + input.getNamespace() + " node namespace (layer)."); body.appendChild(errorTxt); } // write HTML4 transitional doctype w.append(new Doctype(DocumentType.HTMLTransitional).write()); // append the html tree w.append(html.write()); } catch (IOException ex) { log.error(null, ex); } }
From source file:org.openmrs.arden.impl.ArdenServiceImpl.java
/** * @param s/*from w ww. j a va2 s. c om*/ * @param fn */ private boolean parseFile(InputStream s, String fn, String outFolder) throws Exception { boolean retVal = true; try { Date Today = new Date(); String cfn; String packagePrefix = Context.getAdministrationService() .getGlobalProperty("logic.default.packageName"); if (StringUtils.isEmpty(packagePrefix)) { packagePrefix = "org.openmrs.logic.rule"; } MLMObject ardObj = new MLMObject(Context.getLocale(), null); // Create a scanner that reads from the input stream passed to us ArdenBaseLexer lexer = new ArdenBaseLexer(s); // Create a parser that reads from the scanner ArdenBaseParser parser = new ArdenBaseParser(lexer); // start parsing at the compilation unit rule parser.startRule(); BaseAST t = (BaseAST) parser.getAST(); if (log.isDebugEnabled()) { log.debug(t.toStringTree()); // prints maintenance } ArdenBaseTreeParser treeParser = new ArdenBaseTreeParser(); String maintenance = treeParser.maintenance(t, ardObj); cfn = ardObj.getClassName(); String packageFolderName = packagePrefix.replace('.', File.separatorChar); File packageFolder = new File(outFolder, packageFolderName); if (!packageFolder.exists()) { packageFolder.mkdirs(); } // make sure that the file is stored in the correct folder based on the package OutputStream os = new FileOutputStream(new File(packageFolder, cfn + ".java")); Writer w = new OutputStreamWriter(os); log.info("Writing to file - " + cfn + ".java"); w.write("/********************************************************************" + "\n Translated from - " + fn + " on " + Today.toString() + "\n\n"); w.write(maintenance); t = (BaseAST) t.getNextSibling(); // Move to library if (log.isDebugEnabled()) { log.debug(t.toStringTree()); // prints library } String library = treeParser.library(t, ardObj); w.write(library); w.write("\n********************************************************************/\n"); w.write("package " + packagePrefix + ";\n\n"); w.write("import java.util.ArrayList;\n"); w.write("import java.util.HashMap;\n"); w.write("import java.util.List;\n"); w.write("import java.util.Map;\n"); w.write("import java.util.Set;\n"); w.write("import java.util.Collection;\n"); w.write("import java.util.Collections;\n"); w.write("import org.apache.commons.logging.Log;\n"); w.write("import org.apache.commons.logging.LogFactory;\n"); w.write("import org.openmrs.Patient;\n"); w.write("import org.openmrs.api.PatientService;\n"); w.write("import org.openmrs.api.context.Context;\n"); w.write("import org.openmrs.arden.MlmRule;\n"); w.write("import org.openmrs.logic.LogicContext;\n"); w.write("import org.openmrs.logic.impl.LogicCriteriaImpl;\n"); w.write("import org.openmrs.logic.LogicException;\n"); w.write("import org.openmrs.logic.Rule;\n"); w.write("import org.openmrs.logic.result.Result;\n"); w.write("import org.openmrs.logic.result.Result.Datatype;\n"); w.write("import org.openmrs.logic.rule.RuleParameterInfo;\n"); w.write("import org.openmrs.logic.rule.provider.RuleProvider;\n"); w.write("import org.openmrs.logic.Duration;\n"); w.write("import java.util.StringTokenizer;\n\n"); w.write("import org.openmrs.api.ConceptService;\n"); w.write("import java.text.SimpleDateFormat;\n"); w.write("import org.openmrs.Concept;\n"); w.write("import org.openmrs.ConceptName;\n"); String classname = ardObj.getClassName(); w.write("public class " + classname + " implements MlmRule{\n\n"); // Start of class w.write("\tprivate Log log = LogFactory.getLog(this.getClass());\n"); w.flush(); /************************************************************************************** * Implement the other interface methods */ String str = ""; w.write("\t/*** @see org.openmrs.logic.rule.Rule#getDuration()*/\n\t" + "public int getDuration() {\n\t\treturn 60*30; // 30 minutes\n\t}\n\n"); w.write("\t/*** @see org.openmrs.logic.rule.Rule#getDatatype(String)*/\n\t" + "public Datatype getDatatype(String token) {\n\t" + "\treturn Datatype.TEXT;\n\t}\n\n"); w.write("\t/*** @see org.openmrs.logic.rule.Rule#getParameterList()*/\n\t" + "public Set<RuleParameterInfo> getParameterList() {\n\t\treturn null;\n\t}\n\n"); w.write("\t/*** @see org.openmrs.logic.rule.Rule#getDependencies()*/\n\t" + "public String[] getDependencies() {\n\t\treturn new String[] { };\n\t}\n\n"); w.write("\t/*** @see org.openmrs.logic.rule.Rule#getTTL()*/\n\t" + "public int getTTL() {\n\t\treturn 0; //60 * 30; // 30 minutes\n\t}\n\n"); w.write("\t/*** @see org.openmrs.logic.rule.Rule#getDatatype(String)*/\n\t" + "public Datatype getDefaultDatatype() {\n\t\treturn Datatype.CODED;\n\t}\n\n"); str = ardObj.getAuthor(); if (str != null && str.length() == 0) { str = null; } if (str != null) { str = "\"" + str + "\""; } w.write("\t/*** @see org.openmrs.arden.MlmRule#getAuthor()*/\n" + "\tpublic String getAuthor(){\n" + "\t\treturn " + str + ";\n" + "\t}\n\n"); str = ardObj.getCitations(); if (str != null && str.length() == 0) { str = null; } if (str != null) { str = "\"" + str + "\""; } w.write("\t/*** @see org.openmrs.arden.MlmRule#getCitations()*/\n" + "\tpublic String getCitations(){\n" + "\t\treturn " + str + ";\n" + "\t}\n\n"); str = ardObj.getDate(); if (str != null && str.length() == 0) { str = null; } if (str != null) { str = "\"" + str + "\""; } w.write("\t/*** @see org.openmrs.arden.MlmRule#getDate()*/\n" + "\tpublic String getDate(){\n" + "\t\treturn " + str + ";\n" + "\t}\n\n"); str = ardObj.getExplanation(); if (str != null && str.length() == 0) { str = null; } if (str != null) { str = "\"" + str + "\""; } w.write("\t/*** @see org.openmrs.arden.MlmRule#getExplanation()*/\n" + "\tpublic String getExplanation(){\n" + "\t\treturn " + str + ";\n" + "\t}\n\n"); str = ardObj.getInstitution(); if (str != null && str.length() == 0) { str = null; } if (str != null) { str = "\"" + str + "\""; } w.write("\t/*** @see org.openmrs.arden.MlmRule#getInstitution()*/\n" + "\tpublic String getInstitution(){\n" + "\t\treturn " + str + ";\n" + "\t}\n\n"); str = ardObj.getKeywords(); if (str != null && str.length() == 0) { str = null; } if (str != null) { str = "\"" + str + "\""; } w.write("\t/*** @see org.openmrs.arden.MlmRule#getKeywords()*/\n" + "\tpublic String getKeywords(){\n" + "\t\treturn " + str + ";\n" + "\t}\n\n"); str = ardObj.getLinks(); if (str != null && str.length() == 0) { str = null; } if (str != null) { str = "\"" + str + "\""; } w.write("\t/*** @see org.openmrs.arden.MlmRule#getLinks()*/\n" + "\tpublic String getLinks(){\n" + "\t\treturn " + str + ";\n" + "\t}\n\n"); str = ardObj.getPurpose(); if (str != null && str.length() == 0) { str = null; } if (str != null) { str = "\"" + str + "\""; } w.write("\t/*** @see org.openmrs.arden.MlmRule#getPurpose()*/\n" + "\tpublic String getPurpose(){\n" + "\t\treturn " + str + ";\n" + "\t}\n\n"); str = ardObj.getSpecialist(); if (str != null && str.length() == 0) { str = null; } if (str != null) { str = "\"" + str + "\""; } w.write("\t/*** @see org.openmrs.arden.MlmRule#getSpecialist()*/\n" + "\tpublic String getSpecialist(){\n" + "\t\treturn " + str + ";\n" + "\t}\n\n"); str = ardObj.getTitle(); if (str != null && str.length() == 0) { str = null; } if (str != null) { str = "\"" + str + "\""; } w.write("\t/*** @see org.openmrs.arden.MlmRule#getTitle()*/\n" + "\tpublic String getTitle(){\n" + "\t\treturn " + str + ";\n" + "\t}\n\n"); double d = ardObj.getVersion(); w.write("\t/*** @see org.openmrs.arden.MlmRule#getVersion()*/\n" + "\tpublic Double getVersion(){\n" + "\t\treturn " + d + ";\n" + "\t}\n\n"); str = ardObj.getType(); if (str != null && str.length() == 0) { str = null; } if (str != null) { str = "\"" + str + "\""; } w.write("\t/*** @see org.openmrs.arden.MlmRule#getType()*/\n" + "\tpublic String getType(){\n" + "\t\treturn " + str + ";\n" + "\t}\n\n"); /**************************************************************************************/ t = (BaseAST) t.getNextSibling(); // Move to Knowledge log.debug(t.toStringTree()); // prints knowledge /**************************************************Write Knowledge dependent section**********************************************/ Integer p = ardObj.getPriority(); w.write("\t/*** @see org.openmrs.arden.MlmRule#getPriority()*/\n" + "\tpublic Integer getPriority(){\n" + "\t\treturn " + p + ";\n" + "\t}\n\n"); str = ardObj.getData(); if (str != null && str.length() == 0) { str = null; } if (str != null) { str = "\"" + str + "\""; } w.write("\t/*** @see org.openmrs.arden.MlmRule#getData()*/\n" + "\tpublic String getData(){\n" + "\t\treturn " + str + ";\n" + "\t}\n\n"); str = ardObj.getLogic(); if (str != null && str.length() == 0) { str = null; } if (str != null) { str = "\"" + str + "\""; } w.write("\t/*** @see org.openmrs.arden.MlmRule#getLogic()*/\n" + "\tpublic String getLogic(){\n" + "\t\treturn " + str + ";\n" + "\t}\n\n"); str = ardObj.getAction(); if (str != null && str.length() == 0) { str = null; } if (str != null) { str = "\"" + str + "\""; } w.write("\t/*** @see org.openmrs.arden.MlmRule#getAction()*/\n" + "\tpublic String getAction(){\n" + "\t\treturn " + str + ";\n" + "\t}\n\n"); Integer ageMin = ardObj.getAgeMin(); w.write("\t/*** @see org.openmrs.arden.MlmRule#getAgeMin()*/\n" + "\tpublic Integer getAgeMin(){\n" + "\t\treturn " + ageMin + ";\n" + "\t}\n\n"); str = ardObj.getAgeMinUnits(); if (str != null && str.length() == 0) { str = null; } if (str != null) { str = "\"" + str + "\""; } w.write("\t/*** @see org.openmrs.arden.MlmRule#getAgeMinUnits()*/\n" + "\tpublic String getAgeMinUnits(){\n" + "\t\treturn " + str + ";\n" + "\t}\n\n"); Integer ageMax = ardObj.getAgeMax(); w.write("\t/*** @see org.openmrs.arden.MlmRule#getAgeMax()*/\n" + "\tpublic Integer getAgeMax(){\n" + "\t\treturn " + ageMax + ";\n" + "\t}\n\n"); str = ardObj.getAgeMaxUnits(); if (str != null && str.length() == 0) { str = null; } if (str != null) { str = "\"" + str + "\""; } w.write("\t/*** @see org.openmrs.arden.MlmRule#getAgeMaxUnits()*/\n" + "\tpublic String getAgeMaxUnits(){\n" + "\t\treturn " + str + ";\n" + "\t}\n\n"); w.write("\tprivate static boolean containsIgnoreCase(Result key,List<Result> lst){\n"); w.write("\t\tif(key == null){\n"); w.write("\t\t\treturn false;\n"); w.write("\t\t}\n"); w.write("\t\tString keyString = \"\";\n"); w.write("\t\tif(key.getDatatype() == Result.Datatype.CODED) {\n"); w.write("\t\t\tConcept keyConcept = key.toConcept();\n"); w.write("\t\t\tif(keyConcept != null) {\n"); w.write("\t\t\t\tkeyString = ((ConceptName) keyConcept.getNames().toArray()[0]).getName();\n"); w.write("\t\t\t}\n"); w.write("\t\t} else {\n"); w.write("\t\t\tkeyString = key.toString();\n"); w.write("\t\t}\n"); w.write("\t\tfor(Result element:lst){\n"); w.write("\t\t\tConcept concept = element.toConcept();\n"); w.write("\t\t\tif(concept == null){\n"); w.write("\t\t\t\tcontinue;\n"); w.write("\t\t\t}\n"); w.write("\t\t\tCollection<ConceptName> cns = concept.getNames();\n"); w.write("\t\t\tfor(ConceptName cn:cns) {\n"); w.write("\t\t\t\tString elementString = cn.getName();\n"); w.write("\t\t\t\tif(keyString.equalsIgnoreCase(elementString)){\n"); w.write("\t\t\t\t\treturn true;\n"); w.write("\t\t\t\t}\n"); w.write("\t\t}\n"); w.write("\t\t}\n"); w.write("\t\treturn false;\n"); w.write("\t}\n"); w.write("\tprivate static String toProperCase(String str){\n\n"); w.write("\t\tif(str == null || str.length()<1){\n"); w.write("\t\t\treturn str;\n"); w.write("\t\t}\n\n"); w.write("\t\tStringBuffer resultString = new StringBuffer();\n"); w.write("\t\tString delimiter = \" \";\n"); w.write("\t\tStringTokenizer tokenizer = new StringTokenizer(str,delimiter,true);\n"); w.write("\t\tString currToken = null;\n\n"); w.write("\t\twhile(tokenizer.hasMoreTokens()){\n"); w.write("\t\t\tcurrToken = tokenizer.nextToken();\n"); w.write("\t\t\tif(!currToken.equals(delimiter)){\n"); w.write("\t\t\t\tif(currToken.length()>0){\n"); w.write("\t\t\t\t\tcurrToken = currToken.substring(0, 1).toUpperCase()\n"); w.write("\t\t\t\t\t+ currToken.substring(1).toLowerCase();\n"); w.write("\t\t\t\t}\n"); w.write("\t\t\t}\n"); w.write("\t\t\tresultString.append(currToken);\n"); w.write("\t\t}\n"); w.write("\t\treturn resultString.toString();\n"); w.write("\t}\n"); /** * ************************************************************************************* * ********************************************* */ // Move back to knowledge tree to actually start converting data // logic action t = (BaseAST) parser.getAST(); t = (BaseAST) t.getNextSibling().getNextSibling(); // Move to // Knowledge log.debug(t.toStringTree()); // prints knowledge /** *********************************************************************************** */ ardObj.PrintEvaluateList("data"); // To Debug retVal = ardObj.WriteEvaluate(w, classname); if (retVal) { ardObj.WriteAction(w); w.append("}"); // end class w.flush(); w.close(); } else { //delete the compiled file so far w.flush(); w.close(); boolean success = (new File("src/api/org/openmrs/logic/rule/" + cfn + ".java")).delete(); if (!success) { System.out.println("Incomplete compiled file " + cfn + ".java cannot be deleted!"); } } } catch (Exception e) { log.error(e); } finally { s.close(); } return retVal; }