Changeset 84 in default for v2/trunk/de.ugoe.cs.swe.bnftools.ebnf/src/de
- Timestamp:
- 07/02/14 13:59:44 (10 years ago)
- Location:
- v2/trunk/de.ugoe.cs.swe.bnftools.ebnf/src/de/ugoe/cs/swe/bnftools/generator
- Files:
-
- 1 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
v2/trunk/de.ugoe.cs.swe.bnftools.ebnf/src/de/ugoe/cs/swe/bnftools/generator/EbnfGenerator.xtend
r82 r84 4 4 package de.ugoe.cs.swe.bnftools.generator 5 5 6 import de.ugoe.cs.swe.bnftools.ebnf.Atom 7 import de.ugoe.cs.swe.bnftools.ebnf.BnfEntry 8 import de.ugoe.cs.swe.bnftools.ebnf.DefinitionList 9 import de.ugoe.cs.swe.bnftools.ebnf.EtsiBnf 10 import de.ugoe.cs.swe.bnftools.ebnf.GroupedSequence 11 import de.ugoe.cs.swe.bnftools.ebnf.OptionalSequence 12 import de.ugoe.cs.swe.bnftools.ebnf.RepeatedSequence 13 import de.ugoe.cs.swe.bnftools.ebnf.Rule 14 import de.ugoe.cs.swe.bnftools.ebnf.RuleReference 15 import de.ugoe.cs.swe.bnftools.ebnf.SingleDefinition 16 import de.ugoe.cs.swe.bnftools.ebnf.StringRule 17 import de.ugoe.cs.swe.bnftools.ebnf.Term 18 import java.io.File 6 19 import org.eclipse.emf.ecore.resource.Resource 20 import org.eclipse.xtext.generator.IFileSystemAccess 21 import org.eclipse.xtext.generator.IFileSystemAccessExtension2 7 22 import org.eclipse.xtext.generator.IGenerator 8 import org.eclipse.xtext.generator.IFileSystemAccess9 import de.ugoe.cs.swe.bnftools.ebnf.EtsiBnf10 import de.ugoe.cs.swe.bnftools.ebnf.BnfEntry11 import de.ugoe.cs.swe.bnftools.ebnf.Rule12 import de.ugoe.cs.swe.bnftools.ebnf.DefinitionList13 import de.ugoe.cs.swe.bnftools.ebnf.SingleDefinition14 import de.ugoe.cs.swe.bnftools.ebnf.Term15 import de.ugoe.cs.swe.bnftools.ebnf.GroupedSequence16 import de.ugoe.cs.swe.bnftools.ebnf.Atom17 import de.ugoe.cs.swe.bnftools.ebnf.RepeatedSequence18 import de.ugoe.cs.swe.bnftools.ebnf.OptionalSequence19 import de.ugoe.cs.swe.bnftools.ebnf.RuleReference20 import de.ugoe.cs.swe.bnftools.ebnf.StringRule21 import java.io.File22 import org.eclipse.xtext.generator.IFileSystemAccessExtension223 import org.eclipse.emf.common.util.URI;24 import org.eclipse.xtext.generator.IFileSystemAccessExtension25 import java.io.PrintWriter26 23 27 24 /** … … 32 29 class EbnfGenerator implements IGenerator { 33 30 34 //generation of the xsl-file 35 override void doGenerate(Resource resource, IFileSystemAccess fsa) {31 32 def void doGenerate(Resource resource, IFileSystemAccess fsa,boolean mode) { 36 33 var String workspacePath = WorkspaceResolver.getWorkspace(); 37 34 for (e : resource.allContents.toIterable.filter(EtsiBnf)) { … … 45 42 46 43 if (file.exists) { 44 //true -> pdf, false -> rtf 47 45 48 foToPdf.createPdfFromFo(fullUri.substring(0, fullUri.length - 3)); 46 if(mode){ 47 FoToPdfOrRtf.createRtfFromFo(fullUri.substring(0, fullUri.length - 3)); 48 }else{ 49 FoToPdfOrRtf.createPdfFromFo(fullUri.substring(0, fullUri.length - 3)); 50 } 51 52 // fsa.deleteFile(e.name + ".fo"); 53 } 54 } 55 } 56 } 57 58 override void doGenerate(Resource resource, IFileSystemAccess fsa) { 59 var String workspacePath = WorkspaceResolver.getWorkspace(); 60 for (e : resource.allContents.toIterable.filter(EtsiBnf)) { 61 if (e.bnfEntry.size != 0) { 62 fsa.generateFile(e.name + ".fo", e.compile) 63 System.out.println("fo generated"); 64 //generate pdf 65 var uri = (fsa as IFileSystemAccessExtension2).getURI(e.name + ".fo"); 66 var String fullUri = workspacePath + uri.path.substring(10, uri.path.length); 67 var File file = new File(fullUri); 68 69 if (file.exists) { 70 71 FoToPdfOrRtf.createPdfFromFo(fullUri.substring(0, fullUri.length - 3)); 49 72 // fsa.deleteFile(e.name + ".fo"); 50 73 } … … 84 107 ''' 85 108 86 def compile(Rule rule) ''' <fo:block><fo:inline id="«rule.name»" color="purple">«IF rule.rulenumber != 0»«rule.109 def compile(Rule rule) '''«System.out.println(rule.name)»<fo:block><fo:inline id="«rule.name»" color="purple">«IF rule.rulenumber != 0»«rule. 87 110 rulenumber». «ENDIF»«rule.name»</fo:inline> ::= «rule.definitionList.compile»</fo:block>''' 88 111 89 def compile(DefinitionList dList) '''« FOR sDef : dList.singleDefinition»«sDef.compile»«IF !sDef.equals(90 dList.singleDefinition.last)» | «ENDIF»«ENDFOR» '''112 def compile(DefinitionList dList) '''«IF dList!= null»«FOR sDef : dList.singleDefinition»«sDef.compile»«IF !sDef.equals( 113 dList.singleDefinition.last)» | «ENDIF»«ENDFOR»«ENDIF»''' 91 114 92 115 def compile(SingleDefinition sDefinition) '''«FOR term : sDefinition.terms»«term.compile»«ENDFOR»''' … … 110 133 111 134 def compile(StringRule sRule) '''«IF sRule.colon != null»«sRule.colon»«ENDIF»«IF sRule.literal != null»«sRule. 112 literal » «ENDIF»'''135 literal.replaceAll("&","&").replaceAll("<","<").replaceAll(">",">")» «ENDIF»''' 113 136 114 137 } -
v2/trunk/de.ugoe.cs.swe.bnftools.ebnf/src/de/ugoe/cs/swe/bnftools/generator/FoToPdfOrRtf.java
r82 r84 23 23 24 24 25 public class foToPdf {25 public class FoToPdfOrRtf { 26 26 27 27 … … 33 33 } 34 34 35 private static void createfoFromPdf(String foFileName, String pdfFileName)throws IOException, FOPException, 35 public static void createRtfFromFo(String fileName) throws IOException, FOPException, 36 TransformerException{ 37 38 createRtfFromFo(fileName,fileName); 39 } 40 41 private static void createRtfFromFo(String foFileName, String pdfFileName)throws IOException, FOPException, 36 42 TransformerException{ 37 43 File file = new File(foFileName+".fo"); … … 45 51 // Step 3: Construct fop with desired output format 46 52 Fop fop = fopFactory.newFop(MimeConstants.MIME_RTF, out); 53 54 // Step 4: Setup JAXP using identity transformer 55 TransformerFactory factory = TransformerFactory.newInstance(); 56 Transformer transformer = factory.newTransformer(); 57 58 // Step 5: Setup input and output for XSLT transformation 59 Source src = new StreamSource(file); 60 61 // Resulting SAX events (the generated FO) must be piped through to FOP 62 Result res = new SAXResult(fop.getDefaultHandler()); 63 64 // Step 6: Start XSLT transformation and FOP processing 65 transformer.transform(src, res); 66 } finally { 67 out.close(); 68 } 69 } 70 71 72 private static void createfoFromPdf(String foFileName, String pdfFileName)throws IOException, FOPException, 73 TransformerException{ 74 File file = new File(foFileName+".fo"); 75 // Step 1: Construct a FopFactory 76 FopFactory fopFactory = FopFactory.newInstance(); 77 78 // Step 2: Set up output stream. 79 OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(pdfFileName+".pdf"))); 80 81 try { 82 // Step 3: Construct fop with desired output format 83 Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out); 47 84 48 85 // Step 4: Setup JAXP using identity transformer
Note: See TracChangeset
for help on using the changeset viewer.