📄 translation.cs
字号:
/*
Copyright 2006 Christian Gross http://www.devspace.com
From the book Ajax Patterns and Best Practices
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
using System.Web;
using System.Collections.Generic;
using Devspace.Commons.TDD;
using System.Collections;
using System.Threading;
using Devspace.Ajax.PersistentCommunications;
using Devspace.Ajax.InfiniteData;
using Devspace.Ajax.RestMVC;
using System.IO;
namespace Devspace.Ajax.RestMVC.Translation {
public class WordToTranslateRequest : IRequest {
private string _word;
public WordToTranslateRequest(string word) {
_word = word;
}
public String Word {
get {
return _word;
}
}
}
public class TranslatedWordResult : IResult {
private string _original;
private string _translation;
private string _language;
public TranslatedWordResult(string original, string translation, string language) {
_original = original;
_translation = translation;
_language = language;
}
public String Original {
get {
return _original;
}
}
public String Translation {
get {
return _translation;
}
}
public String Language {
get {
return _language;
}
}
}
public class GermanTranslationImplementation : ICommand, IRunnable {
IRequest _request;
IParent _parent;
public void SetRequest(IRequest request) {
_request = request;
}
public void AssignParent(IParent parent) {
_parent = parent;
}
public String Identifier {
get {
return "german";
}
}
public void Run() {
if(((WordToTranslateRequest)_request).Word == "hello") {
_parent.AddResult( new TranslatedWordResult( "hello", "hallo", "german"));
}
}
}
public class FrenchTranslationImplementation : ICommand, IRunnable {
IRequest _request;
IParent _parent;
public void SetRequest(IRequest request) {
_request = request;
}
public void AssignParent(IParent parent) {
_parent = parent;
}
public String Identifier {
get {
return "french";
}
}
public void Run() {
if(((WordToTranslateRequest)_request).Word == "hello") {
_parent.AddResult( new TranslatedWordResult( "hello", "bonjour", "french"));
}
}
}
public class TranslationHandler : RedirectionHttpHandlerBase {
static Hashtable _tasks = new Hashtable();
public TranslationHandler() {
AssignUserResolver(new GenericCookieUserResolverImplementation());
}
protected override bool DoPermutations {
get {
return true;
}
}
protected override bool IsMimeSupported( string mimeType) {
if( mimeType == "text/xml") {
return true;
}
else if( mimeType == "text/html") {
return true;
}
return false;
}
protected internal override void DoGet(HttpRequest req, HttpResponse resp) {
SynchronousParent parent = new SynchronousParent();
parent.AddCommand(new GermanTranslationImplementation());
parent.AddCommand(new FrenchTranslationImplementation());
String wordToTranslate = req.QueryString.Get("word");
if (wordToTranslate != null && wordToTranslate.Length > 0) {
parent.ProcessRequest(new WordToTranslateRequest(wordToTranslate));
if (_mimeType == "text/xml") {
resp.ContentType = "text/xml";
TextWriter output = resp.Output;
output.WriteLine("<results>");
foreach (TranslatedWordResult result in parent) {
output.WriteLine("<result>");
output.WriteLine("<original>" + result.Original + "</original>");
output.WriteLine("<translated>" + result.Translation + "</translated>");
output.WriteLine("<language>" + result.Language + "</language>");
output.WriteLine("</result>");
}
output.WriteLine("</results>");
resp.StatusCode = 200;
}
else if( _mimeType == "text/html") {
resp.ContentType = "text/html";
TextWriter output = resp.Output;
output.WriteLine("Results:<br />");
foreach (TranslatedWordResult result in parent) {
output.WriteLine("original :" + result.Original + "<br />");
output.WriteLine("translated :" + result.Translation + "<br />");
output.WriteLine("languge:" + result.Language + "<br />");
}
resp.StatusCode = 200;
}
}
}
protected internal override void DoPost(HttpRequest req, HttpResponse resp) {
throw new Exception( "HTTP Post is not supported");
}
protected internal override String BaseURL {
get {
return "/restmvc/dotranslation";
}
}
protected internal override String GetURLEnding() {
return _identifiedUser.Identifier;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -