Package com.sun.codemodel

Examples of com.sun.codemodel.JMethod


    final ArrayList<String> comment = new ArrayList<String>();
    comment.add("Java to KML\n");
    comment.add("The object graph is marshalled to an OutputStream object.\n");
    comment.add("The object is not saved as a zipped .kmz file.\n");
    comment.add("@see marshalKmz(String, Kml...)");
    final JMethod generateMarshalOutputStream = generateMarshal(cc, outputStreamClass, comment);
    comment.clear();
    // public boolean marshal(final Writer writer) {
    // try {
    // m = this.createMarshaller();
    // JAXBElement<Kml> jaxbRootElement = new JAXBElement<Kml>(new QName("http://www.opengis.net/kml/2.2", "kml"), Kml.class, this);
    // m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    // m.marshal(jaxbRootElement, writer);
    // return true;
    // } catch (JAXBException _x) {
    // _x.printStackTrace();
    // return false;
    // }
    // }
    comment.add("Java to KML\n");
    comment.add("The object graph is marshalled to a Writer object.\n");
    comment.add("The object is not saved as a zipped .kmz file.\n");
    comment.add("@see marshalKmz(String, Kml...)");
    generateMarshal(cc, writerClass, comment);
    comment.clear();
   
   
    comment.add("Java to KML\n");
    comment.add("The object graph is marshalled to a Contenthandler object.\n");
    comment.add("Useful if  marshaller cis needed to generate CDATA blocks.\n");
    comment.add("{@link https://jaxb.dev.java.net/faq/}\n");
    comment.add("{@link http://code.google.com/p/javaapiforkml/issues/detail?id=7}\n");
    comment.add("The object is not saved as a zipped .kmz file.\n");
    comment.add("@see marshalKmz(String, Kml...)");
    generateMarshal(cc, contentHandlerClass, comment);
    comment.clear();

    comment.add("Java to KML\n");
    comment.add("The object graph is printed to the console.\n");
    comment.add("(Nothing is saved, nor saved. Just printed.)\n");
    generateMarshal(cc, comment);
    comment.clear();

    // public boolean marshal(final File filename, boolean zipped) throws FileNotFoundException {
    // OutputStream out = new FileOutputStream(filename);
    // if (zipped) {
    // out = new ZipOutputStream(out);
    // }
    //
    // return this.marshall(out);
    // }
    comment.add("Java to KML\n");
    comment.add("The object graph is marshalled to a File object.\n");
    comment.add("The object is not saved as a zipped .kmz file.\n");
    comment.add("@see marshalKmz(String, Kml...)");
    final JMethod generateMarshallFilenameWithZIP = cc.implClass.method(JMod.PUBLIC, cc.implClass.owner().BOOLEAN, "marshal");
    generateMarshallFilenameWithZIP._throws(FileNotFoundException.class);
    generateMarshallFilenameWithZIP.javadoc().append(comment);

    final JVar filenameVar = generateMarshallFilenameWithZIP.param(JMod.FINAL, File.class, "filename");
    // final JVar zippedVar = generateMarshallFilenameWithZIP.param(JMod.FINAL, boolean.class, "zipped");
    final JVar outVar = generateMarshallFilenameWithZIP.body().decl(outputStreamClass, "out", JExpr._new(fileOutputStreamClass).arg(filenameVar));
    // final JConditional ifBlockFilename = generateMarshallFilenameWithZIP.body()._if(zippedVar.eq(JExpr.TRUE));
    // ifBlockFilename._then().assign(outVar, JExpr._new(zipOutputStreamClass).arg(outVar));
    generateMarshallFilenameWithZIP.body()._return(JExpr._this().invoke(generateMarshalOutputStream).arg(outVar));
    comment.clear();

