Browse Source

checkstyle

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@274812 13f79535-47bb-0310-9956-ffa450edef68
master
Conor MacNeill 22 years ago
parent
commit
a4d5d0df1c
2 changed files with 99 additions and 82 deletions
  1. +65
    -56
      src/main/org/apache/tools/ant/taskdefs/email/MimeMailer.java
  2. +34
    -26
      src/main/org/apache/tools/ant/types/Description.java

+ 65
- 56
src/main/org/apache/tools/ant/taskdefs/email/MimeMailer.java View File

@@ -67,12 +67,17 @@ import java.util.Properties;
import java.util.StringTokenizer;
import java.util.Vector;
import java.security.Security;
import java.security.Provider;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;

import javax.mail.*;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
@@ -89,8 +94,9 @@ import org.apache.tools.ant.BuildException;
* @since Ant 1.5
*/
public class MimeMailer extends Mailer {
// Default character set
private static final String defaultCharset = System.getProperty("file.encoding");
/** Default character set */
private static final String DEFAULT_CHARSET
= System.getProperty("file.encoding");

// To work poperly with national charsets we have to use
// implementation of interface javax.activation.DataSource
@@ -98,40 +104,41 @@ public class MimeMailer extends Mailer {
* @since Ant 1.6
*/
class StringDataSource implements javax.activation.DataSource {
private String data=null;
private String type=null;
private String data = null;
private String type = null;
private String charset = null;
private ByteArrayOutputStream out;

public InputStream getInputStream() throws IOException {
if(data == null && out == null)
if (data == null && out == null) {
throw new IOException("No data");
else {
if(out!=null) {
data=(data!=null)?data.concat(out.toString(charset)):out.toString(charset);
out=null;
} else {
if (out != null) {
data = (data != null) ? data.concat(out.toString(charset)) : out.toString(charset);
out = null;
}
return new ByteArrayInputStream(data.getBytes(charset));
}
}

public OutputStream getOutputStream() throws IOException {
if(out==null) {
out=new ByteArrayOutputStream();
if (out == null) {
out = new ByteArrayOutputStream();
}
return out;
}

public void setContentType(String type) {
this.type=type.toLowerCase();
this.type = type.toLowerCase();
}

public String getContentType() {
if(type !=null && type.indexOf("charset")>0 && type.startsWith("text/"))
if (type != null && type.indexOf("charset") > 0 && type.startsWith("text/")) {
return type;
}
// Must be like "text/plain; charset=windows-1251"
return type!=null?type.concat("; charset=".concat(charset)):
"text/plain".concat("; charset=".concat(charset));
return type != null ? type.concat("; charset=".concat(charset))
: "text/plain".concat("; charset=".concat(charset));
}

public String getName() {
@@ -160,24 +167,25 @@ public class MimeMailer extends Mailer {
Authenticator auth;
if (SSL) {
try {
java.security.Provider p=(java.security.Provider)Class.forName( "com.sun.net.ssl.internal.ssl.Provider").newInstance();
Provider p
= (Provider) Class.forName("com.sun.net.ssl.internal.ssl.Provider").newInstance();
Security.addProvider(p);
}
catch (Exception e) {
throw new BuildException("could not instantiate ssl security provider, check that you have JSSE in your classpath");
} catch (Exception e) {
throw new BuildException("could not instantiate ssl "
+ "security provider, check that you have JSSE in "
+ "your classpath");
}
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
// SMTP provider
props.put( "mail.smtp.socketFactory.class", SSL_FACTORY);
props.put( "mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
props.put("mail.smtp.socketFactory.fallback", "false");
}
if (user==null && password == null) {
if (user == null && password == null) {
sesh = Session.getDefaultInstance(props, null);
}
else {
} else {
props.put("mail.smtp.auth", "true");
auth = new SimpleAuthenticator(user,password);
sesh = Session.getInstance(props,auth);
auth = new SimpleAuthenticator(user, password);
sesh = Session.getInstance(props, auth);
}
//create the message
MimeMessage msg = new MimeMessage(sesh);
@@ -202,18 +210,17 @@ public class MimeMailer extends Mailer {
// Choosing character set of the mail message
// First: looking it from MimeType
String charset = parseCharSetFromMimeType(message.getMimeType());
if(charset!=null) {
// Assign/reassign message charset from MimeType
if (charset != null) {
// Assign/reassign message charset from MimeType
message.setCharset(charset);
}
// Next: looking if charset having explict definition
else {
charset = message.getCharset();
if(charset==null) {
// Using default
charset=defaultCharset;
message.setCharset(charset);
}
} else {
// Next: looking if charset having explict definition
charset = message.getCharset();
if (charset == null) {
// Using default
charset = DEFAULT_CHARSET;
message.setCharset(charset);
}
}

// Using javax.activation.DataSource paradigm
@@ -221,8 +228,9 @@ public class MimeMailer extends Mailer {
sds.setContentType(message.getMimeType());
sds.setCharset(charset);

if (subject != null)
msg.setSubject(subject,charset);
if (subject != null) {
msg.setSubject(subject, charset);
}
msg.addHeader("Date", getDate());

PrintStream out = new PrintStream(sds.getOutputStream());
@@ -283,27 +291,28 @@ public class MimeMailer extends Mailer {

}

private String parseCharSetFromMimeType(String type){
int pos;
if(type==null || (pos=type.indexOf("charset"))<0)
return null;
// Assuming mime type in form "text/XXXX; charset=XXXXXX"
StringTokenizer token = new StringTokenizer(type.substring(pos),"=; ");
token.nextToken();// Skip 'charset='
return token.nextToken();
private String parseCharSetFromMimeType(String type) {
int pos;
if (type == null || (pos = type.indexOf("charset")) < 0) {
return null;
}
// Assuming mime type in form "text/XXXX; charset=XXXXXX"
StringTokenizer token = new StringTokenizer(type.substring(pos), "=; ");
token.nextToken(); // Skip 'charset='
return token.nextToken();
}
static class SimpleAuthenticator extends Authenticator {
private String user=null;
private String password=null;

static class SimpleAuthenticator extends Authenticator {
private String user = null;
private String password = null;
public SimpleAuthenticator(String user, String password) {
this.user=user;
this.password=password;
this.user = user;
this.password = password;
}
public PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication(user, password);

}
}}
}
}


+ 34
- 26
src/main/org/apache/tools/ant/types/Description.java View File

@@ -54,7 +54,11 @@

package org.apache.tools.ant.types;

import org.apache.tools.ant.*;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.UnknownElement;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.helper.ProjectHelperImpl;

import java.util.Vector;
@@ -65,7 +69,7 @@ import java.util.Vector;
* (that is, a description that applies to a buildfile as a whole).
* If present, the &lt;description&gt; element is printed out before the
* target descriptions.
*
*
* Description has no attributes, only text. There can only be one
* project description per project. A second description element will
* overwrite the first.
@@ -79,11 +83,13 @@ public class Description extends DataType {

/**
* Adds descriptive text to the project.
*
* @param text the descriptive text
*/
public void addText(String text) {

ProjectHelper ph=ProjectHelper.getProjectHelper();
if( ! ( ph instanceof ProjectHelperImpl )) {
ProjectHelper ph = ProjectHelper.getProjectHelper();
if (!(ph instanceof ProjectHelperImpl)) {
// New behavior for delayed task creation. Description
// will be evaluated in Project.getDescription()
return;
@@ -97,41 +103,43 @@ public class Description extends DataType {
}

public static String getDescription(Project project) {
StringBuffer description=new StringBuffer();
Vector targets=(Vector)project.getReference( "ant.targets");
for( int i=0; i<targets.size(); i++ ) {
Target t=(Target)targets.elementAt(i);
StringBuffer description = new StringBuffer();
Vector targets = (Vector) project.getReference("ant.targets");
for (int i = 0; i < targets.size(); i++) {
Target t = (Target) targets.elementAt(i);
concatDescriptions(project, t, description);
}
return description.toString();
}

private static void concatDescriptions(Project project, Target t,
StringBuffer description)
{
if( t==null ) return;
Vector tasks= findElementInTarget(project, t, "description");
if( tasks==null ) return;
for( int i=0; i<tasks.size(); i++ ) {
Task task=(Task)tasks.elementAt(i);
if( ! ( task instanceof UnknownElement)) {
StringBuffer description) {
if (t == null) {
return;
}
Vector tasks = findElementInTarget(project, t, "description");
if (tasks == null) {
return;
}
for (int i = 0; i < tasks.size(); i++) {
Task task = (Task) tasks.elementAt(i);
if (!(task instanceof UnknownElement)) {
continue;
}
UnknownElement ue=((UnknownElement)task);
StringBuffer descComp=ue.getWrapper().getText();
if( descComp != null ) {
description.append( descComp );
UnknownElement ue = ((UnknownElement) task);
StringBuffer descComp = ue.getWrapper().getText();
if (descComp != null) {
description.append(descComp);
}
}
}

private static Vector findElementInTarget(Project project,
Target t, String name )
{
Task tasks[]=t.getTasks();
Vector elems=new Vector();
for( int i=0; i<tasks.length; i++ ) {
if( name.equals( tasks[i].getTaskName()) ) {
Target t, String name) {
Task[] tasks = t.getTasks();
Vector elems = new Vector();
for (int i = 0; i < tasks.length; i++) {
if (name.equals(tasks[i].getTaskName())) {
elems.addElement(tasks[i]);
}
}


Loading…
Cancel
Save