Skip to content

Commit

Permalink
[BZ-1087871] allow to specify encoding for any Kie resource
Browse files Browse the repository at this point in the history
  • Loading branch information
mariofusco committed Apr 15, 2014
1 parent a20b5c9 commit 49dfc2f
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 37 deletions.
Expand Up @@ -26,9 +26,6 @@
import java.util.Arrays;
import java.util.List;

import org.drools.core.io.impl.ClassPathResource;
import org.drools.core.io.impl.InputStreamResource;
import org.drools.core.io.impl.ReaderResource;
import org.drools.compiler.lang.DRLLexer;
import org.drools.compiler.lang.DRLParser;
import org.drools.compiler.lang.DroolsSentence;
Expand All @@ -37,6 +34,7 @@
import org.drools.compiler.lang.Location;
import org.drools.compiler.lang.descr.PackageDescr;
import org.drools.compiler.lang.dsl.DefaultExpanderResolver;
import org.drools.core.io.internal.InternalResource;
import org.kie.internal.builder.conf.LanguageLevelOption;
import org.kie.api.io.Resource;

Expand Down Expand Up @@ -150,16 +148,7 @@ public PackageDescr parse(final boolean isEditor,
final Resource resource) throws DroolsParserException, IOException {
this.resource = resource;
InputStream is = resource.getInputStream();
String encoding = null;
if (resource instanceof ClassPathResource) {
encoding = ((ClassPathResource) resource).getEncoding();
}
if (resource instanceof ReaderResource) {
encoding = ((ReaderResource) resource).getEncoding();
}
if (resource instanceof InputStreamResource) {
encoding = ((InputStreamResource) resource).getEncoding();
}
String encoding = resource instanceof InternalResource ? ((InternalResource) resource).getEncoding() : null;

lexer = buildLexer(is, encoding, languageLevel);
DRLParser parser = buildParser(lexer, languageLevel);
Expand Down
Expand Up @@ -170,42 +170,47 @@ public DummyResource(String resourceName) {

@Override
public URL getURL() throws IOException {
throw new UnsupportedOperationException("KieBuilderSetImpl.DummyResource.getURL -> TODO");
throw new UnsupportedOperationException();
}

@Override
public boolean hasURL() {
throw new UnsupportedOperationException("KieBuilderSetImpl.DummyResource.hasURL -> TODO");
throw new UnsupportedOperationException();
}

@Override
public boolean isDirectory() {
throw new UnsupportedOperationException("KieBuilderSetImpl.DummyResource.isDirectory -> TODO");
throw new UnsupportedOperationException();
}

@Override
public Collection<Resource> listResources() {
throw new UnsupportedOperationException("KieBuilderSetImpl.DummyResource.listResources -> TODO");
throw new UnsupportedOperationException();
}

@Override
public long getLastModified() {
throw new UnsupportedOperationException("KieBuilderSetImpl.DummyResource.getLastModified -> TODO");
throw new UnsupportedOperationException();
}

@Override
public long getLastRead() {
throw new UnsupportedOperationException("KieBuilderSetImpl.DummyResource.getLastRead -> TODO");
throw new UnsupportedOperationException();
}

@Override
public String getEncoding() {
throw new UnsupportedOperationException();
}

@Override
public InputStream getInputStream() throws IOException {
throw new UnsupportedOperationException("KieBuilderSetImpl.DummyResource.getInputStream -> TODO");
throw new UnsupportedOperationException();
}

@Override
public Reader getReader() throws IOException {
throw new UnsupportedOperationException("KieBuilderSetImpl.DummyResource.getReader -> TODO");
throw new UnsupportedOperationException();
}
}
}
Expand Up @@ -38,6 +38,7 @@ public class ByteArrayResource extends BaseResource
Externalizable {

private byte[] bytes;
private String encoding;

public ByteArrayResource() { }

Expand All @@ -48,25 +49,36 @@ public ByteArrayResource(byte[] bytes) {
this.bytes = bytes;
}

public ByteArrayResource(byte[] bytes, String encoding) {
this(bytes);
this.encoding = encoding;
}

@Override
public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
super.readExternal( in );
bytes = (byte[]) in.readObject();
encoding = (String) in.readObject();
}

@Override
public void writeExternal(ObjectOutput out) throws IOException {
super.writeExternal( out );
out.writeObject( bytes );
out.writeObject(this.encoding);
}

public String getEncoding() {
return this.encoding;
}

public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream( this.bytes );
}

public Reader getReader() throws IOException {
return new InputStreamReader( getInputStream() );
return encoding != null ? new InputStreamReader( getInputStream(), encoding ) : new InputStreamReader( getInputStream() );
}

@Override
Expand Down
Expand Up @@ -126,12 +126,16 @@ public ClassPathResource(String path,
}

public void writeExternal(ObjectOutput out) throws IOException {
super.writeExternal( out );
out.writeObject( this.path );
out.writeObject( this.encoding );
}

public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
super.readExternal( in );
this.path = (String) in.readObject();
this.encoding = (String) in.readObject();
}