//    comment.add("Java to KML\n");
//    comment.add("The object graph is marshalled to a File object.\n");
//    comment.add("The object is not saved as a zipped .kmz file.\n");
View Full Code Here




  private JMethod generateMarshal(final ClassOutlineImpl cc, final ArrayList<String> comment) {
    // public boolean marshall(final String filename) throws FileNotFoundException {
    final JMethod generateMarshalOutputStream = cc.implClass.method(JMod.PUBLIC, cc.implClass.owner().BOOLEAN, "marshal");
    generateMarshalOutputStream.javadoc().append(comment);

    // try {
    final JTryBlock tryBlock = generateMarshalOutputStream.body()._try();
    // m = this.createMarshaller();
    tryBlock.body().assign(mVar, JExpr._this().invoke(createMashaller));

    // JAXBElement<Kml> jaxbRootElement = new JAXBElement<Kml>(new QName("http://www.opengis.net/kml/2.2", "kml"), Kml.class, this);
//    JInvocation newJaxbElement = JExpr._new(jaxbElementClass).arg(createQName("http://www.opengis.net/kml/2.2", "kml")).arg(
View Full Code Here

    return generateMarshalOutputStream;
  }

  private JMethod generateMarshal(final ClassOutlineImpl cc, final JType argumentType, final ArrayList<String> comment) {
    // public boolean marshall(final String filename) throws FileNotFoundException {
    final JMethod generateMarshalOutputStream = cc.implClass.method(JMod.PUBLIC, cc.implClass.owner().BOOLEAN, "marshal");
    if (argumentType.equals(outputStreamClass)) {
      generateMarshalOutputStream._throws(FileNotFoundException.class);
    }
    generateMarshalOutputStream.javadoc().append(comment);
    final JVar value = generateMarshalOutputStream.param(JMod.FINAL, argumentType, argumentType.name().toLowerCase());

    // try {
    final JTryBlock tryBlock = generateMarshalOutputStream.body()._try();
    // m = this.createMarshaller();
    tryBlock.body().assign(mVar, JExpr._this().invoke(createMashaller));

    // JAXBElement<Kml> jaxbRootElement = new JAXBElement<Kml>(new QName("http://www.opengis.net/kml/2.2", "kml"), Kml.class, this);
//    JInvocation newJaxbElement = JExpr._new(jaxbElementClass).arg(createQName("http://www.opengis.net/kml/2.2", "kml")).arg(
View Full Code Here

  private void generateUnMarshalMethods(final ClassOutlineImpl cc) {
    schemaLocationVar = cc.implClass.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, stringClass, "SCHEMA_LOCATION", JExpr
        .lit("src/main/resources/schema/ogckml/ogckml22.xsd"));

    final JMethod generateValidate = generateValidateMethod(cc);

    generateUnmarshalFileWithOptionalValidate(cc, generateValidate);

    generateUnmarshalMethodFromString(cc, stringReaderClass);
View Full Code Here

   *   }
   *   return null;
   * }
   */
  private void generateUnmarshalLegacyKmlMethod(final ClassOutlineImpl cc) {
    final JMethod generateUnMarshallerFromString = cc.implClass.method(JMod.PUBLIC | JMod.STATIC, kmlClass, "unmarshalLegacyKml");
    generateUnMarshallerFromString.javadoc().add("KML to Java\n");
    generateUnMarshallerFromString.javadoc().add("reads legacy kml files. \n");
    generateUnMarshallerFromString.javadoc().add("Supported are KML 2.0 (namespace: http://earth.google.com/kml/2.0) \n");
    generateUnMarshallerFromString.javadoc().add("          and KML 2.1 (namespace: http://earth.google.com/kml/2.1) \n");
    generateUnMarshallerFromString.javadoc().trimToSize();
   
    final JVar fileunmarshallVar = generateUnMarshallerFromString.param(JMod.FINAL, fileClass, "file");
    generateUnMarshallerFromString._throws(FileNotFoundException.class);
    final JTryBlock tryBlock = generateUnMarshallerFromString.body()._try();
    final JVar localUnmarshallerFile = tryBlock.body().decl(jaxbUnmarshallerClass, "unmarshaller",
        jaxbContextClass.boxify().staticInvoke("newInstance").arg(JExpr.direct("Kml.class")).invoke("createUnmarshaller"));
   
    final JVar inputsource = tryBlock.body().decl(inputsourceClass, "input", JExpr._new(inputsourceClass).arg(JExpr._new(fileReaderClass).arg(fileunmarshallVar)));
    final JVar saxsource = tryBlock.body().decl(saxsourceClass, "saxSource", JExpr._new(saxsourceClass).arg(JExpr._new(namespaceFilterXMLReaderclass).arg(JExpr.FALSE)).arg(inputsource));
    final JVar decl = tryBlock.body().decl(kmlClass, "jaxbRootElement", JExpr.cast(kmlClass, JExpr.invoke(localUnmarshallerFile, "unmarshal").arg(saxsource)));
    tryBlock.body()._return(decl);
    tryBlock._catch(saxExceptionClass.boxify()).body().directStatement("_x.printStackTrace();");
    tryBlock._catch(parserConfigurationExceptionClass.boxify()).body().directStatement("_x.printStackTrace();");
    tryBlock._catch(jaxbExceptionClass.boxify()).body().directStatement("_x.printStackTrace();");
    generateUnMarshallerFromString.body()._return(JExpr._null());
  }
