Révision 561
| tmp/org.txm.cah.core/src/org/txm/cah/core/messages/AHCCoreMessages.java (revision 561) | ||
|---|---|---|
| 1 | 1 |
package org.txm.cah.core.messages; |
| 2 | 2 |
|
| 3 |
import org.eclipse.osgi.util.NLS; |
|
| 4 | 3 |
import org.txm.utils.messages.Utf8NLS; |
| 5 | 4 |
|
| 6 |
public class AHCCoreMessages extends NLS {
|
|
| 7 |
|
|
| 8 |
private static final String BUNDLE_NAME = "org.txm.cah.core.messages.messages"; //$NON-NLS-1$ |
|
| 9 |
|
|
| 5 |
public class AHCCoreMessages extends Utf8NLS {
|
|
| 10 | 6 |
|
| 7 |
static {
|
|
| 8 |
Utf8NLS.initializeMessages(AHCCoreMessages.class); |
|
| 9 |
} |
|
| 10 |
|
|
| 11 | 11 |
public static String RESULT_TYPE; |
| 12 | 12 |
|
| 13 | 13 |
public static String ERROR_EXPORT_TXT; |
| 14 | 14 |
public static String LOG_COMPUTE_CAH; |
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
static {
|
|
| 19 |
// initialize resource bundle |
|
| 20 |
Utf8NLS.initializeMessages(BUNDLE_NAME, AHCCoreMessages.class); |
|
| 21 |
} |
|
| 22 |
|
|
| 23 |
private AHCCoreMessages() {
|
|
| 24 |
} |
|
| 25 | 15 |
} |
| tmp/org.txm.internalview.rcp/src/org/txm/internal/rcp/editors/InternalViewEditor.java (revision 561) | ||
|---|---|---|
| 35 | 35 |
import org.txm.internalview.rcp.Messages; |
| 36 | 36 |
import org.txm.rcp.IImageKeys; |
| 37 | 37 |
import org.txm.rcp.editors.TXMEditorPart; |
| 38 |
import org.txm.rcp.editors.TXMEditorToolBar; |
|
| 38 | 39 |
import org.txm.rcp.editors.TXMResultEditorInput; |
| 39 | 40 |
import org.txm.rcp.editors.TableKeyListener; |
| 40 | 41 |
import org.txm.rcp.swt.widget.NavigationWidget; |
| ... | ... | |
| 105 | 106 |
return false; |
| 106 | 107 |
} |
| 107 | 108 |
|
| 108 |
public void updateParameters() {
|
|
| 109 |
final StructuralUnit struct = internalView.getAvailableStructs().get(this.structCombo.getSelectionIndex()); |
|
| 110 |
final List<Property> properties = propSelector.getProperties(); |
|
| 111 |
final List<StructuralUnitProperty> supList = new ArrayList<StructuralUnitProperty>(); |
|
| 112 |
List<StructuralUnitProperty> pList = structSelector.getProperties(); |
|
| 113 |
for (Property property : pList) if (property instanceof StructuralUnitProperty) supList.add((StructuralUnitProperty) property); |
|
| 114 |
|
|
| 115 |
Integer currentPage = internalView.getCurrentSegmentNo(); |
|
| 116 |
if (currentPage == null) currentPage = 0; |
|
| 117 |
internalView.setParameters(properties, struct, supList, currentPage); |
|
| 118 |
} |
|
| 119 |
|
|
| 120 | 109 |
@Override |
| 121 |
public void refresh(boolean update) {
|
|
| 110 |
public void updateEditorFromResult(boolean update) {
|
|
| 122 | 111 |
|
| 123 | 112 |
// JobHandler jobhandler = new JobHandler(Messages.InternalViewEditor_1) {
|
| 124 | 113 |
// @Override |
| ... | ... | |
| 187 | 176 |
*/ |
| 188 | 177 |
@Override |
| 189 | 178 |
public void createPartControl(Composite parent) {
|
| 179 |
super.createPartControl(parent); |
|
| 180 |
|
|
| 190 | 181 |
this.setTitleImage(IImageKeys.getImage(AdapterFactory.ACTION_INTERNALVIEW)); |
| 191 |
parent.setLayout(new GridLayout(1,true)); |
|
| 192 |
|
|
| 193 |
formPanel = new Composite(parent, SWT.NONE); |
|
| 182 |
|
|
| 183 |
formPanel = getCommandParametersGroup(); |
|
| 194 | 184 |
formPanel.setLayout(new GridLayout(5,false)); |
| 195 | 185 |
formPanel.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false)); |
| 196 |
navigationPanel = new Composite(parent, SWT.NONE); |
|
| 186 |
|
|
| 187 |
//TXMEditorToolBar navigationPanelToolBar = new TXMEditorToolBar(this.getTopToolBarContainer(), SWT.FLAT | SWT.RIGHT, "top", this); |
|
| 188 |
|
|
| 189 |
navigationPanel = getTopToolbar().installGroup("navigation", "Navigation", null, true);
|
|
| 197 | 190 |
navigationPanel.setLayout(new GridLayout(4,false)); |
| 198 | 191 |
navigationPanel.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false)); |
| 199 |
resultPanel = new Composite(parent, SWT.NONE); |
|
| 192 |
|
|
| 193 |
resultPanel = getResultArea(); |
|
| 200 | 194 |
resultPanel.setLayout(new GridLayout(1,true)); |
| 201 |
resultPanel.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true)); |
|
| 195 |
//resultPanel.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
|
|
| 202 | 196 |
|
| 203 | 197 |
Label structComboLabel = new Label(formPanel, SWT.NONE); |
| 204 | 198 |
structComboLabel.setText(Messages.InternalViewEditor_4); |
| ... | ... | |
| 211 | 205 |
internalView.setStructuralUnit(internalView.getAvailableStructs().get(structCombo.getSelectionIndex())); |
| 212 | 206 |
try {
|
| 213 | 207 |
internalView.getFirst(); |
| 214 |
fillResultArea();
|
|
| 208 |
compute(true);
|
|
| 215 | 209 |
} catch (CqiClientException e1) {
|
| 216 | 210 |
System.out.println("Error: "+e1.getLocalizedMessage());
|
| 217 | 211 |
Log.printStackTrace(e1); |
| ... | ... | |
| 221 | 215 |
@Override |
| 222 | 216 |
public void widgetDefaultSelected(SelectionEvent e) {}
|
| 223 | 217 |
}); |
| 224 |
propSelector = new PropertiesSelector(formPanel, SWT.NONE); |
|
| 218 |
propSelector = new PropertiesSelector<Property>(formPanel, SWT.NONE);
|
|
| 225 | 219 |
propSelector.setLayoutData(new GridData(GridData.CENTER, GridData.CENTER, false, true)); |
| 226 | 220 |
propSelector.setLayout(new GridLayout(3, false)); |
| 227 | 221 |
propSelector.addSelectionListener(new SelectionListener() {
|
| 228 | 222 |
@Override |
| 229 | 223 |
public void widgetSelected(SelectionEvent e) {
|
| 230 | 224 |
internalView.setParameters(propSelector.getProperties(), null, null, null); |
| 231 |
try {
|
|
| 232 |
fillResultArea(); |
|
| 233 |
} catch (CqiClientException e1) {
|
|
| 234 |
System.out.println("Error: "+e1.getLocalizedMessage());
|
|
| 235 |
Log.printStackTrace(e1); |
|
| 236 |
} |
|
| 225 |
compute(true); |
|
| 237 | 226 |
} |
| 238 | 227 |
|
| 239 | 228 |
@Override |
| 240 | 229 |
public void widgetDefaultSelected(SelectionEvent e) {}
|
| 241 | 230 |
}); |
| 242 | 231 |
|
| 243 |
structSelector = new PropertiesSelector(formPanel, SWT.NONE); |
|
| 232 |
structSelector = new PropertiesSelector<StructuralUnitProperty>(formPanel, SWT.NONE);
|
|
| 244 | 233 |
structSelector.setLayoutData(new GridData(GridData.CENTER, GridData.CENTER, false, true)); |
| 245 | 234 |
structSelector.setLayout(new GridLayout(3, false)); |
| 246 | 235 |
structSelector.setText(Messages.InternalViewEditor_5); |
| ... | ... | |
| 357 | 346 |
navigation.setNextEnabled(!internalView.isLast()); |
| 358 | 347 |
navigation.setLastEnabled(true); |
| 359 | 348 |
} catch (CqiClientException e1) {
|
| 360 |
// TODO Auto-generated catch block |
|
| 361 | 349 |
org.txm.rcp.utils.Logger.printStackTrace(e1); |
| 362 | 350 |
} |
| 363 | 351 |
} |
| ... | ... | |
| 379 | 367 |
navigation.setNextEnabled(false); |
| 380 | 368 |
navigation.setLastEnabled(false); |
| 381 | 369 |
} catch (CqiClientException e1) {
|
| 382 |
// TODO Auto-generated catch block |
|
| 383 | 370 |
org.txm.rcp.utils.Logger.printStackTrace(e1); |
| 384 | 371 |
} |
| 385 | 372 |
} |
| ... | ... | |
| 421 | 408 |
} |
| 422 | 409 |
if (inputElement instanceof HashMap<?,?>) {
|
| 423 | 410 |
HashMap<Property, List<String>> l = (HashMap<Property, List<String>>) inputElement; |
| 424 |
for(int i = 0 ; i < props.size() ; i++) {
|
|
| 411 |
for (int i = 0 ; i < props.size() ; i++) {
|
|
| 425 | 412 |
Property prop = props.get(i); |
| 426 | 413 |
List<String> data = l.get(prop); |
| 427 | 414 |
for (int j = 0 ; j < nlines ; j++) {
|
| ... | ... | |
| 463 | 450 |
} |
| 464 | 451 |
|
| 465 | 452 |
private void reloadStructuralUnitProperties() {
|
| 466 |
|
|
| 467 | 453 |
structSelector.setProperties(internalView.getAvailableStructuralUnitProperties(), internalView.getStructuralUnitProperties()); |
| 468 | 454 |
} |
| 469 | 455 |
|
| ... | ... | |
| 672 | 658 |
org.txm.rcp.utils.Logger.printStackTrace(e); |
| 673 | 659 |
} |
| 674 | 660 |
} |
| 661 |
|
|
| 662 |
@Override |
|
| 663 |
public void updateResultFromEditor() {
|
|
| 664 |
final StructuralUnit struct = internalView.getAvailableStructs().get(this.structCombo.getSelectionIndex()); |
|
| 665 |
final List<Property> properties = propSelector.getProperties(); |
|
| 666 |
final List<StructuralUnitProperty> supList = new ArrayList<StructuralUnitProperty>(); |
|
| 667 |
List<StructuralUnitProperty> pList = structSelector.getProperties(); |
|
| 668 |
for (Property property : pList) if (property instanceof StructuralUnitProperty) supList.add((StructuralUnitProperty) property); |
|
| 669 |
|
|
| 670 |
Integer currentPage = internalView.getCurrentSegmentNo(); |
|
| 671 |
if (currentPage == null) currentPage = 0; |
|
| 672 |
internalView.setParameters(properties, struct, supList, currentPage); |
|
| 673 |
} |
|
| 675 | 674 |
} |
| tmp/org.txm.annotation.core/src/org/txm/annotation/core/repository/Messages.java (revision 561) | ||
|---|---|---|
| 1 | 1 |
package org.txm.annotation.core.repository; |
| 2 | 2 |
|
| 3 |
import org.eclipse.osgi.util.NLS;
|
|
| 3 |
import org.txm.utils.messages.Utf8NLS;
|
|
| 4 | 4 |
|
| 5 |
public class Messages extends NLS {
|
|
| 6 |
private static final String BUNDLE_NAME = "org.txm.annotation.repository.messages"; //$NON-NLS-1$ |
|
| 5 |
public class Messages extends Utf8NLS {
|
|
| 7 | 6 |
static {
|
| 8 |
// initialize resource bundle |
|
| 9 |
NLS.initializeMessages(BUNDLE_NAME, Messages.class); |
|
| 7 |
Utf8NLS.initializeMessages(Messages.class); |
|
| 10 | 8 |
} |
| 11 | 9 |
|
| 12 | 10 |
private Messages() {
|
| tmp/org.txm.referencer.core/src/org/txm/functions/referencer/Referencer.java (revision 561) | ||
|---|---|---|
| 57 | 57 |
import java.util.Map; |
| 58 | 58 |
|
| 59 | 59 |
import org.txm.core.messages.TXMCoreMessages; |
| 60 |
import org.txm.core.results.Parameter; |
|
| 60 | 61 |
import org.txm.core.results.TXMParameters; |
| 61 | 62 |
import org.txm.core.results.TXMResult; |
| 62 | 63 |
import org.txm.searchengine.cqp.AbstractCqiClient; |
| ... | ... | |
| 65 | 66 |
import org.txm.searchengine.cqp.corpus.CorpusManager; |
| 66 | 67 |
import org.txm.searchengine.cqp.corpus.Property; |
| 67 | 68 |
import org.txm.searchengine.cqp.corpus.QueryResult; |
| 69 |
import org.txm.searchengine.cqp.corpus.StructuralUnitProperty; |
|
| 68 | 70 |
import org.txm.searchengine.cqp.corpus.query.Match; |
| 69 | 71 |
import org.txm.searchengine.cqp.corpus.query.Query; |
| 70 | 72 |
import org.txm.searchengine.cqp.serverException.CqiServerError; |
| ... | ... | |
| 95 | 97 |
/** The matches. */ |
| 96 | 98 |
List<Match> matches; |
| 97 | 99 |
|
| 100 |
/** The positions. */ |
|
| 101 |
int[] positions; |
|
| 102 |
|
|
| 98 | 103 |
/** The nlines. */ |
| 99 | 104 |
int nlines; |
| 100 | 105 |
|
| 101 |
/** The hierarchic sort. */ |
|
| 106 |
/** The hierarchic sort. */ |
|
| 107 |
@Parameter |
|
| 102 | 108 |
private boolean pHierarchicSort; |
| 103 |
|
|
| 104 |
/** The positions. */ |
|
| 105 |
int[] positions; |
|
| 109 |
|
|
| 110 |
/** The pattern. */ |
|
| 111 |
@Parameter |
|
| 112 |
List<StructuralUnitProperty> pPattern; |
|
| 106 | 113 |
|
| 107 |
/** The pattern. */ |
|
| 108 |
List<Property> pPattern; |
|
| 109 |
|
|
| 110 |
/** The prop. */ |
|
| 114 |
/** The prop. */ |
|
| 115 |
@Parameter |
|
| 111 | 116 |
Property pProperty; |
| 112 | 117 |
|
| 113 |
/** The query. */ |
|
| 118 |
/** The query. */ |
|
| 119 |
@Parameter |
|
| 114 | 120 |
Query pQuery; |
| 115 | 121 |
|
| 116 | 122 |
/** The processed idx. */ |
| ... | ... | |
| 136 | 142 |
*/ |
| 137 | 143 |
public Referencer(Corpus corpus) {
|
| 138 | 144 |
super(corpus); |
| 139 |
}
|
|
| 145 |
} |
|
| 140 | 146 |
|
| 141 | 147 |
/** |
| 142 | 148 |
* As r matrix. |
| ... | ... | |
| 160 | 166 |
//rw.eval(symbol+"$query <- \""+this.query.getQueryString()+"\")"); |
| 161 | 167 |
rw.eval(symbol+"$refs <- list()"); //$NON-NLS-1$ |
| 162 | 168 |
int i = 1; |
| 163 |
for(Line line : this.lines) |
|
| 164 |
{
|
|
| 169 |
for (Line line : this.lines) {
|
|
| 165 | 170 |
String refs[] = line.getReferences().toArray(new String[line.getReferences().size()]); |
| 166 | 171 |
//System.out.println(line.getPropValue()+": "+Arrays.toString(refs)); |
| 167 | 172 |
rw.addVectorToWorkspace("reflinerefs", refs); //$NON-NLS-1$
|
| ... | ... | |
| 177 | 182 |
@Override |
| 178 | 183 |
public void clean() {
|
| 179 | 184 |
try {
|
| 180 |
this.writer.flush(); |
|
| 181 |
this.writer.close(); |
|
| 185 |
if (this.writer != null) {
|
|
| 186 |
this.writer.flush(); |
|
| 187 |
this.writer.close(); |
|
| 188 |
} |
|
| 182 | 189 |
} catch (IOException e) {
|
| 183 | 190 |
// TODO Auto-generated catch block |
| 184 | 191 |
org.txm.utils.logger.Log.printStackTrace(e); |
| ... | ... | |
| 190 | 197 |
*/ |
| 191 | 198 |
public void clearAllLines() |
| 192 | 199 |
{
|
| 193 |
for(Line line : lines)// we need to recompute lines' references |
|
| 194 |
{
|
|
| 200 |
for(Line line : lines) { // we need to recompute lines' references
|
|
| 195 | 201 |
line.clear(); |
| 196 | 202 |
} |
| 197 | 203 |
} |
| ... | ... | |
| 230 | 236 |
} |
| 231 | 237 |
|
| 232 | 238 |
/** |
| 233 |
* Gets the corpus. |
|
| 239 |
* Gets the parent corpus.
|
|
| 234 | 240 |
* |
| 235 | 241 |
* @return the corpus |
| 236 | 242 |
*/ |
| ... | ... | |
| 345 | 351 |
* |
| 346 | 352 |
* @return the pattern |
| 347 | 353 |
*/ |
| 348 |
public List<Property> getPattern() {
|
|
| 354 |
public List<StructuralUnitProperty> getPattern() {
|
|
| 349 | 355 |
return pPattern; |
| 350 | 356 |
} |
| 351 | 357 |
|
| ... | ... | |
| 452 | 458 |
return true; |
| 453 | 459 |
} |
| 454 | 460 |
|
| 455 |
public void setParameters(Query query, Property prop, List<Property> pattern, Boolean hierarchicSort) {
|
|
| 461 |
public void setParameters(Query query, Property prop, List<StructuralUnitProperty> pattern, Boolean hierarchicSort) {
|
|
| 456 | 462 |
if (query != null) this.pQuery = query; |
| 457 | 463 |
if (pattern != null) this.pPattern = pattern; |
| 458 | 464 |
if (prop != null) this.pProperty = prop; |
| ... | ... | |
| 530 | 536 |
|
| 531 | 537 |
@Override |
| 532 | 538 |
public boolean canCompute() {
|
| 533 |
return getCorpus() != null; |
|
| 539 |
return getCorpus() != null |
|
| 540 |
&& pQuery != null |
|
| 541 |
&& !pQuery.isEmpty(); |
|
| 534 | 542 |
} |
| 535 | 543 |
|
| 536 |
|
|
| 537 | 544 |
@Override |
| 538 |
public boolean saveParameters() {
|
|
| 539 |
// TODO Auto-generated method stub |
|
| 540 |
System.err.println("Referencer.saveParameters(): not yet implemented.");
|
|
| 545 |
public boolean loadParameters() throws CqiClientException {
|
|
| 546 |
this.pQuery = new Query(this.getStringParameterValue(ReferencerPreferences.QUERY)); |
|
| 547 |
String s = this.getStringParameterValue(ReferencerPreferences.PATTERN); |
|
| 548 |
this.pPattern = StructuralUnitProperty.fromStringToList(getCorpus(), s); |
|
| 549 |
|
|
| 550 |
s = this.getStringParameterValue(ReferencerPreferences.PROPERTY); |
|
| 551 |
this.pProperty = getCorpus().getProperty(s); |
|
| 552 |
|
|
| 553 |
this.pHierarchicSort = this.getBooleanParameterValue(ReferencerPreferences.SORTBYFREQ); |
|
| 541 | 554 |
return true; |
| 542 | 555 |
} |
| 543 | 556 |
|
| 544 |
|
|
| 545 |
@Override |
|
| 546 |
public boolean loadParameters() {
|
|
| 547 |
// TODO Auto-generated method stub |
|
| 548 |
System.err.println("Referencer.loadParameters(): not yet implemented.");
|
|
| 549 |
return true; |
|
| 550 |
} |
|
| 551 | 557 |
|
| 552 |
|
|
| 553 | 558 |
/** |
| 554 | 559 |
* The Class Line. |
| 555 | 560 |
*/ |
| ... | ... | |
| 679 | 684 |
public String toString() {
|
| 680 | 685 |
return linepropvalue+" : <"+id+","+pProperty+"> : "+references.toString(); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 681 | 686 |
} |
| 687 |
} |
|
| 688 |
|
|
| 689 |
public void setQuery(Query query) {
|
|
| 690 |
this.pQuery = query; |
|
| 691 |
} |
|
| 692 |
|
|
| 693 |
public void setProperty(Property property) {
|
|
| 694 |
this.pProperty = property; |
|
| 695 |
} |
|
| 696 |
|
|
| 697 |
public void setPattern(List<StructuralUnitProperty> properties) {
|
|
| 698 |
this.pPattern = properties; |
|
| 682 | 699 |
} |
| 683 | 700 |
} |
| tmp/org.txm.referencer.core/src/org/txm/functions/referencer/ReferencerPreferences.java (revision 561) | ||
|---|---|---|
| 11 | 11 |
|
| 12 | 12 |
/** The Constant SORTBYFREQ. */ |
| 13 | 13 |
//FIXME: to extract to referencer plug-in |
| 14 |
public static final String SORTBYFREQ = "referencer_sorttype"; //$NON-NLS-1$
|
|
| 14 |
public static final String SORTBYFREQ = "sorttype"; //$NON-NLS-1$ |
|
| 15 | 15 |
|
| 16 |
|
|
| 17 |
public static final String QUERY = "query"; |
|
| 18 |
public static final String PROPERTY = "property"; |
|
| 19 |
public static final String PATTERN = "pattern"; |
|
| 20 |
|
|
| 16 | 21 |
@Override |
| 17 | 22 |
public void initializeDefaultPreferences() {
|
| 18 | 23 |
Preferences preferences = DefaultScope.INSTANCE.getNode(PREFERENCES_NODE); |
| 19 | 24 |
|
| 20 | 25 |
preferences.putBoolean(SORTBYFREQ, false); //$NON-NLS-1$ |
| 21 | 26 |
|
| 27 |
preferences.put(QUERY, ""); //$NON-NLS-1$ |
|
| 28 |
preferences.put(PROPERTY, "word"); //$NON-NLS-1$ |
|
| 29 |
preferences.put(PATTERN, "text_id"); //$NON-NLS-1$ |
|
| 22 | 30 |
} |
| 23 | 31 |
} |
| tmp/org.txm.wordcloud.core/src/org/txm/wordcloud/core/functions/WordCloud.java (revision 561) | ||
|---|---|---|
| 1 | 1 |
package org.txm.wordcloud.core.functions; |
| 2 | 2 |
|
| 3 | 3 |
import java.io.File; |
| 4 |
import java.util.ArrayList; |
|
| 5 | 4 |
import java.util.List; |
| 6 | 5 |
|
| 7 | 6 |
import org.txm.chartsengine.core.results.ChartResult; |
| ... | ... | |
| 9 | 8 |
import org.txm.core.results.TXMParameters; |
| 10 | 9 |
import org.txm.index.core.functions.Index; |
| 11 | 10 |
import org.txm.index.core.functions.Line; |
| 12 |
import org.txm.searchengine.cqp.corpus.Corpus; |
|
| 13 |
import org.txm.searchengine.cqp.corpus.Property; |
|
| 14 |
import org.txm.searchengine.cqp.corpus.query.Query; |
|
| 15 | 11 |
import org.txm.statsengine.r.core.RWorkspace; |
| 16 | 12 |
import org.txm.utils.logger.Log; |
| 17 | 13 |
import org.txm.wordcloud.core.messages.WordCloudCoreMessages; |
| 18 | 14 |
import org.txm.wordcloud.core.preferences.WordCloudPreferences; |
| 19 | 15 |
|
| 20 | 16 |
/** |
| 21 |
* Word cloud.
|
|
| 17 |
* WordCloud computes word clouds using the "wordcloud" R package and an Index result
|
|
| 22 | 18 |
* |
| 23 | 19 |
* @author mdecorde |
| 24 | 20 |
* @author sjacquot |
| ... | ... | |
| 29 | 25 |
//TODO: test rw.setStringEncoding("UTF-8"); // or connection
|
| 30 | 26 |
private int[] freqs; |
| 31 | 27 |
private String[] labels; |
| 32 |
private Object source; |
|
| 28 |
|
|
| 29 |
protected Integer pFMinFilter; |
|
| 30 |
|
|
| 31 |
protected Integer pVMaxFilter; |
|
| 33 | 32 |
|
| 34 |
// public WordCloud(LexicalTable table) throws RWorkspaceException, REXPMismatchException {
|
|
| 35 |
// parent = table.getPartition(); |
|
| 36 |
// if (parent == null) parent = table.getCorpus(); |
|
| 37 |
// |
|
| 38 |
// RWorkspace rw = RWorkspace.getRWorkspaceInstance(); |
|
| 39 |
// this.labels = rw.evalToString("rownames("+table.getSymbol()+")");
|
|
| 40 |
// this.freqs = rw.eval("rowSums("+table.getSymbol()+")").asIntegers();
|
|
| 41 |
// |
|
| 42 |
// removeDiacritics(); |
|
| 43 |
// } |
|
| 44 |
|
|
| 45 | 33 |
/** |
| 46 | 34 |
* |
| 47 | 35 |
* @param index |
| 48 | 36 |
*/ |
| 49 | 37 |
public WordCloud(Index index) {
|
| 50 | 38 |
super(index); |
| 51 |
this.source = index; |
|
| 52 |
//removeDiacritics(); |
|
| 53 | 39 |
} |
| 54 | 40 |
|
| 55 | 41 |
/** |
| ... | ... | |
| 57 | 43 |
* @param index |
| 58 | 44 |
*/ |
| 59 | 45 |
private void initWith(Index index) {
|
| 60 |
source = index; |
|
| 61 | 46 |
List<Line> lines = index.getAllLines(); |
| 62 | 47 |
this.freqs = new int[lines.size()]; |
| 63 | 48 |
this.labels = new String[lines.size()]; |
| ... | ... | |
| 69 | 54 |
i++; |
| 70 | 55 |
} |
| 71 | 56 |
} |
| 72 |
|
|
| 73 | 57 |
|
| 74 |
// public WordCloud(QueryIndex qindex) {
|
|
| 75 |
// parent = qindex.getCorpus(); |
|
| 76 |
// source = qindex; |
|
| 77 |
// Collection<QueryIndexLine> lines = qindex.getLines(); |
|
| 78 |
// this.freqs = new int[lines.size()]; |
|
| 79 |
// this.labels = new String[lines.size()]; |
|
| 80 |
// |
|
| 81 |
// int i = 0; |
|
| 82 |
// for (QueryIndexLine line : lines) {
|
|
| 83 |
// freqs[i] = line.getFrequency(); |
|
| 84 |
// labels[i] = line.getName(); |
|
| 85 |
// } |
|
| 86 |
// //removeDiacritics(); |
|
| 87 |
// } |
|
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 | 58 |
private void removeDiacritics() {
|
| 92 | 59 |
//for (int i = 0; i < labels.length ; i++) |
| 93 | 60 |
// labels[i] = AsciiUtils.convertNonAscii(labels[i]); |
| ... | ... | |
| 186 | 153 |
|
| 187 | 154 |
@Override |
| 188 | 155 |
public String getSimpleName() {
|
| 189 |
return WordCloudCoreMessages.RESULT_TYPE + " (" + this.getIntParameterValue(WordCloudPreferences.FMIN)
|
|
| 156 |
return WordCloudCoreMessages.RESULT_TYPE + " (" + this.pFMinFilter
|
|
| 190 | 157 |
+ " / " |
| 191 |
+ this.getIntParameterValue(WordCloudPreferences.MAX_WORDS)
|
|
| 158 |
+ this.pVMaxFilter
|
|
| 192 | 159 |
+ ")"; |
| 193 | 160 |
} |
| 194 | 161 |
|
| ... | ... | |
| 200 | 167 |
|
| 201 | 168 |
@Override |
| 202 | 169 |
public boolean canCompute() {
|
| 203 |
return source != null;
|
|
| 170 |
return parent != null && pFMinFilter != null;
|
|
| 204 | 171 |
} |
| 205 | 172 |
|
| 206 | 173 |
@Override |
| 207 |
public boolean saveParameters() {
|
|
| 208 |
// TODO Auto-generated method stub |
|
| 209 |
System.err.println("WordCloud.saveParameters(): not yet implemented.");
|
|
| 210 |
return true; |
|
| 211 |
} |
|
| 212 |
|
|
| 213 |
@Override |
|
| 214 | 174 |
public boolean setParameters(TXMParameters parameters) {
|
| 215 |
// TODO Auto-generated method stub |
|
| 216 |
System.err.println("WordCloud.setParameters(): not yet implemented.");
|
|
| 175 |
|
|
| 176 |
if (parameters.containsKey(WordCloudPreferences.FMIN)) {
|
|
| 177 |
Object value = parameters.get(WordCloudPreferences.FMIN); |
|
| 178 |
if (value instanceof String) {
|
|
| 179 |
this.pFMinFilter = Integer.parseInt(value.toString()); |
|
| 180 |
} else if (value instanceof Integer) {
|
|
| 181 |
this.pFMinFilter = (Integer)value; |
|
| 182 |
} |
|
| 183 |
} |
|
| 184 |
|
|
| 185 |
if (parameters.containsKey(WordCloudPreferences.MAX_WORDS)) {
|
|
| 186 |
Object value = parameters.get(WordCloudPreferences.MAX_WORDS); |
|
| 187 |
if (value instanceof String) {
|
|
| 188 |
this.pVMaxFilter = Integer.parseInt(value.toString()); |
|
| 189 |
} else if (value instanceof Integer) {
|
|
| 190 |
this.pVMaxFilter = (Integer)value; |
|
| 191 |
} |
|
| 192 |
} |
|
| 193 |
|
|
| 217 | 194 |
return true; |
| 218 | 195 |
} |
| 219 | 196 |
|
| ... | ... | |
| 237 | 214 |
|
| 238 | 215 |
@Override |
| 239 | 216 |
protected boolean _compute(boolean update) {
|
| 240 |
// FIXME: source must always be an Index because the corpus source produces an Index |
|
| 241 |
// from corpus |
|
| 242 |
if (source instanceof Corpus) {
|
|
| 243 |
try {
|
|
| 244 |
Corpus corpus = (Corpus) source; |
|
| 245 |
Query query; |
|
| 246 |
String lang = corpus.getLang(); |
|
| 247 |
if (corpus.getProperty(lang + "pos") != null) {
|
|
| 248 |
query = new Query(getQueryForLang(lang)); |
|
| 249 |
} |
|
| 250 |
else {
|
|
| 251 |
query = new Query("[]");
|
|
| 252 |
} |
|
| 253 |
|
|
| 254 |
System.out.println("Used query: " + query);
|
|
| 255 |
|
|
| 256 |
ArrayList<Property> props = new ArrayList<Property>(); |
|
| 257 |
props.add(corpus.getWordProperty()); |
|
| 258 |
Index index = new Index(corpus); |
|
| 259 |
index.setParameters(query, props, null, null, null, null); |
|
| 260 |
if (!query.toString().equals("[]")) {
|
|
| 261 |
corpus.addChild(index); |
|
| 262 |
} |
|
| 263 |
|
|
| 264 |
index.compute(false, monitor); |
|
| 265 |
|
|
| 266 |
index.addChild(this); |
|
| 267 |
|
|
| 268 |
List<Line> lines = index.getAllLines(); |
|
| 269 |
if (lines != null && lines.size() > 0) {
|
|
| 270 |
this.initWith(index); |
|
| 271 |
} |
|
| 272 |
else {
|
|
| 273 |
Log.severe("Error: Could not build a word selection with this corpus");
|
|
| 274 |
throw new IllegalArgumentException("Error: Index failed with corpus " + corpus + " with lang " + lang + " and query " + query);
|
|
| 275 |
} |
|
| 276 |
} |
|
| 277 |
catch(Exception e) {
|
|
| 278 |
System.out.println("Error: "+e.getLocalizedMessage());
|
|
| 279 |
Log.printStackTrace(e); |
|
| 280 |
return false; |
|
| 281 |
} |
|
| 282 |
} |
|
| 283 |
// from index |
|
| 284 |
else if(this.source instanceof Index) {
|
|
| 285 |
this.initWith((Index) this.source); |
|
| 286 |
return true; |
|
| 287 |
} |
|
| 288 |
|
|
| 217 |
// // FIXME: source must always be an Index because the corpus source produces an Index |
|
| 218 |
// // from corpus |
|
| 219 |
// if (source instanceof Corpus) {
|
|
| 220 |
// try {
|
|
| 221 |
// Corpus corpus = (Corpus) source; |
|
| 222 |
// Query query; |
|
| 223 |
// String lang = corpus.getLang(); |
|
| 224 |
// if (corpus.getProperty(lang + "pos") != null) {
|
|
| 225 |
// query = new Query(getQueryForLang(lang)); |
|
| 226 |
// } |
|
| 227 |
// else {
|
|
| 228 |
// query = new Query("[]");
|
|
| 229 |
// } |
|
| 230 |
// |
|
| 231 |
// System.out.println("Used query: " + query);
|
|
| 232 |
// |
|
| 233 |
// ArrayList<Property> props = new ArrayList<Property>(); |
|
| 234 |
// props.add(corpus.getWordProperty()); |
|
| 235 |
// Index index = new Index(corpus); |
|
| 236 |
// index.setParameters(query, props, null, null, null, null); |
|
| 237 |
// if (!query.toString().equals("[]")) {
|
|
| 238 |
// corpus.addChild(index); |
|
| 239 |
// } |
|
| 240 |
// |
|
| 241 |
// index.compute(false, monitor); |
|
| 242 |
// |
|
| 243 |
// index.addChild(this); |
|
| 244 |
// |
|
| 245 |
// List<Line> lines = index.getAllLines(); |
|
| 246 |
// if (lines != null && lines.size() > 0) {
|
|
| 247 |
// this.initWith(index); |
|
| 248 |
// } |
|
| 249 |
// else {
|
|
| 250 |
// Log.severe("Error: Could not build a word selection with this corpus");
|
|
| 251 |
// throw new IllegalArgumentException("Error: Index failed with corpus " + corpus + " with lang " + lang + " and query " + query);
|
|
| 252 |
// } |
|
| 253 |
// } |
|
| 254 |
// catch(Exception e) {
|
|
| 255 |
// System.out.println("Error: "+e.getLocalizedMessage());
|
|
| 256 |
// Log.printStackTrace(e); |
|
| 257 |
// return false; |
|
| 258 |
// } |
|
| 259 |
this.initWith((Index)parent); |
|
| 260 |
// } |
|
| 261 |
// // from index |
|
| 262 |
// else if(this.source instanceof Index) {
|
|
| 263 |
// this.initWith((Index) this.source); |
|
| 264 |
// return true; |
|
| 265 |
// } |
|
| 266 |
// |
|
| 289 | 267 |
return true; |
| 290 | 268 |
} |
| 291 | 269 |
|
| 270 |
@Override |
|
| 271 |
public boolean loadParameters() throws Exception {
|
|
| 272 |
this.pFMinFilter = this.getIntParameterValue(WordCloudPreferences.FMIN); |
|
| 273 |
this.pVMaxFilter = this.getIntParameterValue(WordCloudPreferences.MAX_WORDS); |
|
| 274 |
return false; |
|
| 275 |
} |
|
| 292 | 276 |
|
| 277 |
public Integer getFMinFilter() {
|
|
| 278 |
return pFMinFilter; |
|
| 279 |
} |
|
| 280 |
|
|
| 281 |
public Integer getVMaxFilter() {
|
|
| 282 |
return pVMaxFilter; |
|
| 283 |
} |
|
| 293 | 284 |
} |
| tmp/org.txm.internalview.core/src/org/txm/internalview/core/Messages.java (revision 561) | ||
|---|---|---|
| 1 | 1 |
package org.txm.internalview.core; |
| 2 | 2 |
|
| 3 |
import org.eclipse.osgi.util.NLS;
|
|
| 3 |
import org.txm.utils.messages.Utf8NLS;
|
|
| 4 | 4 |
|
| 5 |
public class Messages extends NLS {
|
|
| 5 |
public class Messages extends Utf8NLS {
|
|
| 6 |
|
|
| 6 | 7 |
public static String InternalView_3; |
| 7 | 8 |
public static String InternalView_6; |
| 8 | 9 |
public static String InternalView_7; |
| 10 |
|
|
| 11 |
static {
|
|
| 12 |
Utf8NLS.initializeMessages(Messages.class); |
|
| 13 |
} |
|
| 9 | 14 |
} |
| tmp/org.txm.internalview.core/src/org/txm/functions/internal/InternalView.java (revision 561) | ||
|---|---|---|
| 9 | 9 |
import java.util.List; |
| 10 | 10 |
|
| 11 | 11 |
import org.apache.commons.lang.StringUtils; |
| 12 |
import org.txm.core.results.Parameter; |
|
| 12 | 13 |
import org.txm.core.results.TXMParameters; |
| 13 | 14 |
import org.txm.core.results.TXMResult; |
| 14 | 15 |
import org.txm.internalview.core.Messages; |
| ... | ... | |
| 19 | 20 |
import org.txm.searchengine.cqp.corpus.QueryResult; |
| 20 | 21 |
import org.txm.searchengine.cqp.corpus.StructuralUnit; |
| 21 | 22 |
import org.txm.searchengine.cqp.corpus.StructuralUnitProperty; |
| 23 |
import org.txm.searchengine.cqp.corpus.WordProperty; |
|
| 22 | 24 |
import org.txm.searchengine.cqp.corpus.query.Match; |
| 23 | 25 |
import org.txm.searchengine.cqp.corpus.query.Query; |
| 24 | 26 |
import org.txm.utils.io.IOUtils; |
| ... | ... | |
| 32 | 34 |
protected List<Match> matches = new ArrayList<Match>(); |
| 33 | 35 |
protected int nmatches = 0; |
| 34 | 36 |
|
| 37 |
@Parameter |
|
| 35 | 38 |
protected Integer pCurrentPage = -1; |
| 39 |
@Parameter |
|
| 36 | 40 |
protected List<Property> pProperties; |
| 37 |
|
|
| 41 |
@Parameter |
|
| 38 | 42 |
protected List<StructuralUnitProperty> pStructProperties; |
| 43 |
@Parameter |
|
| 39 | 44 |
protected StructuralUnit pStructure; |
| 40 | 45 |
|
| 41 | 46 |
public InternalView(Corpus corpus) {
|
| ... | ... | |
| 44 | 49 |
|
| 45 | 50 |
try {
|
| 46 | 51 |
String propNames = this.getStringParameterValue(InternalViewPreferences.PROPERTIES); |
| 47 |
pProperties = Property.stringToProperties(corpus, propNames);
|
|
| 52 |
pProperties = WordProperty.fromStringToList(corpus, propNames);
|
|
| 48 | 53 |
availablesProperties = new ArrayList<Property>(corpus.getOrderedProperties()); |
| 49 | 54 |
availablesProperties.removeAll(pProperties); |
| 50 | 55 |
|
| 51 |
propNames = this.getStringParameterValue(InternalViewPreferences.STRUCTURALUNITS); |
|
| 52 | 56 |
availableStructs = corpus.getStructuralUnits(); |
| 53 |
pStructure = StructuralUnit.stringToStructuralUnits(corpus, propNames).get(0); |
|
| 54 |
availableStructs.remove(pStructure); |
|
| 57 |
propNames = this.getStringParameterValue(InternalViewPreferences.STRUCTURALUNIT); |
|
| 58 |
List<StructuralUnit> structs = StructuralUnit.stringToStructuralUnits(corpus, propNames); |
|
| 59 |
pStructure = structs.get(0); |
|
| 60 |
//availableStructs.remove(pStructure); |
|
| 55 | 61 |
|
| 56 | 62 |
propNames = this.getStringParameterValue(InternalViewPreferences.STRUCTURALUNITPROPERTIES); |
| 57 | 63 |
availableStructuralUnitProperties = pStructure.getProperties(); |
| ... | ... | |
| 88 | 94 |
pStructProperties != null && |
| 89 | 95 |
pStructProperties.size() > 0; |
| 90 | 96 |
} |
| 91 |
|
|
| 92 | 97 |
|
| 93 | 98 |
@Override |
| 94 | 99 |
public boolean saveParameters() {
|
| 95 |
// TODO Auto-generated method stub |
|
| 96 |
System.err.println("InternalView.saveParameters(): not yet implemented.");
|
|
| 100 |
|
|
| 101 |
if (pCurrentPage != null) this.setParameterValue(InternalViewPreferences.CURRENTPAGE, pCurrentPage); |
|
| 102 |
if (pStructure != null) this.setParameterValue(InternalViewPreferences.STRUCTURALUNIT, pStructure.getName()); |
|
| 103 |
if (pStructProperties != null) this.setParameterValue(InternalViewPreferences.STRUCTURALUNITPROPERTIES, StringUtils.join(pStructProperties, "\t")); |
|
| 104 |
if (pProperties != null) this.setParameterValue(InternalViewPreferences.PROPERTIES, StringUtils.join(pProperties, "\t")); |
|
| 105 |
|
|
| 97 | 106 |
return true; |
| 98 | 107 |
} |
| 99 |
|
|
| 100 | 108 |
|
| 101 | 109 |
@Override |
| 102 | 110 |
public void clean() {
|
| ... | ... | |
| 268 | 276 |
|
| 269 | 277 |
|
| 270 | 278 |
@Override |
| 271 |
public boolean loadParameters() {
|
|
| 272 |
// TODO Auto-generated method stub |
|
| 273 |
System.err.println("InternalView.loadParameters(): not yet implemented.");
|
|
| 279 |
public boolean loadParameters() throws Exception {
|
|
| 280 |
|
|
| 281 |
this.pCurrentPage = this.getIntParameterValue(InternalViewPreferences.CURRENTPAGE); |
|
| 282 |
|
|
| 283 |
String s = this.getStringParameterValue(InternalViewPreferences.STRUCTURALUNIT); |
|
| 284 |
if (s != null && s.length() > 0) |
|
| 285 |
this.pStructure = corpus.getStructuralUnit(s); |
|
| 286 |
|
|
| 287 |
s = this.getStringParameterValue(InternalViewPreferences.STRUCTURALUNITPROPERTIES); |
|
| 288 |
this.pStructProperties = StructuralUnitProperty.fromStringToList(corpus, s); |
|
| 289 |
|
|
| 290 |
s = this.getStringParameterValue(InternalViewPreferences.PROPERTIES); |
|
| 291 |
this.pProperties = WordProperty.fromStringToList(corpus, s); |
|
| 292 |
|
|
| 274 | 293 |
return true; |
| 275 | 294 |
} |
| 276 | 295 |
|
| tmp/org.txm.internalview.core/src/org/txm/functions/internal/InternalViewPreferences.java (revision 561) | ||
|---|---|---|
| 12 | 12 |
/** the selected word properties */ |
| 13 | 13 |
public static final String PROPERTIES = "properties"; |
| 14 | 14 |
/** the selected structural units */ |
| 15 |
public static final String STRUCTURALUNITS = "structuralunits";
|
|
| 15 |
public static final String STRUCTURALUNIT = "structuralunit";
|
|
| 16 | 16 |
/** the selected structure properties */ |
| 17 | 17 |
public static final String STRUCTURALUNITPROPERTIES = "structuralunitsproperties"; |
| 18 | 18 |
/** the current page number */ |
| ... | ... | |
| 23 | 23 |
Preferences preferences = DefaultScope.INSTANCE.getNode(PREFERENCES_NODE); |
| 24 | 24 |
|
| 25 | 25 |
preferences.put(PROPERTIES, "word"); |
| 26 |
preferences.put(STRUCTURALUNITS, "text"); |
|
| 26 |
preferences.put(STRUCTURALUNIT, "text"); |
|
| 27 |
preferences.put(STRUCTURALUNITPROPERTIES, "text_id"); |
|
| 27 | 28 |
preferences.putInt(CURRENTPAGE, 0); |
| 28 | 29 |
} |
| 29 | 30 |
} |
| tmp/org.txm.ca.rcp/src/org/txm/ca/rcp/messages/CAUIMessages.java (revision 561) | ||
|---|---|---|
| 3 | 3 |
import org.eclipse.osgi.util.NLS; |
| 4 | 4 |
import org.txm.utils.messages.Utf8NLS; |
| 5 | 5 |
|
| 6 |
public class CAUIMessages extends NLS {
|
|
| 7 |
|
|
| 8 |
private static final String BUNDLE_NAME = "org.txm.ca.rcp.messages.messages"; //$NON-NLS-1$ |
|
| 9 |
|
|
| 6 |
public class CAUIMessages extends Utf8NLS {
|
|
| 7 |
|
|
| 8 |
static {
|
|
| 9 |
Utf8NLS.initializeMessages(CAUIMessages.class); |
|
| 10 |
} |
|
| 11 |
|
|
| 10 | 12 |
public static String PreferencePage_0; |
| 11 | 13 |
public static String PreferencePage_1; |
| 12 | 14 |
public static String PreferencePage_2; |
| ... | ... | |
| 48 | 50 |
public static String CorrespondanceAnalysisEditorInput_9; |
| 49 | 51 |
|
| 50 | 52 |
public static String PreferencePage_CHART_SHOW_POINT_SHAPES; |
| 51 |
|
|
| 52 |
|
|
| 53 |
static {
|
|
| 54 |
// initializes resource bundle |
|
| 55 |
Utf8NLS.initializeMessages(BUNDLE_NAME, CAUIMessages.class); |
|
| 56 |
} |
|
| 57 |
|
|
| 58 |
private CAUIMessages() {
|
|
| 59 |
//no instantiation |
|
| 60 |
} |
|
| 61 | 53 |
} |
| tmp/org.txm.core/.classpath (revision 561) | ||
|---|---|---|
| 32 | 32 |
<attributes> |
| 33 | 33 |
<attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="org.txm.toolbox/native"/> |
| 34 | 34 |
</attributes> |
| 35 |
<accessrules> |
|
| 36 |
<accessrule kind="accessible" pattern="**"/> |
|
| 37 |
</accessrules> |
|
| 35 | 38 |
</classpathentry> |
| 36 | 39 |
<classpathentry kind="src" path="src/java"/> |
| 37 | 40 |
<classpathentry kind="src" path="test"/> |
| tmp/org.txm.core/src/java/org/txm/core/preferences/TXMPreferences.java (revision 561) | ||
|---|---|---|
| 13 | 13 |
import java.util.Iterator; |
| 14 | 14 |
import java.util.Set; |
| 15 | 15 |
|
| 16 |
import org.eclipse.core.internal.preferences.Base64; |
|
| 17 | 16 |
import org.eclipse.core.runtime.Platform; |
| 18 | 17 |
import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer; |
| 19 | 18 |
import org.eclipse.core.runtime.preferences.DefaultScope; |
| ... | ... | |
| 71 | 70 |
scope.getNode(node).remove(key); |
| 72 | 71 |
} |
| 73 | 72 |
|
| 73 |
/** |
|
| 74 |
* Looks for the value of the specified <code>key</code> in the specified parameters and preference nodes. |
|
| 75 |
* Returns the value of a key in the specified parameters if exists. |
|
| 76 |
* Otherwise try to get it from the local result node if exists. |
|
| 77 |
* Otherwise try to get it from the specified node qualifier if exists. |
|
| 78 |
* Otherwise try to get it from the specified alternative node qualifiers defined in the list <code>TXMPreferences.alternativeNodesQualifiers</code>. |
|
| 79 |
* Returns <code>false</code> if no value has been found for the specified key. |
|
| 80 |
* @param commandParameters |
|
| 81 |
* @param nodeQualifier |
|
| 82 |
* @param result |
|
| 83 |
* @param key |
|
| 84 |
* @return |
|
| 85 |
*/ |
|
| 86 |
public static Serializable getSerializable(String key, TXMParameters commandParameters, TXMResult result, String nodeQualifier) {
|
|
| 87 |
if(commandParameters != null && commandParameters.get(key) != null) {
|
|
| 88 |
return (Serializable) commandParameters.get(key); |
|
| 89 |
} |
|
| 90 |
return getSerializable(key, result, nodeQualifier); |
|
| 91 |
} |
|
| 74 | 92 |
|
| 93 |
|
|
| 94 |
public static Serializable getSerializable(String key, TXMParameters commandParameters, TXMResult result) {
|
|
| 95 |
return getSerializable(key, commandParameters, result, result.getPreferencesNodeQualifier()); |
|
| 96 |
} |
|
| 97 |
|
|
| 75 | 98 |
/** |
| 99 |
* Looks for the value of the specified <code>key</code> in preference nodes. |
|
| 100 |
* Returns the value of the local result node if exists. |
|
| 101 |
* Otherwise try to get it from the specified node qualifier if exists. |
|
| 102 |
* Otherwise try to get it from the specified alternative node qualifiers defined in the list <code>TXMPreferences.alternativeNodesQualifiers</code>. |
|
| 103 |
* Returns defaultValue if no value has been found for the specified key. |
|
| 104 |
* @param nodeQualifier |
|
| 105 |
* @param result |
|
| 106 |
* @param key |
|
| 107 |
* @return |
|
| 108 |
*/ |
|
| 109 |
public static Serializable getSerializable(String key, TXMResult result, String nodeQualifier, Serializable defaultValue) {
|
|
| 110 |
|
|
| 111 |
nodeQualifier = findNodeQualifier(nodeQualifier, result, key); |
|
| 112 |
|
|
| 113 |
String data_default = DefaultScope.INSTANCE.getNode(nodeQualifier).get(key, ""); |
|
| 114 |
if (data_default.length() == 0) {
|
|
| 115 |
return null; |
|
| 116 |
} |
|
| 117 |
|
|
| 118 |
try {
|
|
| 119 |
defaultValue = fromString(data_default); |
|
| 120 |
} catch (Exception e) {
|
|
| 121 |
Log.printStackTrace(e); |
|
| 122 |
} |
|
| 123 |
|
|
| 124 |
String data_return = scope.getNode(nodeQualifier).get(key, ""); |
|
| 125 |
if (data_return.length() == 0) {
|
|
| 126 |
return defaultValue; |
|
| 127 |
} |
|
| 128 |
|
|
| 129 |
try {
|
|
| 130 |
return fromString(data_return); |
|
| 131 |
} catch (Exception e) {
|
|
| 132 |
Log.printStackTrace(e); |
|
| 133 |
} |
|
| 134 |
return null; |
|
| 135 |
} |
|
| 136 |
|
|
| 137 |
/** |
|
| 138 |
* Looks for the value of the specified <code>key</code> in preference nodes. |
|
| 139 |
* Returns the value of the local result node if exists. |
|
| 140 |
* Otherwise try to get it from the specified node qualifier if exists. |
|
| 141 |
* Otherwise try to get it from the specified alternative node qualifiers defined in the list <code>TXMPreferences.alternativeNodesQualifiers</code>. |
|
| 142 |
* Returns <code>false</code> if no value has been found for the specified key. |
|
| 143 |
* @param nodeQualifier |
|
| 144 |
* @param result |
|
| 145 |
* @param key |
|
| 146 |
* @return |
|
| 147 |
*/ |
|
| 148 |
public static Serializable getSerializable(String key, TXMResult result, String nodeQualifier) {
|
|
| 149 |
return getSerializable(key, result, nodeQualifier, false); |
|
| 150 |
} |
|
| 151 |
|
|
| 152 |
/** |
|
| 153 |
* |
|
| 154 |
* @param nodeQualifier |
|
| 155 |
* @param key |
|
| 156 |
* @return |
|
| 157 |
*/ |
|
| 158 |
public static Serializable getSerializable(String key, String nodeQualifier) {
|
|
| 159 |
return getSerializable(key, null, nodeQualifier); |
|
| 160 |
} |
|
| 161 |
|
|
| 162 |
/** |
|
| 163 |
* |
|
| 164 |
* @param result |
|
| 165 |
* @param key |
|
| 166 |
* @return |
|
| 167 |
*/ |
|
| 168 |
public static Serializable getSerializable(String key, TXMResult result) {
|
|
| 169 |
return getSerializable(key, result, null); |
|
| 170 |
} |
|
| 171 |
|
|
| 172 |
/** |
|
| 76 | 173 |
* Looks for the value of the specified <code>key</code> in the specified parameters and preference nodes. |
| 77 | 174 |
* Returns the value of a key in the specified parameters if exists. |
| 78 | 175 |
* Otherwise try to get it from the local result node if exists. |
| ... | ... | |
| 765 | 862 |
e.printStackTrace(); |
| 766 | 863 |
} |
| 767 | 864 |
} |
| 768 |
|
|
| 865 |
|
|
| 769 | 866 |
/** |
| 770 | 867 |
* Dumps the keys and values of the specified node. |
| 771 | 868 |
* @param nodeQualifier |
| ... | ... | |
| 858 | 955 |
if(keyExists) {
|
| 859 | 956 |
nodeQualifier = result.getUUID(); |
| 860 | 957 |
} |
| 861 |
|
|
| 958 |
|
|
| 862 | 959 |
// look in the global alternative preferences nodes |
| 863 | 960 |
else if(!keyExists(nodeQualifier, key)) {
|
| 864 | 961 |
for(int i = 0; i < alternativeNodesQualifiers.size(); i++) {
|
| ... | ... | |
| 926 | 1023 |
else if (Boolean.class.isInstance(value)) {
|
| 927 | 1024 |
scope.getNode(nodeQualifier).putBoolean(key, (Boolean) value); |
| 928 | 1025 |
} |
| 1026 |
else if (Serializable.class.isInstance(value)) {
|
|
| 1027 |
try {
|
|
| 1028 |
scope.getNode(nodeQualifier).put(key, toString((Serializable)value)); |
|
| 1029 |
} catch (IOException e) {
|
|
| 1030 |
// TODO Auto-generated catch block |
|
| 1031 |
e.printStackTrace(); |
|
| 1032 |
} |
|
| 1033 |
} |
|
| 1034 |
// FIXME: to do |
|
| 929 | 1035 |
// FIXME: need to prove/validate this methode |
| 930 | 1036 |
else if (byte.class.isAssignableFrom(value.getClass().getComponentType())) {
|
| 931 | 1037 |
System.err.println("TXMPreferences.put(): byte array -> need to prove/validate this method.");
|
| 932 | 1038 |
scope.getNode(nodeQualifier).putByteArray(key, (byte[]) value); |
| 933 | 1039 |
} |
| 934 |
|
|
| 1040 |
else {
|
|
| 935 | 1041 |
// FIXME: to do if needed |
| 936 |
// else if (Serializable.class.isInstance(value)) {
|
|
| 937 |
// putLocalSerializable(result, key, (Serializable) value); |
|
| 938 |
else {
|
|
| 939 | 1042 |
//FIXME: Debug |
| 940 |
System.err.println("TXMPreferences.put(): error, can't find a put method that matches the value type: " + value.getClass() + "=" + value + ".");
|
|
| 1043 |
System.err.println("TXMPreferences.putLocal(): error, can't find a put method that matches the value type: " + value.getClass() + "=" + value + ".");
|
|
| 941 | 1044 |
} |
| 942 |
|
|
| 943 |
|
|
| 944 | 1045 |
} |
| 1046 |
|
|
| 1047 |
/** Read the object from Base64 string. */ |
|
| 1048 |
private static Serializable fromString( String s ) throws IOException , |
|
| 1049 |
ClassNotFoundException {
|
|
| 1050 |
|
|
| 1051 |
byte [] data = Base64.getDecoder().decode( s ); |
|
| 1052 |
ObjectInputStream ois = new ObjectInputStream( |
|
| 1053 |
new ByteArrayInputStream( data ) ); |
|
| 1054 |
Object o = ois.readObject(); |
|
| 1055 |
ois.close(); |
|
| 1056 |
return (Serializable)o; |
|
| 1057 |
} |
|
| 1058 |
|
|
| 1059 |
/** Write the object to a Base64 string. */ |
|
| 1060 |
private static String toString( Serializable o ) throws IOException {
|
|
| 1061 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|
| 1062 |
ObjectOutputStream oos = new ObjectOutputStream( baos ); |
|
| 1063 |
oos.writeObject( o ); |
|
| 1064 |
oos.close(); |
|
| 1065 |
return Base64.getEncoder().encodeToString(baos.toByteArray()); |
|
| 1066 |
} |
|
| 945 | 1067 |
|
| 946 | 1068 |
// |
| 947 | 1069 |
// /** |
| ... | ... | |
| 1007 | 1129 |
} |
| 1008 | 1130 |
} |
| 1009 | 1131 |
} |
| 1010 |
|
|
| 1132 |
|
|
| 1011 | 1133 |
//FIXME: this code is dedicated to dynamically retrieve the static field PREFERENCES_NODE of the runtime subclass and use it super class to avoid to pass it to all methods. |
| 1012 | 1134 |
// eg. to use ProgressionPreferences.getBoolean("test") instead of TXMPreferences.getBoolean(ProgressionPreferences.PREFERENCES_NODE, "test")
|
| 1013 | 1135 |
// but it doesn"t work, need to continue the tests... |
| tmp/org.txm.core/src/java/org/txm/core/preferences/Base64.java (revision 561) | ||
|---|---|---|
| 1 |
/* |
|
| 2 |
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
|
| 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
| 4 |
* |
|
| 5 |
* This code is free software; you can redistribute it and/or modify it |
|
| 6 |
* under the terms of the GNU General Public License version 2 only, as |
|
| 7 |
* published by the Free Software Foundation. Oracle designates this |
|
| 8 |
* particular file as subject to the "Classpath" exception as provided |
|
| 9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
| 10 |
* |
|
| 11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
| 12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
| 13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
| 14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
| 15 |
* accompanied this code). |
|
| 16 |
* |
|
| 17 |
* You should have received a copy of the GNU General Public License version |
|
| 18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
| 19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
| 20 |
* |
|
| 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
| 22 |
* or visit www.oracle.com if you need additional information or have any |
|
| 23 |
* questions. |
|
| 24 |
*/ |
|
| 25 |
|
|
| 26 |
package org.txm.core.preferences; |
|
| 27 |
|
|
| 28 |
import java.io.FilterOutputStream; |
|
| 29 |
import java.io.IOException; |
|
| 30 |
import java.io.InputStream; |
|
| 31 |
import java.io.OutputStream; |
|
| 32 |
import java.nio.ByteBuffer; |
|
| 33 |
import java.nio.charset.StandardCharsets; |
|
| 34 |
import java.util.Arrays; |
|
| 35 |
import java.util.Objects; |
|
| 36 |
|
|
| 37 |
/** |
|
| 38 |
* This class consists exclusively of static methods for obtaining |
|
| 39 |
* encoders and decoders for the Base64 encoding scheme. The |
|
| 40 |
* implementation of this class supports the following types of Base64 |
|
| 41 |
* as specified in |
|
| 42 |
* <a href="http://www.ietf.org/rfc/rfc4648.txt">RFC 4648</a> and |
|
| 43 |
* <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a>. |
|
| 44 |
* |
|
| 45 |
* <ul> |
|
| 46 |
* <li><a name="basic"><b>Basic</b></a> |
|
| 47 |
* <p> Uses "The Base64 Alphabet" as specified in Table 1 of |
|
| 48 |
* RFC 4648 and RFC 2045 for encoding and decoding operation. |
|
| 49 |
* The encoder does not add any line feed (line separator) |
|
| 50 |
* character. The decoder rejects data that contains characters |
|
| 51 |
* outside the base64 alphabet.</p></li> |
|
| 52 |
* |
|
| 53 |
* <li><a name="url"><b>URL and Filename safe</b></a> |
|
| 54 |
* <p> Uses the "URL and Filename safe Base64 Alphabet" as specified |
|
| 55 |
* in Table 2 of RFC 4648 for encoding and decoding. The |
|
| 56 |
* encoder does not add any line feed (line separator) character. |
|
| 57 |
* The decoder rejects data that contains characters outside the |
|
| 58 |
* base64 alphabet.</p></li> |
|
| 59 |
* |
|
| 60 |
* <li><a name="mime"><b>MIME</b></a> |
|
| 61 |
* <p> Uses the "The Base64 Alphabet" as specified in Table 1 of |
|
| 62 |
* RFC 2045 for encoding and decoding operation. The encoded output |
|
| 63 |
* must be represented in lines of no more than 76 characters each |
|
| 64 |
* and uses a carriage return {@code '\r'} followed immediately by
|
|
| 65 |
* a linefeed {@code '\n'} as the line separator. No line separator
|
|
| 66 |
* is added to the end of the encoded output. All line separators |
|
| 67 |
* or other characters not found in the base64 alphabet table are |
|
| 68 |
* ignored in decoding operation.</p></li> |
|
| 69 |
* </ul> |
|
| 70 |
* |
|
| 71 |
* <p> Unless otherwise noted, passing a {@code null} argument to a
|
|
| 72 |
* method of this class will cause a {@link java.lang.NullPointerException
|
|
| 73 |
* NullPointerException} to be thrown. |
|
| 74 |
* |
|
| 75 |
* @author Xueming Shen |
|
| 76 |
* @since 1.8 |
|
| 77 |
*/ |
|
| 78 |
//TODO: switch to Java 1.8 and drop this copy of java.util.Base64 |
|
| 79 |
public class Base64 {
|
|
| 80 |
|
|
| 81 |
private Base64() {}
|
|
| 82 |
|
|
| 83 |
/** |
|
| 84 |
* Returns a {@link Encoder} that encodes using the
|
|
| 85 |
* <a href="#basic">Basic</a> type base64 encoding scheme. |
|
| 86 |
* |
|
| 87 |
* @return A Base64 encoder. |
|
| 88 |
*/ |
|
| 89 |
public static Encoder getEncoder() {
|
|
| 90 |
return Encoder.RFC4648; |
|
| 91 |
} |
|
| 92 |
|
|
| 93 |
/** |
|
| 94 |
* Returns a {@link Encoder} that encodes using the
|
|
| 95 |
* <a href="#url">URL and Filename safe</a> type base64 |
|
| 96 |
* encoding scheme. |
|
| 97 |
* |
|
| 98 |
* @return A Base64 encoder. |
|
| 99 |
*/ |
|
| 100 |
public static Encoder getUrlEncoder() {
|
|
| 101 |
return Encoder.RFC4648_URLSAFE; |
|
| 102 |
} |
|
| 103 |
|
|
| 104 |
/** |
|
| 105 |
* Returns a {@link Encoder} that encodes using the
|
|
| 106 |
* <a href="#mime">MIME</a> type base64 encoding scheme. |
|
| 107 |
* |
|
| 108 |
* @return A Base64 encoder. |
|
| 109 |
*/ |
|
| 110 |
public static Encoder getMimeEncoder() {
|
|
| 111 |
return Encoder.RFC2045; |
|
| 112 |
} |
|
| 113 |
|
|
| 114 |
/** |
|
| 115 |
* Returns a {@link Encoder} that encodes using the
|
|
| 116 |
* <a href="#mime">MIME</a> type base64 encoding scheme |
|
| 117 |
* with specified line length and line separators. |
|
| 118 |
* |
|
| 119 |
* @param lineLength |
|
| 120 |
* the length of each output line (rounded down to nearest multiple |
|
| 121 |
* of 4). If {@code lineLength <= 0} the output will not be separated
|
|
| 122 |
* in lines |
|
| 123 |
* @param lineSeparator |
|
| 124 |
* the line separator for each output line |
|
| 125 |
* |
|
| 126 |
* @return A Base64 encoder. |
|
| 127 |
* |
|
| 128 |
* @throws IllegalArgumentException if {@code lineSeparator} includes any
|
|
| 129 |
* character of "The Base64 Alphabet" as specified in Table 1 of |
|
| 130 |
* RFC 2045. |
|
| 131 |
*/ |
|
| 132 |
public static Encoder getMimeEncoder(int lineLength, byte[] lineSeparator) {
|
|
| 133 |
Objects.requireNonNull(lineSeparator); |
|
| 134 |
int[] base64 = Decoder.fromBase64; |
|
| 135 |
for (byte b : lineSeparator) {
|
|
| 136 |
if (base64[b & 0xff] != -1) |
|
| 137 |
throw new IllegalArgumentException( |
|
| 138 |
"Illegal base64 line separator character 0x" + Integer.toString(b, 16)); |
|
| 139 |
} |
|
| 140 |
if (lineLength <= 0) {
|
|
| 141 |
return Encoder.RFC4648; |
|
| 142 |
} |
|
| 143 |
return new Encoder(false, lineSeparator, lineLength >> 2 << 2, true); |
|
| 144 |
} |
|
| 145 |
|
|
| 146 |
/** |
|
| 147 |
* Returns a {@link Decoder} that decodes using the
|
|
| 148 |
* <a href="#basic">Basic</a> type base64 encoding scheme. |
|
| 149 |
* |
|
| 150 |
* @return A Base64 decoder. |
|
| 151 |
*/ |
|
| 152 |
public static Decoder getDecoder() {
|
|
| 153 |
return Decoder.RFC4648; |
|
| 154 |
} |
|
| 155 |
|
|
| 156 |
/** |
|
| 157 |
* Returns a {@link Decoder} that decodes using the
|
|
| 158 |
* <a href="#url">URL and Filename safe</a> type base64 |
|
| 159 |
* encoding scheme. |
|
| 160 |
* |
|
| 161 |
* @return A Base64 decoder. |
|
| 162 |
*/ |
|
| 163 |
public static Decoder getUrlDecoder() {
|
|
| 164 |
return Decoder.RFC4648_URLSAFE; |
|
| 165 |
} |
|
| 166 |
|
|
| 167 |
/** |
|
| 168 |
* Returns a {@link Decoder} that decodes using the
|
|
| 169 |
* <a href="#mime">MIME</a> type base64 decoding scheme. |
|
| 170 |
* |
|
| 171 |
* @return A Base64 decoder. |
|
| 172 |
*/ |
|
| 173 |
public static Decoder getMimeDecoder() {
|
|
| 174 |
return Decoder.RFC2045; |
|
| 175 |
} |
|
| 176 |
|
|
| 177 |
/** |
|
| 178 |
* This class implements an encoder for encoding byte data using |
|
| 179 |
* the Base64 encoding scheme as specified in RFC 4648 and RFC 2045. |
|
| 180 |
* |
|
| 181 |
* <p> Instances of {@link Encoder} class are safe for use by
|
|
| 182 |
* multiple concurrent threads. |
|
| 183 |
* |
|
| 184 |
* <p> Unless otherwise noted, passing a {@code null} argument to
|
|
| 185 |
* a method of this class will cause a |
|
| 186 |
* {@link java.lang.NullPointerException NullPointerException} to
|
|
| 187 |
* be thrown. |
|
| 188 |
* |
|
| 189 |
* @see Decoder |
|
| 190 |
* @since 1.8 |
|
| 191 |
*/ |
|
| 192 |
public static class Encoder {
|
|
| 193 |
|
|
| 194 |
private final byte[] newline; |
|
| 195 |
private final int linemax; |
|
| 196 |
private final boolean isURL; |
|
| 197 |
private final boolean doPadding; |
|
| 198 |
|
|
| 199 |
private Encoder(boolean isURL, byte[] newline, int linemax, boolean doPadding) {
|
|
| 200 |
this.isURL = isURL; |
|
| 201 |
this.newline = newline; |
|
| 202 |
this.linemax = linemax; |
|
| 203 |
this.doPadding = doPadding; |
|
| 204 |
} |
|
| 205 |
|
|
| 206 |
/** |
|
| 207 |
* This array is a lookup table that translates 6-bit positive integer |
|
| 208 |
* index values into their "Base64 Alphabet" equivalents as specified |
|
| 209 |
* in "Table 1: The Base64 Alphabet" of RFC 2045 (and RFC 4648). |
|
| 210 |
*/ |
|
| 211 |
private static final char[] toBase64 = {
|
|
| 212 |
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', |
|
| 213 |
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', |
|
| 214 |
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', |
|
| 215 |
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', |
|
| 216 |
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' |
|
| 217 |
}; |
|
| 218 |
|
|
| 219 |
/** |
|
| 220 |
* It's the lookup table for "URL and Filename safe Base64" as specified |
|
| 221 |
* in Table 2 of the RFC 4648, with the '+' and '/' changed to '-' and |
|
| 222 |
* '_'. This table is used when BASE64_URL is specified. |
|
| 223 |
*/ |
|
| 224 |
private static final char[] toBase64URL = {
|
|
| 225 |
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', |
|
| 226 |
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', |
|
| 227 |
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', |
|
| 228 |
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', |
|
| 229 |
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_' |
|
| 230 |
}; |
|
| 231 |
|
|
| 232 |
private static final int MIMELINEMAX = 76; |
|
| 233 |
private static final byte[] CRLF = new byte[] {'\r', '\n'};
|
|
| 234 |
|
|
| 235 |
static final Encoder RFC4648 = new Encoder(false, null, -1, true); |
|
| 236 |
static final Encoder RFC4648_URLSAFE = new Encoder(true, null, -1, true); |
|
| 237 |
static final Encoder RFC2045 = new Encoder(false, CRLF, MIMELINEMAX, true); |
|
| 238 |
|
|
| 239 |
private final int outLength(int srclen) {
|
|
| 240 |
int len = 0; |
|
| 241 |
if (doPadding) {
|
|
| 242 |
len = 4 * ((srclen + 2) / 3); |
|
| 243 |
} else {
|
|
| 244 |
int n = srclen % 3; |
|
| 245 |
len = 4 * (srclen / 3) + (n == 0 ? 0 : n + 1); |
|
| 246 |
} |
|
| 247 |
if (linemax > 0) // line separators |
|
| 248 |
len += (len - 1) / linemax * newline.length; |
|
| 249 |
return len; |
|
| 250 |
} |
|
| 251 |
|
|
| 252 |
/** |
|
| 253 |
* Encodes all bytes from the specified byte array into a newly-allocated |
|
| 254 |
* byte array using the {@link Base64} encoding scheme. The returned byte
|
|
| 255 |
* array is of the length of the resulting bytes. |
|
| 256 |
* |
|
| 257 |
* @param src |
|
| 258 |
* the byte array to encode |
|
| 259 |
* @return A newly-allocated byte array containing the resulting |
|
| 260 |
* encoded bytes. |
|
| 261 |
*/ |
|
| 262 |
public byte[] encode(byte[] src) {
|
|
| 263 |
int len = outLength(src.length); // dst array size |
|
| 264 |
byte[] dst = new byte[len]; |
|
| 265 |
int ret = encode0(src, 0, src.length, dst); |
|
| 266 |
if (ret != dst.length) |
|
| 267 |
return Arrays.copyOf(dst, ret); |
|
| 268 |
return dst; |
|
| 269 |
} |
|
| 270 |
|
|
| 271 |
/** |
|
| 272 |
* Encodes all bytes from the specified byte array using the |
|
| 273 |
* {@link Base64} encoding scheme, writing the resulting bytes to the
|
|
| 274 |
* given output byte array, starting at offset 0. |
|
| 275 |
* |
|
| 276 |
* <p> It is the responsibility of the invoker of this method to make |
|
| 277 |
* sure the output byte array {@code dst} has enough space for encoding
|
|
| 278 |
* all bytes from the input byte array. No bytes will be written to the |
|
| 279 |
* output byte array if the output byte array is not big enough. |
|
| 280 |
* |
|
| 281 |
* @param src |
|
| 282 |
* the byte array to encode |
|
| 283 |
* @param dst |
|
| 284 |
* the output byte array |
|
| 285 |
* @return The number of bytes written to the output byte array |
|
| 286 |
* |
|
| 287 |
* @throws IllegalArgumentException if {@code dst} does not have enough
|
|
| 288 |
* space for encoding all input bytes. |
|
| 289 |
*/ |
|
| 290 |
public int encode(byte[] src, byte[] dst) {
|
|
| 291 |
int len = outLength(src.length); // dst array size |
|
| 292 |
if (dst.length < len) |
|
| 293 |
throw new IllegalArgumentException( |
|
| 294 |
"Output byte array is too small for encoding all input bytes"); |
|
| 295 |
return encode0(src, 0, src.length, dst); |
|
| 296 |
} |
|
| 297 |
|
|
| 298 |
/** |
|
| 299 |
* Encodes the specified byte array into a String using the {@link Base64}
|
|
| 300 |
* encoding scheme. |
|
| 301 |
* |
|
| 302 |
* <p> This method first encodes all input bytes into a base64 encoded |
|
| 303 |
* byte array and then constructs a new String by using the encoded byte |
|
| 304 |
* array and the {@link java.nio.charset.StandardCharsets#ISO_8859_1
|
|
| 305 |
* ISO-8859-1} charset. |
|
| 306 |
* |
|
| 307 |
* <p> In other words, an invocation of this method has exactly the same |
|
| 308 |
* effect as invoking |
|
| 309 |
* {@code new String(encode(src), StandardCharsets.ISO_8859_1)}.
|
|
| 310 |
* |
|
| 311 |
* @param src |
|
| 312 |
* the byte array to encode |
|
| 313 |
* @return A String containing the resulting Base64 encoded characters |
|
| 314 |
*/ |
|
| 315 |
@SuppressWarnings("deprecation")
|
|
| 316 |
public String encodeToString(byte[] src) {
|
|
| 317 |
byte[] encoded = encode(src); |
|
| 318 |
return new String(encoded, 0, 0, encoded.length); |
|
| 319 |
} |
|
| 320 |
|
|
| 321 |
/** |
|
| 322 |
* Encodes all remaining bytes from the specified byte buffer into |
|
| 323 |
* a newly-allocated ByteBuffer using the {@link Base64} encoding
|
|
| 324 |
* scheme. |
|
| 325 |
* |
|
| 326 |
* Upon return, the source buffer's position will be updated to |
|
| 327 |
* its limit; its limit will not have been changed. The returned |
|
| 328 |
* output buffer's position will be zero and its limit will be the |
|
| 329 |
* number of resulting encoded bytes. |
|
| 330 |
* |
|
| 331 |
* @param buffer |
|
| 332 |
* the source ByteBuffer to encode |
|
| 333 |
* @return A newly-allocated byte buffer containing the encoded bytes. |
|
| 334 |
*/ |
|
| 335 |
public ByteBuffer encode(ByteBuffer buffer) {
|
|
| 336 |
int len = outLength(buffer.remaining()); |
|
| 337 |
byte[] dst = new byte[len]; |
|
| 338 |
int ret = 0; |
|
| 339 |
if (buffer.hasArray()) {
|
|
| 340 |
ret = encode0(buffer.array(), |
|
| 341 |
buffer.arrayOffset() + buffer.position(), |
|
| 342 |
buffer.arrayOffset() + buffer.limit(), |
|
| 343 |
dst); |
|
| 344 |
buffer.position(buffer.limit()); |
|
| 345 |
} else {
|
|
| 346 |
byte[] src = new byte[buffer.remaining()]; |
|
| 347 |
buffer.get(src); |
|
| 348 |
ret = encode0(src, 0, src.length, dst); |
|
| 349 |
} |
|
| 350 |
if (ret != dst.length) |
|
| 351 |
dst = Arrays.copyOf(dst, ret); |
|
| 352 |
return ByteBuffer.wrap(dst); |
|
| 353 |
} |
|
| 354 |
|
|
| 355 |
/** |
|
| 356 |
* Wraps an output stream for encoding byte data using the {@link Base64}
|
|
| 357 |
* encoding scheme. |
|
| 358 |
* |
|
| 359 |
* <p> It is recommended to promptly close the returned output stream after |
|
| 360 |
* use, during which it will flush all possible leftover bytes to the underlying |
|
| 361 |
* output stream. Closing the returned output stream will close the underlying |
|
| 362 |
* output stream. |
|
| 363 |
* |
|
| 364 |
* @param os |
|
| 365 |
* the output stream. |
|
| 366 |
* @return the output stream for encoding the byte data into the |
|
| 367 |
* specified Base64 encoded format |
|
| 368 |
*/ |
|
| 369 |
public OutputStream wrap(OutputStream os) {
|
|
| 370 |
Objects.requireNonNull(os); |
|
| 371 |
return new EncOutputStream(os, isURL ? toBase64URL : toBase64, |
|
| 372 |
newline, linemax, doPadding); |
|
| 373 |
} |
|
| 374 |
|
|
| 375 |
/** |
|
| 376 |
* Returns an encoder instance that encodes equivalently to this one, |
|
| 377 |
* but without adding any padding character at the end of the encoded |
|
| 378 |
* byte data. |
|
| 379 |
* |
|
| 380 |
* <p> The encoding scheme of this encoder instance is unaffected by |
|
| 381 |
* this invocation. The returned encoder instance should be used for |
|
| 382 |
* non-padding encoding operation. |
|
| 383 |
* |
|
| 384 |
* @return an equivalent encoder that encodes without adding any |
|
| 385 |
* padding character at the end |
|
| 386 |
*/ |
|
| 387 |
public Encoder withoutPadding() {
|
|
| 388 |
if (!doPadding) |
|
| 389 |
return this; |
|
| 390 |
return new Encoder(isURL, newline, linemax, false); |
|
| 391 |
} |
|
| 392 |
|
|
| 393 |
private int encode0(byte[] src, int off, int end, byte[] dst) {
|
|
| 394 |
char[] base64 = isURL ? toBase64URL : toBase64; |
|
| 395 |
int sp = off; |
|
| 396 |
int slen = (end - off) / 3 * 3; |
|
| 397 |
int sl = off + slen; |
|
| 398 |
if (linemax > 0 && slen > linemax / 4 * 3) |
|
| 399 |
slen = linemax / 4 * 3; |
|
| 400 |
int dp = 0; |
|
| 401 |
while (sp < sl) {
|
|
| 402 |
int sl0 = Math.min(sp + slen, sl); |
|
| 403 |
for (int sp0 = sp, dp0 = dp ; sp0 < sl0; ) {
|
|
| 404 |
int bits = (src[sp0++] & 0xff) << 16 | |
|
| 405 |
(src[sp0++] & 0xff) << 8 | |
|
| 406 |
(src[sp0++] & 0xff); |
|
| 407 |
dst[dp0++] = (byte)base64[(bits >>> 18) & 0x3f]; |
|
| 408 |
dst[dp0++] = (byte)base64[(bits >>> 12) & 0x3f]; |
|
| 409 |
dst[dp0++] = (byte)base64[(bits >>> 6) & 0x3f]; |
|
| 410 |
dst[dp0++] = (byte)base64[bits & 0x3f]; |
|
| 411 |
} |
|
| 412 |
int dlen = (sl0 - sp) / 3 * 4; |
|
Formats disponibles : Unified diff