/**
Expand Down
Expand Up @@ -58,7 +58,11 @@ public void writeExternal(ObjectOutput out) throws IOException {
super.writeExternal( out );
out.writeObject( descr );
}


public String getEncoding() {
return null;
}

public URL getURL() throws IOException {
throw new FileNotFoundException( "descr cannot be resolved to URL");
}
Expand Down
Expand Up @@ -41,6 +41,7 @@
public class FileSystemResource extends BaseResource implements InternalResource, Externalizable {
private File file;
private long lastRead = -1;
private String encoding;

public FileSystemResource() {

Expand All @@ -65,16 +66,10 @@ public FileSystemResource(File file) {
setSourcePath( file.getName() );
setResourceType( ResourceType.determineResourceType( getSourcePath() ) );
}

public void writeExternal(ObjectOutput out) throws IOException {
super.writeExternal( out );
out.writeObject( this.file );
}

public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
super.readExternal( in );
this.file = (File) in.readObject();
public FileSystemResource(File file, String encoding) {
this(file);
this.encoding = encoding;
}

/**
Expand All @@ -95,7 +90,29 @@ public FileSystemResource(String path) {
setSourcePath( path );
setResourceType( ResourceType.determineResourceType( getSourcePath() ) );
}


public FileSystemResource(String path, String encoding) {
this(path);
this.encoding = encoding;
}

public void writeExternal(ObjectOutput out) throws IOException {
super.writeExternal( out );
out.writeObject(this.file);
out.writeObject(this.encoding);
}

public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
super.readExternal( in );
this.file = (File) in.readObject();
this.encoding = (String) in.readObject();
}

public String getEncoding() {
return this.encoding;
}

/**
* This implementation opens a FileInputStream for the underlying file.
* @see java.io.FileInputStream
Expand All @@ -104,9 +121,8 @@ public InputStream getInputStream() throws IOException {
this.lastRead = getLastModified();
return new FileInputStream(this.file);
}

public Reader getReader() throws IOException {
return new InputStreamReader( getInputStream() );
public Reader getReader() throws IOException {
return encoding != null ? new InputStreamReader( getInputStream(), encoding ) : new InputStreamReader( getInputStream() );
}

public File getFile() {
Expand Down
Expand Up @@ -57,6 +57,11 @@ public Resource newByteArrayResource(byte[] bytes) {
return new ByteArrayResource( bytes );
}

public Resource newByteArrayResource(byte[] bytes,
String encoding) {
return new ByteArrayResource( bytes, encoding );
}

public Resource newClassPathResource(String path) {
return new ClassPathResource( path );
}
Expand Down Expand Up @@ -99,10 +104,20 @@ public Resource newFileSystemResource(File file) {
return new FileSystemResource( file );
}

public Resource newFileSystemResource(File file,
String encoding) {
return new FileSystemResource( file, encoding );
}

public Resource newFileSystemResource(String fileName) {
return new FileSystemResource( fileName );
}

public Resource newFileSystemResource(String fileName,
String encoding) {
return new FileSystemResource( fileName, encoding );
}

public Resource newInputStreamResource(InputStream stream) {
return new InputStreamResource( stream );
}
Expand All @@ -127,10 +142,20 @@ public Resource newUrlResource(URL url) {
return new UrlResource( url );
}

public Resource newUrlResource(URL url,
String encoding) {
return new UrlResource( url, encoding );
}

public Resource newUrlResource(String path) {
return new UrlResource( path );
}

public Resource newUrlResource(String path,
String encoding) {
return new UrlResource( path, encoding );
}

public Resource newDescrResource( KieDescr descr ) {
return new DescrResource( descr );
}
Expand Down
Expand Up @@ -66,6 +66,7 @@ public class UrlResource extends BaseResource
private String basicAuthentication = "disabled";
private String username = "";
private String password = "";
private String encoding;

private static final String DROOLS_RESOURCE_URLTIMEOUT = "drools.resource.urltimeout";
private static final int DEFAULT_TIMEOUT = 10000; // 10 seconds
Expand All @@ -82,6 +83,11 @@ public UrlResource(URL url) {
setResourceType(ResourceType.determineResourceType(this.url.getPath()));
}

public UrlResource(URL url, String encoding) {
this(url);
this.encoding = encoding;
}

public UrlResource(String path) {
try {
this.url = getCleanedUrl(new URL(path),
Expand All @@ -94,13 +100,26 @@ public UrlResource(String path) {
}
}

public UrlResource(String path, String encoding) {
this(path);
this.encoding = encoding;
}

public void writeExternal(ObjectOutput out) throws IOException {
super.writeExternal( out );
out.writeObject(this.url);
out.writeObject(this.encoding);
}

public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
super.readExternal( in );
this.url = (URL) in.readObject();
this.encoding = (String) in.readObject();
}

public String getEncoding() {
return this.encoding;
}

public String getBasicAuthentication() {
Expand Down Expand Up @@ -240,7 +259,7 @@ private InputStream grabStream() throws IOException {
}

public Reader getReader() throws IOException {
return new InputStreamReader(getInputStream());
return encoding != null ? new InputStreamReader( getInputStream(), encoding ) : new InputStreamReader( getInputStream() );
}

/**
Expand Down
Expand Up @@ -64,4 +64,6 @@ public interface InternalResource extends Resource {
void addCategory( String category );

byte[] getBytes();

String getEncoding();
}

0 comments on commit 49dfc2f

Please sign in to comment.