View Full Code Here

    return kmlfiles.toArray(EMPTY_KML_ARRAY);
  }
  </code></pre>
  */
  private void generateUnmarshalFromKmzMethod(final ClassOutlineImpl cc) {
    final JMethod unmarshalFromKMZ = cc.implClass.method(JMod.PUBLIC | JMod.STATIC, kmlClass.array(), "unmarshalFromKmz");
    unmarshalFromKMZ.javadoc().add("KMZ to Java\n");
    unmarshalFromKMZ.javadoc().add("Similar to the other unmarshal methods\n\n");
    unmarshalFromKMZ.javadoc().add("with the exception that it transforms a KMZ-file into a graph of Java objects. \n")
    unmarshalFromKMZ._throws(IOException.class);
    final JVar varFile = unmarshalFromKMZ.param(fileClass, "file");
    varFile.annotate(NotNull.class);
    final JVar varEMPTY_KML_ARRAY = unmarshalFromKMZ.body().decl(kmlClass.array(), "EMPTY_KML_ARRAY", JExpr.direct("new Kml[0]"));// JExpr._new(kmlClass.array().)..arg("0"));
    unmarshalFromKMZ.body()._if(varFile.invoke("getName").invoke("endsWith").arg(".kmz").not())._then()._return(varEMPTY_KML_ARRAY);
    final JVar varZip = unmarshalFromKMZ.body().decl(zipFileClass, "zip",JExpr._new(zipFileClass).arg(varFile) );
    final JVar varEntries = unmarshalFromKMZ.body().decl(enumerationClass.boxify().narrow(zipEntryClass.boxify().wildcard()), "entries", varZip.invoke("entries"));
    unmarshalFromKMZ.body()._if(varFile.invoke("exists").not())._then()._return(varEMPTY_KML_ARRAY);
    final JVar varKmlFiles = unmarshalFromKMZ.body().decl(arrayListClass.boxify().narrow(kmlClass.boxify()), "kmlfiles", JExpr._new(arrayListClass.boxify().narrow(kmlClass.boxify())));
    final JBlock while1 = unmarshalFromKMZ.body()._while(varEntries.invoke("hasMoreElements")).body();
    final JVar varEntry = while1.decl(zipEntryClass, "entry",JExpr.cast(zipEntryClass, varEntries.invoke("nextElement")));
    while1._if(varEntry.invoke("getName").invoke("contains").arg("__MACOSX").cor(varEntry.invoke("getName").invoke("contains").arg(".DS_STORE")))._then()._continue();
    final JVar entryName = while1.decl(stringClass, "entryName", urlDecoderClass.boxify().staticInvoke("decode").arg(varEntry.invoke("getName")).arg("UTF-8"));
    while1._if(entryName.invoke("endsWith").arg(".kml").not())._then()._continue();
    final JVar varIn = while1.decl(inputStreamClass, "in", varZip.invoke("getInputStream").arg(varEntry));
    final JVar varUnmarshal = while1.decl(kmlClass, "unmarshal", kmlClass.boxify().staticInvoke("unmarshal").arg(varIn));
    while1.add(varKmlFiles.invoke("add").arg(varUnmarshal));
    unmarshalFromKMZ.body().add(varZip.invoke("close"));
    unmarshalFromKMZ.body()._return(varKmlFiles.invoke("toArray").arg(varEMPTY_KML_ARRAY));
   
//    JVar varOut = unmarshalFromKMZ.body().decl(zipEntryClass, "out", JExpr._new(zipOutputStreamClass).arg(varName));
   
  }
