public void addDeclarations(Collection<? extends Declaration> ds) { for (Declaration d : ds) addDeclaration(d); }
public void visitSourceFile(SourceFile e) { implode(e.getDeclarations(), "\n" + indent); }
public void accept(Visitor visitor) { for (SourceFile sourceFile : sourceFiles) { sourceFile.accept(visitor); } for (Define define : defines) { define.accept(visitor); } }
if ( state.backtracking==0 ) { sourceFile = new SourceFile(); } sourceFile.addDeclaration((declaration1!=null?declaration1.declaration:null)); if (sourceFile.getElementFile() == null) sourceFile.setElementFile(getFile());
SourceFile sourceFile = new SourceFile(); sourceFile.setElementFile(sourcePath); for (String inc : new String[]{"v8.h", "node.h", "node_buffer.h"}) { sourceFile.addDeclaration(new Include(Include.Type.CInclude, inc)); if (librarySourceFiles != null) { for (File file : librarySourceFiles) { sourceFile.addDeclaration(new Include(Include.Type.CInclude, file.toString())); sourceFile.addDeclaration(new Include(Include.Type.ObjCImport, otherFramework + "/" + otherFramework + ".h")); initMethod.setBody(new Block()); sourceFile.addDeclaration(initMethod); new Arg("hint", new TypeRef.Pointer(typeRef("void"), Declarator.PointerStyle.Pointer))); dummyNodeBufferFreeCallback.setBody(new Block()); sourceFile.addDeclaration(dummyNodeBufferFreeCallback); sourceFile.addDeclaration(initMethod); sourceFile.addDeclaration(decl(stat(methodCall("NODE_MODULE", varRef(ident(library)), varRef(ident(initFunctionName)))))); writeLibraryInterface(result, sourceFiles, sourceFile, library, null);
private SourceFile parseBridgeSupportFile(File bsf) throws Exception { String framework = bsf.getName(); if (framework.toLowerCase().endsWith(".bridgesupport")) { framework = framework.substring(0, framework.length() - ".bridgesupport".length()); } Document xml = XMLUtils.readXML(bsf); String sourceFilePath = bsf.toString(); Node signatures = XMLUtils.getFirstNamedNode(xml, "signatures"); if (signatures == null) { return null; } SourceFile sf = new SourceFile(); sf.setElementFile(sourceFilePath); parseStructs(signatures, sf); parseEnums(signatures, sf); parseConstants(signatures, sf); parseClasses(framework, signatures, sf); parseStringConstants(framework, signatures, sf); parseFunctions(framework, signatures, sf); return sf; }
protected void writeLibraryInterface(Result result, SourceFiles sourceFiles, DeclarationsHolder interf, String library, Identifier javaPackage) throws IOException { Identifier fullLibraryClassName = interf.getResolvedJavaIdentifier(); Signatures signatures = result.getSignaturesForOutputClass(fullLibraryClassName); if (interf instanceof Struct) { interf = result.notifyBeforeWritingClass(fullLibraryClassName, (Struct) interf, signatures, library); if (interf != null) { final PrintWriter out = result.classOutputter.getClassSourceWriter(fullLibraryClassName.toString()); //out.println("///\n/// This file was autogenerated by JNAerator (http://jnaerator.googlecode.com/), \n/// a tool written by Olivier Chafik (http://ochafik.com/).\n///"); result.printJavaClass(javaPackage, (Struct) interf, out); out.close(); } } else if (interf instanceof SourceFile) { SourceFile sourceFile = (SourceFile) interf; String sourcePath = sourceFile.getElementFile(); assert sourcePath != null; final PrintWriter out = result.classOutputter.getSourceWriter(sourcePath); //out.println("///\n/// This file was autogenerated by JNAerator (http://jnaerator.googlecode.com/), \n/// a tool written by Olivier Chafik (http://ochafik.com/).\n///"); out.print(sourceFile); out.close(); } } }
if (testOption == TestOption.ParseAndPrettyPrint) { if (!SyntaxUtils.equal(string, sourceFile == null ? null : sourceFile.toString())) sourceFile = newParser(string).sourceFile(); sourceFile == null ? null : sourceFile.toString() .trim()); for (Declaration decl : sourceFile.getDeclarations()) { if (decl == null) continue;
String getLibrary(Element decl) { String file = resolveFile(decl); String library = config.getLibrary(file, Element.getName(decl)); if (library == null) { SourceFile f = decl.findParentOfType(SourceFile.class); if (f != null) { //library = config.getLibrary(f.getElementFile()); //if (library == null) { library = guessFramework(file); if (library == null) { library = f.getLibrary(); } //} } } return library; }
public void setDeclarations(List<Declaration> declarations) { changeValue(this, this.declarations, declarations); } public void addDeclaration(Declaration d) {
@Override public Element getNextChild(Element child) { return getNextSibling(declarations, child); }
SourceFile sourceFile = new SourceFile(); sourceFile.setElementFile(sourcePath); for (String inc : new String[]{"v8.h", "node.h", "node_buffer.h"}) { sourceFile.addDeclaration(new Include(Include.Type.CInclude, inc)); if (librarySourceFiles != null) { for (File file : librarySourceFiles) { sourceFile.addDeclaration(new Include(Include.Type.CInclude, file.toString())); sourceFile.addDeclaration(new Include(Include.Type.ObjCImport, otherFramework + "/" + otherFramework + ".h")); initMethod.setBody(new Block()); sourceFile.addDeclaration(initMethod); new Arg("hint", new TypeRef.Pointer(typeRef("void"), Declarator.PointerStyle.Pointer))); dummyNodeBufferFreeCallback.setBody(new Block()); sourceFile.addDeclaration(dummyNodeBufferFreeCallback); sourceFile.addDeclaration(initMethod); sourceFile.addDeclaration(decl(stat(methodCall("NODE_MODULE", varRef(ident(library)), varRef(ident(initFunctionName)))))); writeLibraryInterface(result, sourceFiles, sourceFile, library, null);
private SourceFile parseBridgeSupportFile(File bsf) throws Exception { String framework = bsf.getName(); if (framework.toLowerCase().endsWith(".bridgesupport")) { framework = framework.substring(0, framework.length() - ".bridgesupport".length()); } Document xml = XMLUtils.readXML(bsf); String sourceFilePath = bsf.toString(); Node signatures = XMLUtils.getFirstNamedNode(xml, "signatures"); if (signatures == null) { return null; } SourceFile sf = new SourceFile(); sf.setElementFile(sourceFilePath); parseStructs(signatures, sf); parseEnums(signatures, sf); parseConstants(signatures, sf); parseClasses(framework, signatures, sf); parseStringConstants(framework, signatures, sf); parseFunctions(framework, signatures, sf); return sf; }
protected void writeLibraryInterface(Result result, SourceFiles sourceFiles, DeclarationsHolder interf, String library, Identifier javaPackage) throws IOException { Identifier fullLibraryClassName = interf.getResolvedJavaIdentifier(); Signatures signatures = result.getSignaturesForOutputClass(fullLibraryClassName); if (interf instanceof Struct) { interf = result.notifyBeforeWritingClass(fullLibraryClassName, (Struct) interf, signatures, library); if (interf != null) { final PrintWriter out = result.classOutputter.getClassSourceWriter(fullLibraryClassName.toString()); //out.println("///\n/// This file was autogenerated by JNAerator (http://jnaerator.googlecode.com/), \n/// a tool written by Olivier Chafik (http://ochafik.com/).\n///"); result.printJavaClass(javaPackage, (Struct) interf, out); out.close(); } } else if (interf instanceof SourceFile) { SourceFile sourceFile = (SourceFile) interf; String sourcePath = sourceFile.getElementFile(); assert sourcePath != null; final PrintWriter out = result.classOutputter.getSourceWriter(sourcePath); //out.println("///\n/// This file was autogenerated by JNAerator (http://jnaerator.googlecode.com/), \n/// a tool written by Olivier Chafik (http://ochafik.com/).\n///"); out.print(sourceFile); out.close(); } } }
String getLibrary(Element decl) { String file = resolveFile(decl); String library = config.getLibrary(file, Element.getName(decl)); if (library == null) { SourceFile f = decl.findParentOfType(SourceFile.class); if (f != null) { //library = config.getLibrary(f.getElementFile()); //if (library == null) { library = guessFramework(file); if (library == null) { library = f.getLibrary(); } //} } } return library; }
private void parseFunctions(String framework, Node signatures, SourceFile sf) throws XPathExpressionException { for (Node function : XMLUtils.getChildrenByName(signatures, "function")) { String name = XMLUtils.getAttribute(function, "name"); Node retval = XMLUtils.getFirstNamedNode(function, "retval"); String already_retained = retval != null ? XMLUtils.getAttribute(retval, "already_retained") : null; if (already_retained != null && (already_retained = already_retained.trim()).length() > 0) { boolean alreadyRetained = "true".equals(already_retained); Result.getMap(result.retainedRetValFunctions, framework).put(name, alreadyRetained); } if ("true".equals(XMLUtils.getAttribute(function, "inline"))) { continue; // TODO handle inline functions : link to BridgeSupport auxiliary library } try { Function f = parseFunction(Type.CFunction, function, sf); if (f == null) { continue; } sf.addDeclaration(f); // f.accept(result); } catch (Exception ex) { ex.printStackTrace(); } } }
String rawSrcFilePath = new File(sourceFile.getElementFile()).getCanonicalPath(); String srcFilePath = result.config.relativizeFileForSourceComments(rawSrcFilePath); File srcFile = new File(srcFilePath);
public void visitSourceFile(SourceFile header) { visitElement(header); visit(header.getDeclarations()); }
public void accept(Visitor visitor) { for (SourceFile sourceFile : sourceFiles) { sourceFile.accept(visitor); } for (Define define : defines) { define.accept(visitor); } }
private void parseFunctions(String framework, Node signatures, SourceFile sf) throws XPathExpressionException { for (Node function : XMLUtils.getChildrenByName(signatures, "function")) { String name = XMLUtils.getAttribute(function, "name"); Node retval = XMLUtils.getFirstNamedNode(function, "retval"); String already_retained = retval != null ? XMLUtils.getAttribute(retval, "already_retained") : null; if (already_retained != null && (already_retained = already_retained.trim()).length() > 0) { boolean alreadyRetained = "true".equals(already_retained); Result.getMap(result.retainedRetValFunctions, framework).put(name, alreadyRetained); } if ("true".equals(XMLUtils.getAttribute(function, "inline"))) { continue; // TODO handle inline functions : link to BridgeSupport auxiliary library } try { Function f = parseFunction(Type.CFunction, function, sf); if (f == null) { continue; } sf.addDeclaration(f); // f.accept(result); } catch (Exception ex) { ex.printStackTrace(); } } }