View Full Code Here

    //
    // out.close();
    // missingNameCounter = 1;
    // return false;
    // }
    final JMethod generateMarshalKmz = cc.implClass.method(JMod.PUBLIC, cc.implClass.owner().BOOLEAN, "marshalAsKmz");
    final JVar varName = generateMarshalKmz.param(stringClass, "name");
    varName.annotate(NotNull.class);
    final JVar varAdditionalFiles = generateMarshalKmz.varParam(kmlClass.boxify(), "additionalFiles");
    generateMarshalKmz._throws(IOException.class);
    final JVar varOut = generateMarshalKmz.body().decl(zipOutputStreamClass, "out", JExpr._new(zipOutputStreamClass).arg(JExpr._new(fileOutputStreamClass).arg(varName)));
    generateMarshalKmz.body().add(varOut.invoke("setComment").arg("KMZ-file created with Java API for KML. Visit us: http://code.google.com/p/javaapiforkml/"));
    generateMarshalKmz.body().add(JExpr._this().invoke(createAddToKmzFile).arg(JExpr._this()).arg(varOut).arg(JExpr.TRUE));
    final JForEach forEach = generateMarshalKmz.body().forEach(kmlClass, "kml", varAdditionalFiles);
    forEach.body().add(JExpr._this().invoke(createAddToKmzFile).arg(forEach.var()).arg(varOut).arg(JExpr.FALSE));
    generateMarshalKmz.body().add(varOut.invoke("close"));
    generateMarshalKmz.body().assign(varMissingNameCounter,JExpr.lit(1));
    generateMarshalKmz.body()._return(JExpr.FALSE);
  }
View Full Code Here

    generateMarshalKmz.body().assign(varMissingNameCounter,JExpr.lit(1));
    generateMarshalKmz.body()._return(JExpr.FALSE);
  }

  private void generateUnmarshalMethodPlain(final ClassOutlineImpl cc, final JClass invokeMarshalWith) {
    final JMethod generateUnMarshallerFromString = cc.implClass.method(JMod.PUBLIC | JMod.STATIC, kmlClass, "unmarshal");
    generateUnMarshallerFromString.javadoc().add("KML to Java\n");
    generateUnMarshallerFromString.javadoc().add("Similar to the other unmarshal methods \n\n");
    generateUnMarshallerFromString.javadoc().add(
        "with the exception that it transforms a " + invokeMarshalWith.name() + " into a graph of Java objects. \n");
    generateUnMarshallerFromString.javadoc().trimToSize();
    final JVar stringunmarshallVar = generateUnMarshallerFromString.param(JMod.FINAL, invokeMarshalWith, "content");

    final JTryBlock tryStringBlock = generateUnMarshallerFromString.body()._try();
    final JVar localUnmarshaller = tryStringBlock.body().decl(jaxbUnmarshallerClass, "unmarshaller",
        jaxbContextClass.boxify().staticInvoke("newInstance").arg(JExpr.direct("Kml.class")).invoke("createUnmarshaller"));
   
    final JVar inputsource = tryStringBlock.body().decl(inputsourceClass, "input", JExpr._new(inputsourceClass).arg(stringunmarshallVar));
    final JVar saxsource = tryStringBlock.body().decl(saxsourceClass, "saxSource", JExpr._new(saxsourceClass).arg(JExpr._new(namespaceFilterXMLReaderclass).arg(JExpr.FALSE)).arg(inputsource));
    final JVar decl = tryStringBlock.body().decl(kmlClass, "jaxbRootElement", JExpr.cast(kmlClass, JExpr.invoke(localUnmarshaller, "unmarshal").arg(saxsource)));
    tryStringBlock.body()._return(decl);
    tryStringBlock._catch(saxExceptionClass.boxify()).body().directStatement("_x.printStackTrace();");
    tryStringBlock._catch(parserConfigurationExceptionClass.boxify()).body().directStatement("_x.printStackTrace();");
    tryStringBlock._catch(jaxbExceptionClass.boxify()).body().directStatement("_x.printStackTrace();");
    generateUnMarshallerFromString.body()._return(JExpr._null());   
  }
View Full Code Here

    tryStringBlock._catch(jaxbExceptionClass.boxify()).body().directStatement("_x.printStackTrace();");
    generateUnMarshallerFromString.body()._return(JExpr._null());   
  }

  private void generateUnmarshalMethodFromString(final ClassOutlineImpl cc, final JType invokeMarshalWith) {
    final JMethod generateUnMarshallerFromString = cc.implClass.method(JMod.PUBLIC | JMod.STATIC, kmlClass, "unmarshal");
    generateUnMarshallerFromString.javadoc().add("KML to Java\n");
    generateUnMarshallerFromString.javadoc().add("Similar to the other unmarshal methods \n\n");
    generateUnMarshallerFromString.javadoc().add("with the exception that it transforms a String into a graph of Java objects. \n");
    generateUnMarshallerFromString.javadoc().trimToSize();
    final JVar stringunmarshallVar = generateUnMarshallerFromString.param(JMod.FINAL, stringClass, "content");

    final JTryBlock tryStringBlock = generateUnMarshallerFromString.body()._try();
    final JVar localUnmarshaller = tryStringBlock.body().decl(jaxbUnmarshallerClass, "unmarshaller",
        jaxbContextClass.boxify().staticInvoke("newInstance").arg(JExpr.direct("Kml.class")).invoke("createUnmarshaller"));
   
    final JVar inputsource = tryStringBlock.body().decl(inputsourceClass, "input", JExpr._new(inputsourceClass).arg(JExpr._new(stringReaderClass).arg(stringunmarshallVar)));
    final JVar saxsource = tryStringBlock.body().decl(saxsourceClass, "saxSource", JExpr._new(saxsourceClass).arg(JExpr._new(namespaceFilterXMLReaderclass).arg(JExpr.FALSE)).arg(inputsource));
    final JVar decl = tryStringBlock.body().decl(kmlClass, "jaxbRootElement", JExpr.cast(kmlClass, JExpr.invoke(localUnmarshaller, "unmarshal").arg(saxsource)));
    tryStringBlock.body()._return(decl);
    tryStringBlock._catch(saxExceptionClass.boxify()).body().directStatement("_x.printStackTrace();");
    tryStringBlock._catch(parserConfigurationExceptionClass.boxify()).body().directStatement("_x.printStackTrace();");
    tryStringBlock._catch(jaxbExceptionClass.boxify()).body().directStatement("_x.printStackTrace();");
    generateUnMarshallerFromString.body()._return(JExpr._null());   
  }
View Full Code Here

    tryStringBlock._catch(jaxbExceptionClass.boxify()).body().directStatement("_x.printStackTrace();");
    generateUnMarshallerFromString.body()._return(JExpr._null());   
  }

  private void generateUnmarshalFileWithOptionalValidate(final ClassOutlineImpl cc, final JMethod generateValidate) {
    final JMethod generateUnMarshallerFileFile = cc.implClass.method(JMod.PUBLIC | JMod.STATIC, kmlClass, "unmarshal");
    generateUnMarshallerFileFile.javadoc().add("KML to Java\n");
    generateUnMarshallerFileFile.javadoc().add("KML given as a file object is transformed into a graph of Java objects.\n");
    generateUnMarshallerFileFile.javadoc().add("The boolean value indicates, whether the File object should be validated \n");
    generateUnMarshallerFileFile.javadoc().add("automatically during unmarshalling and be checked if the object graph meets \n");
    generateUnMarshallerFileFile.javadoc().add("all constraints defined in OGC's KML schema specification.");
    generateUnMarshallerFileFile.javadoc().trimToSize();
    final JVar fileunmarshallVar = generateUnMarshallerFileFile.param(JMod.FINAL, fileClass, "file");
    final JVar validateVar = generateUnMarshallerFileFile.param(JMod.FINAL, boolean.class, "validate");

    final JTryBlock tryBlock = generateUnMarshallerFileFile.body()._try();
    final JVar localUnmarshallerFile = tryBlock.body().decl(jaxbUnmarshallerClass, "unmarshaller",
        jaxbContextClass.boxify().staticInvoke("newInstance").arg(JExpr.direct("Kml.class")).invoke("createUnmarshaller"));

    final JConditional ifBlockFilename = tryBlock.body()._if(validateVar.eq(JExpr.TRUE));
    ifBlockFilename._then().add(kmlClass.boxify().staticInvoke(generateValidate).arg(localUnmarshallerFile));
   
    final JVar inputsource = tryBlock.body().decl(inputsourceClass, "input", JExpr._new(inputsourceClass).arg(JExpr._new(fileReaderClass).arg(fileunmarshallVar)));
    final JVar saxsource = tryBlock.body().decl(saxsourceClass, "saxSource", JExpr._new(saxsourceClass).arg(JExpr._new(namespaceFilterXMLReaderclass).arg(validateVar)).arg(inputsource));
    final JVar decl = tryBlock.body().decl(kmlClass, "jaxbRootElement", JExpr.cast(kmlClass, JExpr.invoke(localUnmarshallerFile, "unmarshal").arg(saxsource)));
    tryBlock.body()._return(decl);
    tryBlock._catch(saxExceptionClass.boxify()).body().directStatement("_x.printStackTrace();");
    tryBlock._catch(parserConfigurationExceptionClass.boxify()).body().directStatement("_x.printStackTrace();");
    tryBlock._catch(jaxbExceptionClass.boxify()).body().directStatement("_x.printStackTrace();");
    tryBlock._catch(fileNotFoundExceptionClass.boxify()).body().directStatement("_x.printStackTrace();");
    generateUnMarshallerFileFile.body()._return(JExpr._null());

    final JMethod generateUnMarshallerFile = cc.implClass.method(JMod.PUBLIC | JMod.STATIC, kmlClass, "unmarshal");
    generateUnMarshallerFile.param(JMod.FINAL, fileClass, "file");
    generateUnMarshallerFile.javadoc().add("KML to Java\n");
    generateUnMarshallerFile.javadoc().add("KML given as a file object is transformed into a graph of Java objects.\n");
    generateUnMarshallerFile.javadoc().add("Similar to the method: \n");
    generateUnMarshallerFile.javadoc().add("unmarshal(final File, final boolean) \n");
    generateUnMarshallerFile.javadoc().add("with the exception that the File object is not validated (boolean is false). ");
    generateUnMarshallerFile.javadoc().trimToSize();
    generateUnMarshallerFile.body()._return(
        kmlClass.boxify().staticInvoke(generateUnMarshallerFileFile).arg(fileunmarshallVar).arg(JExpr.FALSE));
  }
View Full Code Here

TOP

Related Classes of com.sun.codemodel.JMethod

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.